Initiating a Pairing Relationship Using the Bluetooth Pairing Service

Windows CE 5.0 Not SupportedWindows Embedded NavReady 2009 Supported

10/16/2008

This topic describes how Windows Embedded CE initiates the Bluetooth pairing procedure with a Bluetooth–enabled device.

Discovering and Pairing with a Bluetooth Device

While in Discovery Mode, the Windows Embedded CE–based device performs an inquiry scan and looks for discoverable devices in the local area. If the user wants Windows Embedded CE to find its Bluetooth–enabled device, the user must put that device in Discoverable Mode using the device's UI.

Prior to starting the pairing procedure, Windows Embedded CE must create two pairing event objects so that it may be notified when the pairing procedure succeeds (or fails) — a 'PairedEvent' event object and a 'PairFailedEvent' event object. Windows Embedded CE should name each event using the strings specified in the Bluetooth Pairing API Events topic.

After this work is completed, Windows Embedded CE can start the pairing procedure with a call to StartBTPair, passing in the Bluetooth device address of the Bluetooth–enabled device acquired during the device discovery procedure, and the device's PIN code acquired from the user. **

**The following sample code shows how Windows Embedded CE initiates the Bluetooth pairing procedure:

Note

PIN generation in this sample is done manually for simplicity.

    BYTE arPIN[16] = { '1','2','3','4' };
    DWORD dwPINSize = 4;

    if ( argc > 1 && argv[1] )
    {
        dwPINSize = WideCharToMultiByte ( CP_ACP, NULL, argv[1], wcslen(argv[1]), (LPSTR)arPIN, sizeof ( arPIN ), NULL, NULL );
    }

    HANDLE hPairedEvent = CreateEvent ( NULL, TRUE, FALSE, BTPAIRAPI_NEWDEVICE_PAIRING_EVENT );
    HANDLE hPairFailedEvent = CreateEvent ( NULL, TRUE, FALSE, BTPAIRAPI_FAILED_PAIR_ATTEMPT_EVENT );
    HANDLE arEvents [2];

     arEvents[0] = hPairedEvent;
     arEvents[1] = hPairFailedEvent;

    dwResult = StartBTPair ( btPhoneDevice, arPIN, dwPINSize );

    RETAILMSG ( 1, ( TEXT ( "BTPairSvcSample: StartBTPair return code %08x\r\n" ), dwResult ));

    if ( dwResult != ERROR_SUCCESS )
        goto Cleanup;


     //
     // Finally, wait for either postive or negative confirmation of the pairing
     //
     BOOL fEventReceived = FALSE;
     for ( nLoopCount = 0; !fEventReceived && nLoopCount < 10; ++nLoopCount )
       {
          WORD dwWait = WaitForMultipleObjects ( 2, arEvents, FALSE, 1000 * 60 );

          switch (dwWait)
          {
          case WAIT_OBJECT_0:
               RETAILMSG ( 1, ( TEXT ( "BTPairSvcSample: Pairing succeeded\r\n" ), dwResult ));
              fEventReceived = TRUE;
               break;
          case WAIT_OBJECT_0 + 1:
               RETAILMSG ( 1, ( TEXT ( "BTPairSvcSample: Pairing failed\r\n" ), dwResult ));
               fEventReceived = TRUE;
               break;
          default:
               // must be WAIT_TIMEOUT
               RETAILMSG ( 1, ( TEXT ( "BTPairSvcSample: Waiting for pairing event has timed out\r\n" ), dwResult ));
               break;
          }

     }

Cleanup:

    if ( hPairedEvent )
           CloseHandle ( hPairedEvent );

    if ( hPairFailedEvent )
           CloseHandle ( hPairFailedEvent );

See Also

Concepts

Bluetooth Pairing Procedure
Discovering Nearby Devices Using the Bluetooth Pairing Service
Bluetooth Pairing Procedure