October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Experiencing This Mysterious Bluetooth Stack Programming

  • October 13, 2006
  • By Alex Gusev
  • Send Email »
  • More Articles »

Getting the devices' service list

Well, once your application has trapped some BT device, why not discover which services it actually does support? With the Widcomm SDK, it's a rather trivial task to accomplish. All you're supposed to do is to call CBtIf::StartDiscovery(...) to initiate this process. If you know exactly which service you are going to discover, you may specify its GUID as a second parameter to reduce unwanted notifications. A full list of standard service GUIDs may be found in BtIfClasses.h. Again, it is an asynchronous operation, so when it finishes, a derived virtual function CYourStackClass::OnDiscoveryComplete will be launched. You can obtain the data through CBtIf::ReadDiscoveryRecords(...) call it, for example, like this:

CSdpDiscoveryRec sdpData[16];
// optionally, set it to desired service
GUID guidFilter = GUID_SERVICE_XXX;
int nServiceCount =
   m_BTStack.ReadDiscoveryRecords(btAddr,
                                  sizeof(sdpData)/
                                  sizeof(CSdpDiscoveryRec),
                                  sdpData, &guidFilter);
for (int i = 0; i < nServiceCount; i++)
{
   // process data
}

CSdpDiscoveryRec class has a number of member functions to gather any required information about given service: service name, RFComm channel, and so forth. You can easily use it as you please.

Using communications client classes

The Widcomm SDK has a number of communications client classes you may use in a quite straightforward way. I won't discuss them here because the SDK samples give you an excellent explanation on various areas: FTP client, OBEX, and so on. In many cases, you can easily adjust them to your own purposes.

One Additional Possible Wrapper

To make your code more portable between different BT stacks (and Widcomm and Microsoft aren't the only ones), you might go one step further and implement a bit more flexible approach that allows you to select a suitable SDK at runtime. Getting access to the classes exported from some DLL is a big headache in C++, so I won't go this way. My approach employs the same idea as Microsoft ActiveSync with its plug-ins. First, you can declare some common base class with desired functionality that suits your needs:

class CBTInterfaceBase
{
public:
   CBTInterfaceBase();
   virtual ~CBTInterfaceBase();

   virtual int EnumDevices(.../*you can specify all you needs here*/);
   ...
};

Then, an actual implementation will be some kind of wrapping over given BT SDK—the Widcomm SDK in the current case. You can place this implementation into a DLL and load it dynamically. The only thing left to make it work is to declare a couple of functions with predefined names for creation/deletion of a base class object:

YOURAPI CBTInterfaceBase* CreateInterface()
{
   CBaseInterfaceBase *pItfce = new CSpecificClassHere;
   return pItfce;
}

YOURAPI void DestroyInterface(CBaseInterfaceBase*& pItfce)
{
   delete pItfce;
   pItfce = NULL;
}

Thus, you have some outer interface and an actual implementation hidden inside. You even might call it a "COM Interface" if you want because it is actually the same, but doesn't require any additional registration. From the other side, it may be out of the needs of your application, so it's up to you to decide whether or not to use it.

Conclusion

You have learned about the one of available BT Stacks SDKs—Widcomm SDK for Windows Mobile OS. It gives you literally all you need to manage surrounding BT devices (well, everything has some bugs in, and this stack isn't an exception). Sample code snippets in the article illustrate some typical use cases; for more complicated ones, please refer to the SDK documentation. In the next article, you will look at what Microsoft can offer you from their own BT stack.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. After working almost a decade for an international retail software company as a team leader of the Windows Mobile R department, he has decided to dive into Symbian OS ™ Core development.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel