Share via


Retrieving the RFCOMM Channel Identifier (Windows Embedded CE 6.0)

1/6/2010

The following procedure converts binary service data into ISdpRecord buffer and retrieves the RFCOMM channel identifier by using ISdpNodeContainer methods.

For information about SDP record parsing, and the implementation of the ServiceAndAttributeSearchParse function, see Parsing an SDP Record Using COM Interfaces.

WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
            #ifdef UNDER_NT
            LPSTR     lpCmdLine,
            #else
            LPWSTR    lpCmdLine,
            #endif
            int nCmdShow)
{
  ISdpRecord **pRecordArg;
  ISdpRecord *pRecord = NULL;
  ULONG ulRecords = 0;
  ULONG i,j,k;
  ULONG  ulChannelId = 0;
  BOOL   fSuccess = FALSE;
  CoInitializeEx(NULL,COINIT_MULTITHREADED);
    // Note: in real scenarios szBuf will be results from WSALookupServiceNext after
    // performing a ServiceAttribute search.
  if (ERROR_SUCCESS != ServiceAndAttributeSearchParse(szBuf,sizeof(szBuf),&pRecordArg,&ulRecords))
  goto done;
  for (i = 0; i < ulRecords; i++) 
{
  pRecord = pRecordArg[i];    // Particular record to examine in this loop.
  CNodeDataFreeString protocolList;
  // Contains SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST data, if available.
  if (ERROR_SUCCESS != pRecord->GetAttribute(SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST,&protocolList) ||
  (protocolList.type != SDP_TYPE_CONTAINER))
  {
    continue;
  }
  ISdpNodeContainer *pRecordContainer = protocolList.u.container;
  DWORD cProtocols = 0;
  NodeData protocolDescriptor; // Information about a specific protocol. (i.e. L2CAP, RFCOMM, ...)
  pRecordContainer->GetNodeCount(&cProtocols);
  for (j = 0; j < cProtocols; j++) 
  {
    pRecordContainer->GetNode(j,&protocolDescriptor);
    if (protocolDescriptor.type != SDP_TYPE_CONTAINER)
    continue;
    ISdpNodeContainer *pProtocolContainer = protocolDescriptor.u.container;
    DWORD cProtocolAtoms;
    pProtocolContainer->GetNodeCount(&cProtocolAtoms);
    for (k = 0; k < cProtocolAtoms; k++) 
    {
// Individual data element, such as what protocol this is or RFCOMM. channel id.
      NodeData nodeAtom;  
      pProtocolContainer->GetNode(k,&nodeAtom);
      if (IsRfcommUuid(&nodeAtom))  
      {
        if (k+1 == cProtocolAtoms) 
        {
// Misformatted response.  Channel ID should follow RFCOMM uuid.
          break;
        }
        NodeData channelID;
        pProtocolContainer->GetNode(k+1,&channelID);
        if (GetChannel(&channelID,&ulChannelId)) 
        {
          fSuccess = TRUE;
          goto done;
        }
        break; // Formatting error.
      }
    }
  }
}
done:
  for (i = 0; i < ulRecords; i++) 
  pRecordArg[i]->Release();
  CoTaskMemFree(pRecordArg);
  CoUninitialize();
return 0;
}

See Also

Concepts

Searching SDP Attributes Using COM Interfaces

Other Resources

Bluetooth Application Development