Bluetooth technology - equipment query and service discovery

xiaoxiao2021-03-06  39

Bluetooth technology as a newborns with short-range wireless communication technology, with its unique technical advantages, such as low cost, good anti-signal fading, use 2.4GHz ISM band, no need to apply ... due to its unique technical advantage, There is a large world in the wireless communication market. The use of Bluetooth technology is now hot topic. This article will first take the first step in the first step of Bluetooth technology, first explain how to use Bluetooth technology to realize equipment query and service discovery of the corresponding equipment. - Feng Xiaoyun is as indicated by the title, the equipment query and service finding is the first step of using Bluetooth technology, only this step can handle it, can then use the Bluetooth (Bluetooth) for Windows DK API other classes Thus, the function we need to complete, transfer file, voice, Fax ... Run implementation environment: Windows 2000 SP4 Bluetooth for Windows DK API Reference Guide (Widcomm) VC 6.0 English version first introduces the environment of the development of Bluetooth project Some necessary settings: Confirm that the corresponding SDK that provides Bluetooth development is installed. This example is the Bluetooth for Windows DK API Reference Guide, Widcomm, version 1.4.2.10 (the next setting is different due to SDK, this example setting can Referring to the above DK's help, the importation section is introduced to several key classes: CBTIF provides interface-level functions, requests and service discovery to use this class CSDPService Manage SDP service record CSDPDiscoveryRec Get SDP discovery records and provide query mode ----- ---------------------------- Due to the implementation of Bluetooth technology Similar to Socket, there must be server-side and client parts. Different from Bluetooth servers typically support multiple clients, without having to have one in P2P. The server-side and clients can be used as the initiator of the connection, so as the device query and service discovery, the server side and the client implementation is exactly the same, and the server side must have the corresponding service provided, and the client can No service support. Due to the focus of this article, the device query and service discovery are discovered, so it is not considered to add the corresponding service on the server side (the specific implementation is through the above CSDPService, csdpdpdpdiscoveryREC) to describe the specific implementation of SDP. Specifically, the implementation is mainly completed by class CBTIF. There is a brief analysis of the corresponding process: First, the corresponding class object must be inherited from the CBTIF, which must be completed, otherwise the function in the SDK cannot be used.

The specific function call relationship is as follows: Client Server CBTIF :: StartINQUIRY () --------------------------------> CBTIF :: OndeviceResponded () <-----------------------------. CBTIF :: OndeviceResponded () <---- --------------------------- CBTIF :: oninquirycomplete () <---------------- ------------------ Device Query Stage CBTIF :: StartDiscovery (Address) ----------------------> CBTIF :: OnDiscoverycomplete () <---------------------------- Serving Discovery Stage CBTIF :: ReadDiscoveryRecords () The above call relationship More clear, there is a need to explain that the task can first be initiated from the client, and the same server side can also be initiated. The image is initiated by the client. When the STARTINQUIRY () function is called, once the device responds, the ONDEVICERESPONDED () function is evoked. It is worth noting that this response can be a multiple response of the same device, or a different response of different devices. (Mainly because the same device name and other attributes can be used as different independent responses), you need to add appropriate processing in this function. Waiting for the onInquiryComplete () function to be called, indicating that the device lookup is completed, meaning the service discovery. You can then call STARTDiscovery (), which must be included with the address of a specific device when calling, this address can be obtained in the above device query. The other last thing is that when the service finds, the part of the data is obtained by calling the READISCOVERYRECORDS () function to read the specific service content. (The above functions have corresponding parameters, omitted here, specifically query the corresponding SDK), which gives the specific code below, which is convenient for everyone to understand the above process.

-------------------------------- Void CBTDLG :: Onsers () {// Todo: Add Your Control NOTIFICATION HANDLER code here if (! StartInquiry ()) // call SetDlgItemText (IDC_STATUS_TEXT, "ERROR - Unable to start device inquiry!");} void CBTDlg :: OnDeviceResponded (BD_ADDR bda, DEV_CLASS devClass, BD_NAME bdName, BOOL bConnected) // response to corresponding processing {// Add the device address and name (if any) // to the Server List It is OK to pass // duplicates This method will ignore them CString item_text;... int item_count = m_ServerList.GetItemCount ( ); // listView controlFor (int x = 0; x isbdaddrequal (bda)) {if (p_cmpinfo-> m_name.getlength () == 0) {p_cmpinfo-> m_name = bdname; m_serverlist.setitemtext (x, 0, p_cmpinfo-> devicestring ());} return;}} cbdinfo * p_info = new CBDInfo (BDA, BDNAME);

m_serverlist.insertitem (item_count, p_info-> devicesstring ()); m_serverlist.setitemdata (item_count, (lparam) p_info;

} Void CBTDlg :: OnDiscovery () {// TODO: Add your control notification handler code here StopInquiry (); found m_pDiscoveryThread = AfxBeginThread (CBTDlg :: DiscoverServices, this) // complete service by the new thread;} ​​UINT CBTDlg :: DiscoverServices (LPVOID pParam) {CBTDlg * p_Dlg = static_cast (pParam); CString strServiceName = ""; POSITION pos = p_Dlg-> m_ServerList.GetFirstSelectedItemPosition (); if (! pos = NULL) {int nItem = p_Dlg-> m_ServerList.GetNextSelectedItem (pos); CBdInfo * p_Info = (CBdInfo *) p_Dlg-> m_ServerList.GetItemData (nItem); p_Dlg-> SetDlgItemText (IDC_STATUS_TEXT, "Please Waiting for Services discovering ...:" p_Info-> DeviceAsString () );

if // Start Service Discovery {p_Dlg-> SetDlgItemText (IDC_STATUS_TEXT, "Discovery Failed!" p_Info-> DeviceAsString ());} (p_Dlg-> StartDiscovery (p_Info-> m_BdAddr, NULL)!) else {WaitForSingleObject (p_Dlg- > m_hEventStopDiscoveryThread, 30000); // wait for the completion arrival p_Dlg-> SetDlgItemText (IDC_STATUS_TEXT, "Services on:" p_Info-> DeviceAsString ()); p_Dlg-> m_ServerList.DeleteAllItems (); p_Dlg-> m_ServerList.DeleteColumn (0) ; p_Dlg-> m_ServerList.InsertColumn (0, "Service", LVCFMT_CENTER, 180); if (p_Dlg-> m_bDialogClosed!) {CSdpDiscoveryRec sdp_Record [MAX_SERVICE]; int nServiceNumber = 0; nServiceNumber = p_Dlg-> ReadDiscoveryRecords (p_Info-> m_BdAddr , Max_service, sdp_record; // read specific service content

转载请注明原文地址:https://www.9cbs.com/read-76922.html

New Post(0)