Partager via


Querying and Pairing Sample (Windows CE 5.0)

Send Feedback

The Btpair sample application performs querying, pairing, and authentication to illustrate the use of the Windows CE–based Bluetooth stack and security subsystem.

The compiled sample generates a Btpair.exe application. It relies on the BTHLINK sample device driver that needs to be compiled and copied to the device.

Usage

btpair.exe

When executed, the application displays a dialog box that enables querying, pairing and authentication with other devices.

Querying

The query process is used to discover other Bluetooth devices in the area. This process is initiated by the BthNsLookupServiceBegin function.

int iErr = BthNsLookupServiceBegin (&wsaq, LUP_CONTAINERS, &hLookup);

If the function completes successfully, the discovered devices can be queried to determine which services they offer. The services are learned by calling the BthNsLookupServiceNext function.

While (iErr== ERROR_SUCCESS)
  {
    ...
    iErr = BthNsLookupServiceNext (
                     hLookup,
                     LUP_RETURN_ADDR,
                     &dwSize,
                     pwsaResults
                    );
    ...
    if (iErr == ERROR_SUCCESS) {
      /* store device information */
      }
  }

Pairing

The pairing process generates a common link key between two devices. The link key is generated from a PIN, the Bluetooth address, and a random number. To set the PIN up on a device, an IOCTL call is provided as BT_IOCTL_BthSetPIN.

BthSetPIN (&b, cPin, pin);

A common link is established between two devices to be paired. This example demonstrates how to use the BTHLINK protocol layer extension to make a connection at the lower protocol layer level.

The following example code loads the protocol layer extension DLL.

HANDLE hDev = RegisterDevice (L"BTL", 1, L"bthlink.dll", NULL);
HANDLE hFile = CreateFile (
                 L"BTL1:", 
                 0, 0, NULL,
                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
                );

Authentication

If the link key generates successfully, authentication can be performed. This process is initiated by calling BT_IOCTL_BthAuthenticate, which is provided by an IOCTL call.

iRes = BthAuthenticate (&b);

If the authentication is successful, the extension layer can be destroyed. The following example code demonstrates how to do this.

DeviceIoControl (
             hFile, BTHLINK_IOCTL_DISCONNECT,
             &p, sizeof(p), NULL, 0, NULL, NULL
            );
DeregisterDevice (hDev);

Sample Location

%_WINCEROOT%\Public\Common\Sdk\Samples\Bluetooth\Btpair

**Note   **This sample application has not been thoroughly tested and is not intended for production use.

See Also

Bluetooth Application Development | Bluetooth Samples

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.