// ... #include //... /**************************************************************** * FUNCTION: CreateDevice * DESC: Creates Bluetooth virtual COM ports for outgoing/incoming. * This method uses Microsoft's BTD library. * RFCOMM connections. * PARAMS: * nPortIndex - Unused port index * nRfChannel - RF Channel corresponding to the port to be created * (Note that a channel services a particular RF connection) * pAddr - If not NULL, client COM port is created and attempts to make * connection with this Bluetooth peer device. *****************************************************************/ BOOL CreateDevice( UINT nPortIndex, UINT nRfChannel, BT_ADDR* pAddr /* = NULL */ ) { WCHAR szErrW[256]; int rfChannel = 0; PORTEMUPortParams pp; ZeroMemory (&pp, sizeof(PORTEMUPortParams)); // Verify channel supplied is okay or not if ( nRfChannel != 0 ) { if ( (nRfChannel <= 31) && (nRfChannel >= 1) ) rfChannel = (unsigned char)nRfChannel; // Default overriden by user else { wsprintf (szErrW, L"User supplied RfChannel is invalid"); MessageBox( NULL, szErrW, L"LwVirtualCom", MB_OK ); return FALSE; } } else { rfChannel = DEFAULT_RFCOMM_CHANNEL; } if (!pAddr) { // Following, required for server port pp.flocal = TRUE; // This enables a server COM port to accept incoming connections pp.channel = rfChannel & CHANNEL_MASK; // The server channel is specified explicitly by setting channel. } else { // Following, required for client port CopyMemory(&pp.device, pAddr, sizeof(BT_ADDR)); pp.imtu = DEFAULT_MTU; pp.channel = rfChannel & CHANNEL_MASK; pp.uiportflags = 0; //RFCOMM_PORT_FLAGS_REMOTE_DCB; } // Register the Bluetooth Protocol stack with the virtual COM port hDevice = RegisterDevice (L"COM", nPortIndex, L"btd.dll", (DWORD)&pp); if (hDevice == NULL) { wsprintf (szErrW, L"RegisterDevice failed, error = %d\n", GetLastError ()); //POPMSG(szErrW); MessageBox( NULL, szErrW, L"LwVirtualCom", MB_OK ); return FALSE; } return TRUE; } VOID RemoveDevice() { if (hDevice) { // Ensure that COM port is not in service first //.. //.. DeregisterDevice(hDevice); hDevice = NULL; } }