Setting up a Bluetooth Connection

This tutorial describes how to enable a Bluetooth connection using the AIW Service API. This API also enables applications to register for callback notifications when a phone comes in contact with another phone and gets paired successfully.

Steps

  1. Define an AIW_INTEREST resource element in the application using F as the service command and KAiwClassBase as the service class:

    RESOURCE AIW_INTEREST r_example_app_aiw_nfc_bt_interest
    {
        items =
            {
            AIW_CRITERIA_ITEM
                {
                id              = 200002;
                serviceCmd      = KAiwCmdNFCEasySetup;
                contentType     = "*";            
                serviceClass    = KAiwClassBase;
                maxProviders    = 1;
                }
            };
     }
    
  2. Register Bluetooth pairing callbacks using any of the following ways:

    • Run the KAiwCmdNFCEasySetup service command with KAiwOptASyncronous as the only parameter:

      iAiwServiceHandler = CAiwServiceHandler::NewL();
      iAiwServiceHandler->AttachL( R_EXAMPLE_APP_AIW_NFC_BT_INTEREST );
      iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup,
                                              iAiwServiceHandler->InParamList(),
                                              iAiwServiceHandler->OutParamListL(), 
                                              KAiwOptASyncronous,
                                              this );
      
    • Call the ExecuteServiceCmdL method without any optional parameter (value 0). When one phone comes in contact with another and before a time-out occurs, the ExecuteServiceCmdL method sets up a Bluetooth connection between the two phones.
      iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup,
                                              iAiwServiceHandler->InParamListL(),
                                              iAiwServiceHandler->OutParamListL(), 
                                              0,
                                              this );
      

      Note: The previous service commands must be canceled before ExecuteServiceCmdL is called.

  3. Implement the MAiwNotifyCallback::HandleNotifyL method to receive callback notifications when the two NFC enabled devices are held together and the Bluetooth information have been exchanged between the devices. To start the Bluetooth Out-Of-Band (OOB) pairing, call ExecureServiceCmdL() after the KAiwEventStarted event has been received. If the pairing (that is authenticated Bluetooth link) is not required, Reset() must be called when receiving the KAiwEventStarted event.

    TInt CExampleApplication::HandleNotifyL(
    
    TInt aCmdId,
    TInt aEventId,
    CAiwGenericParamList& /*aEventParamList*/,
    const CAiwGenericParamList& /*aInParamList*/ )
      {
      If ( aCmdId == KAiwCmdNFCEasySetup )
          {
          if ( aEventId == KAiwEventStarted )
              {
              // aEventParamList contains information about the other device
              // Start OOB pairing by calling ExecuteServiceCmdL another time.
              // If pairing is not needed, Reset should be called already here.
              iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup, 
                  iAiwServiceHandler->InParamListL(),
                  iAiwServiceHandler->OutParamListL(),
                  KAiwOptASyncronous, this );
    				 }
          else if ( aEventId == KAiwEventCompleted )
              {
              // Easy Setup completed – release touch 
              iAiwServiceHandler->Reset();
              }
          else // KAiwEventError
              {
              // In case of easy setup failure release must be done
              // For preparation failure resetting is not necessary
              iAiwServiceHandler->Reset();
              }
          }
        
      return KErrNone;
    

    In KAiwEventStarted event aEventParamList contains a buffer with information about the device that was touched. The information in the buffer may contain multiple item, the type of each defined by the first byte of the item:

    Item identifier byte

    Item description

    0x00

    RFU

    0x01

    Bluetooth information

    0x02

    KpeerDeviceNameEasyDataType: It contains the length of the device name and the name itself. For example:

    20:02:45.841 ....0x2 // KPeerDeviceNameEasyDataType 
    20:02:45.841 ....0xb // devicename length 11 
    20:02:45.841 ....0x4e // N 
    20:02:45.841 ....0x6f // o 
    20:02:45.841 ....0x6b // k 
    20:02:45.841 ....0x69 // i 
    20:02:45.841 ....0x61 // a 
    20:02:45.841 ....0x20 // space 
    20:02:45.841 ....0x43 // C 
    20:02:45.841 ....0x37 // 7 
    20:02:45.841 ....0x2d // - 
    20:02:45.841 ....0x30 // 0 
    20:02:45.841 ....0x30 // 0 
    

    Format of the Bluetooth information item is following:

    Field

    Length

    Description

    Device address

    6 bytes

    Bluetooth address of the target device

    Device class

    3 bytes

    Device class of the target device

    Device name length

    1 byte

    Length of the device name in bytes

    Device name

    n bytes

    Device name of the target device