int GetBluetoothStruct(BLUETOOTH_DEVICE_INFO* pbtdi) { pbtdi->dwSize = sizeof(BLUETOOTH_DEVICE_INFO); BLUETOOTH_DEVICE_SEARCH_PARAMS bdsp; bdsp.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS); bdsp.fReturnAuthenticated = false; bdsp.fReturnRemembered = false; bdsp.fReturnUnknown = true; bdsp.fReturnConnected = false; bdsp.cTimeoutMultiplier = 5; //8 second timeout bdsp.hRadio = NULL; //all bluetooth radios //BLUETOOTH_DEVICE_INFO pbtdi; HANDLE btHandle = BluetoothFindFirstDevice( &bdsp, pbtdi); int result = -1; //handle errors if(btHandle == NULL) { int error = GetLastError(); if(error == ERROR_NO_MORE_ITEMS) result = R_NO_DEVICE_FOUND; result = error; } while(result < 0) { //check if we found the right device if(IsMuscleMateDevice(pbtdi)) { result = R_SUCCESS; } //look for the next device else if(!BluetoothFindNextDevice(btHandle, pbtdi)) { //error(s), deal with it int error = GetLastError(); if(error == ERROR_NO_MORE_ITEMS) result = R_NO_DEVICE_FOUND; else result = error; } } //free up the list BluetoothFindDeviceClose(btHandle); return result; }
static bool get_bluetooth_device_info( const HANDLE hRadio, const BLUETOOTH_ADDRESS *addr, BLUETOOTH_DEVICE_INFO *device_info, BOOL inquire) { bool foundDeviceInfo= false; assert(addr != nullptr); assert(device_info != nullptr); BLUETOOTH_DEVICE_SEARCH_PARAMS search_params; search_params.dwSize = sizeof(search_params); search_params.cTimeoutMultiplier = GET_BT_DEVICES_TIMEOUT_MULTIPLIER; search_params.fIssueInquiry = inquire; search_params.fReturnAuthenticated = TRUE; search_params.fReturnConnected = TRUE; search_params.fReturnRemembered = TRUE; search_params.fReturnUnknown = TRUE; search_params.hRadio = hRadio; device_info->dwSize = sizeof(*device_info); HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&search_params, device_info); if (hFind) { do { // check if the device's Bluetooth address matches the one we are looking for if (device_info->Address.ullLong == addr->ullLong) { foundDeviceInfo= true; break; } } while(BluetoothFindNextDevice(hFind, device_info)); if (!BluetoothFindDeviceClose(hFind)) { SERVER_MT_LOG_ERROR("AsyncBluetoothPairDeviceRequest") << "Failed to close bluetooth device enumeration handle"; } } else { if (GetLastError() == ERROR_NO_MORE_ITEMS) { SERVER_MT_LOG_INFO("AsyncBluetoothPairDeviceRequest") << "No bluetooth devices connected."; } else { SERVER_MT_LOG_ERROR("AsyncBluetoothPairDeviceRequest") << "Failed to enumerate attached bluetooth devices"; } } return foundDeviceInfo; }
int _tmain(int argc, _TCHAR* argv[]) { while(true) { m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio); int m_radio_id = 0; do { m_radio_id++; BluetoothGetRadioInfo(m_radio, &m_bt_info); wprintf(L"Radio %d:\r\n", m_radio_id); wprintf(L"\tName: %s\r\n", m_bt_info.szName); wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]); wprintf(L"\tClass: 0x%08x\r\n", m_bt_info.ulClassofDevice); wprintf(L"\tManufacturer: 0x%04x\r\n", m_bt_info.manufacturer); m_search_params.hRadio = m_radio; ::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO)); m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info); int m_device_id = 0; do { m_device_id++; wprintf(L"\tDevice %d:\r\n", m_device_id); wprintf(L"\t\tName: %s\r\n", m_device_info.szName); wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]); wprintf(L"\t\tClass: 0x%08x\r\n", m_device_info.ulClassofDevice); wprintf(L"\t\tConnected: %s\r\n", m_device_info.fConnected ? L"true" : L"false"); wprintf(L"\t\tAuthenticated: %s\r\n", m_device_info.fAuthenticated ? L"true" : L"false"); wprintf(L"\t\tRemembered: %s\r\n", m_device_info.fRemembered ? L"true" : L"false"); } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info)); BluetoothFindDeviceClose(m_bt_dev); } while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio)); BluetoothFindRadioClose(m_bt); Sleep(10000); } return 0; }
static void getDevices(std::vector<al::Bluetooth>& devs){ //BLUETOOTH_ADDRESS returnAddress = { BLUETOOTH_NULL_ADDRESS }; /* typedef struct { DWORD dwSize; BOOL fReturnAuthenticated; BOOL fReturnRemembered; BOOL fReturnUnknown; BOOL fReturnConnected; BOOL fIssueInquiry; UCHAR cTimeoutMultiplier; // in units of 1.28 seconds HANDLE hRadio; // NULL == search all local radios } BLUETOOTH_DEVICE_SEARCH_PARAMS; */ BLUETOOTH_DEVICE_SEARCH_PARAMS params = { sizeof(params), //TRUE, FALSE, FALSE, TRUE, FALSE, 0, NULL TRUE, FALSE, TRUE, TRUE, TRUE, 5, NULL }; BLUETOOTH_DEVICE_INFO info = { sizeof(info) }; HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(¶ms, &info); if(NULL != hFind){ char addr[18]; char name[256]; Bluetooth bt; do{ //printf("%x%x\n", long(info.Address.ullLong>>32), long(info.Address.ullLong)); btaddr2str(addr, info.Address.rgBytes); wcstombs(name, info.szName, sizeof(name)); //printf("Found %s (%s)\n", addr, name); //wprintf(L"%s)\n", info.szName); bt.mName = name; bt.mAddr = addr; bt.mClass= info.ulClassofDevice; devs.push_back(bt); } while(BluetoothFindNextDevice(hFind, &info)); } BluetoothFindDeviceClose(hFind); }
static int get_bluetooth_device_info(const HANDLE hRadio, const BLUETOOTH_ADDRESS *addr, BLUETOOTH_DEVICE_INFO *device_info) { if (!addr || !device_info) { return 1; } BLUETOOTH_DEVICE_SEARCH_PARAMS search_params; search_params.dwSize = sizeof(search_params); search_params.cTimeoutMultiplier = 4; search_params.fIssueInquiry = FALSE; search_params.fReturnAuthenticated = TRUE; search_params.fReturnConnected = TRUE; search_params.fReturnRemembered = TRUE; search_params.fReturnUnknown = TRUE; search_params.hRadio = hRadio; device_info->dwSize = sizeof(*device_info); HBLUETOOTH_DEVICE_FIND hFind = BluetoothFindFirstDevice(&search_params, device_info); if (!hFind) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { WINPAIR_DEBUG("Failed to enumerate devices"); return 1; } } else { do { /* check if the device's Bluetooth address matches the one we are looking for */ if (device_info->Address.ullLong == addr->ullLong) { break; } } while(BluetoothFindNextDevice(hFind, device_info)); if (!BluetoothFindDeviceClose(hFind)) { WINPAIR_DEBUG("Failed to close device enumeration handle"); return 1; } } return 0; }
int main(){ BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)}; BLUETOOTH_RADIO_INFO m_bt_info = {sizeof(BLUETOOTH_RADIO_INFO),0,}; BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),1,1,1,1,1,5,NULL }; BLUETOOTH_DEVICE_INFO m_device_info = {sizeof(BLUETOOTH_DEVICE_INFO),0,}; HBLUETOOTH_DEVICE_FIND m_bt_dev = BluetoothFindFirstDevice(&m_search_params , &m_device_info); if (m_bt_dev == NULL) { char errstring[10] = {0}; sprintf(errstring,"!%d",GetLastError()); errstring[0] = 0x94; printf("%s",errstring); return -1; } UCHAR address[225]; do { char name[225] = {0}; wcstombs((char*)name, m_device_info.szName, wcslen(m_device_info.szName)); sprintf(address,"%02X:%02X:%02X:%02X:%02X:%02X",m_device_info.Address.rgBytes[5],m_device_info.Address.rgBytes[4],m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[2],m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[0]); printf("%s,%s\n",address,name); } while (BluetoothFindNextDevice(m_bt_dev , &m_device_info)); return 0; }
void *CVSCPBTDetectWrkTread::Entry() { BLUETOOTH_FIND_RADIO_PARAMS bt_find_radio = { sizeof( BLUETOOTH_FIND_RADIO_PARAMS ) }; BLUETOOTH_RADIO_INFO bt_info = { sizeof( BLUETOOTH_RADIO_INFO ), 0, }; BLUETOOTH_DEVICE_SEARCH_PARAMS search_params = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS), 1, 0, 1, 1, 1, 1, // Timeout NULL }; BLUETOOTH_DEVICE_INFO device_info = { sizeof( BLUETOOTH_DEVICE_INFO ), 0, }; HANDLE radio = NULL; HBLUETOOTH_RADIO_FIND bt = NULL; HBLUETOOTH_DEVICE_FIND bt_dev = NULL; // First log on to the host and get configuration // variables if ( VSCP_ERROR_SUCCESS != m_srv.doCmdOpen( m_pObj->m_host, m_pObj->m_username, m_pObj->m_password ) ) { return NULL; } // Find the channel id uint32_t ChannelID; m_srv.doCmdGetChannelID( &ChannelID ); // It is possible that there is configuration data the server holds // that we need to read in. // We look for // prefix_period Seconds between scans. // prefix_zone Zone to use for Token Activity // prefix_subzone Subzone to use for Token Activity // prefix_bsendtoken Send Token activity. Class=20 (Information, Type=37 // prefix_bsenddetect Send detect event. Class=20 (Information), Type=49 // prefix_detectzone // prefix_detectsubzone // // Future defines. // =============== // prefix_onaddresses - List with addresses that will generate on-event // prefix_offaddresses - List with addresses that will generate off-event bool bSendTokenActivity = true; // Sending Token Activity is default behaviour. bool bSendDetect = false; // Sending detect events must be enabled. uint16_t pausTime = 1; // default svan period is one seconds. uint8_t zone = 0; // Zone for token activity. uint8_t subzone = 0; // Subzone for token activity. uint8_t detectIndex = 0; // Index used for detect event. uint8_t detectZone = 0; // Zone used for detect event. uint8_t detectSubzone = 0; // Subzone used for detect events. bool bDisableRadiodetect= false; // Don't report radio discovery. wxString strAddress; _deviceHash deviceHashPrev; _deviceHash deviceHashNow; CDetectedDevice *pDevice; int intvalue; bool bvalue; if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_pausetime" ), &intvalue ) ) { if ( intvalue >= 0 ) pausTime = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_zone" ), &intvalue ) ) { zone = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_subzone" ), &intvalue ) ) { subzone = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_detectindex" ), &intvalue ) ) { detectIndex = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_detectzone" ), &intvalue ) ) { detectZone = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableInt( m_pObj->m_prefix + _( "_detectsubzone" ), &intvalue ) ) { detectSubzone = intvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableBool( m_pObj->m_prefix + _( "_send_token_activity" ), &bvalue ) ) { bSendTokenActivity = bvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableBool( m_pObj->m_prefix + _( "_send_detect" ), &bvalue ) ) { bSendDetect = bvalue; } if ( VSCP_ERROR_SUCCESS == m_srv.getVariableBool( m_pObj->m_prefix + _( "_disable_radio_detect" ), &bvalue ) ) { bDisableRadiodetect = bvalue; } // Close the channel m_srv.doCmdClose(); while ( !TestDestroy() && !m_pObj->m_bQuit ) { bt = BluetoothFindFirstRadio( &bt_find_radio, &radio ); int radio_id = 0; do { radio_id++; if ( ERROR_SUCCESS == BluetoothGetRadioInfo( radio, &bt_info ) ) { strAddress.Printf( _("%02x:%02x:%02x:%02x:%02x:%02x"), bt_info.address.rgBytes[ 0 ], bt_info.address.rgBytes[ 1 ], bt_info.address.rgBytes[ 2 ], bt_info.address.rgBytes[ 3 ], bt_info.address.rgBytes[ 4 ], bt_info.address.rgBytes[ 5 ] ); if ( deviceHashPrev.count( strAddress ) && ( NULL != ( pDevice = deviceHashPrev[ strAddress ] ) ) ) { // Address is known and in the table pDevice->m_bfound = true; } else { if ( !bDisableRadiodetect ) { // Add the new device pDevice = new CDetectedDevice(); if ( NULL != pDevice ) { pDevice->m_radio_id = radio_id; pDevice->m_device_id = 0; memcpy( pDevice->m_address, bt_info.address.rgBytes, 6 ); pDevice->m_name = bt_info.szName; pDevice->m_class = bt_info.ulClassofDevice; pDevice->m_manufacturer = bt_info.manufacturer; deviceHashNow[ strAddress ] = pDevice; } } } search_params.hRadio = radio; ::ZeroMemory( &device_info, sizeof( BLUETOOTH_DEVICE_INFO ) ); device_info.dwSize = sizeof( BLUETOOTH_DEVICE_INFO ); if ( NULL != ( bt_dev = BluetoothFindFirstDevice( &search_params, &device_info ) ) ) { int device_id = 0; do { device_id++; strAddress.Printf( _("%02x:%02x:%02x:%02x:%02x:%02x"), device_info.Address.rgBytes[ 0 ], device_info.Address.rgBytes[ 1 ], device_info.Address.rgBytes[ 2 ], device_info.Address.rgBytes[ 3 ], device_info.Address.rgBytes[ 4 ], device_info.Address.rgBytes[ 5 ]); if ( deviceHashPrev.count( strAddress ) && ( NULL != ( pDevice = deviceHashPrev[ strAddress ] ) ) ) { // Address is known and in the table pDevice->m_bfound = true; } else { // Add the new device pDevice = new CDetectedDevice(); if ( NULL != pDevice ) { pDevice->m_radio_id = radio_id; pDevice->m_device_id = device_id; memcpy( pDevice->m_address, device_info.Address.rgBytes, 6 ); pDevice->m_name = device_info.szName; pDevice->m_class = device_info.ulClassofDevice; memcpy( &pDevice->m_lastSeen, &device_info.stLastSeen, sizeof( SYSTEMTIME ) ); deviceHashNow[ strAddress ] = pDevice; } } } while ( BluetoothFindNextDevice( bt_dev, &device_info ) ); BluetoothFindDeviceClose( bt_dev ); } } } while ( BluetoothFindNextRadio( &bt_find_radio, &radio ) ); BluetoothFindRadioClose( bt ); // Go through devices that have been lost since last run _deviceHash::iterator itPrev; for ( itPrev = deviceHashPrev.begin(); itPrev != deviceHashPrev.end(); ++itPrev ) { wxString key = itPrev->first; CDetectedDevice *pDev = itPrev->second; if ( NULL == pDev ) { continue; } if ( false == pDev->m_bfound ) { if ( bSendTokenActivity ) { vscpEventEx evx; memset( evx.GUID, 0, 16 ); // Use interface GUID evx.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; evx.vscp_type = VSCP_TYPE_INFORMATION_TOKEN_ACTIVITY; evx.timestamp = wxDateTime::Now().GetTicks(); evx.head = VSCP_PRIORITY_NORMAL; evx.sizeData = 8; evx.data[ 0 ] = ( 18 << 2 ) + 2; // Bluetooth device + "Released" evx.data[ 1 ] = zone; evx.data[ 2 ] = subzone; evx.data[ 3 ] = 0; // Frame 0 evx.data[ 4 ] = pDev->m_address[ 0 ]; evx.data[ 5 ] = pDev->m_address[ 1 ]; evx.data[ 6 ] = pDev->m_address[ 2 ]; evx.data[ 7 ] = pDev->m_address[ 3 ]; sendEvent( evx ); // Send the event memset( evx.GUID, 0, 16 ); // Use interface GUID evx.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; evx.vscp_type = VSCP_TYPE_INFORMATION_TOKEN_ACTIVITY; evx.timestamp = wxDateTime::Now().GetMillisecond(); evx.head = VSCP_PRIORITY_NORMAL; evx.sizeData = 6; evx.data[ 0 ] = ( 18 << 2 ) + 2; // Bluetooth device + "Released" evx.data[ 1 ] = zone; evx.data[ 2 ] = subzone; evx.data[ 3 ] = 1; // Frame 1 evx.data[ 4 ] = pDev->m_address[ 4 ]; evx.data[ 5 ] = pDev->m_address[ 5 ]; sendEvent( evx ); } // Remove it delete pDev; deviceHashPrev[ key ] = NULL; } else { // Prepare for next detection loop pDev->m_bfound = false; } } // Go through devices that have been found since last run _deviceHash::iterator itNow; for ( itNow = deviceHashNow.begin(); itNow != deviceHashNow.end(); ++itNow ) { wxString key = itNow->first; CDetectedDevice *pDev = itNow->second; if ( NULL == pDev ) { continue; } // Save it in previously found devices deviceHashPrev[ key ] = pDev; deviceHashNow[ key ] = NULL; if ( bSendTokenActivity ) { // Tell the world we found it vscpEventEx evx; memset( evx.GUID, 0, 16 ); // Use interface GUID evx.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; evx.vscp_type = VSCP_TYPE_INFORMATION_TOKEN_ACTIVITY; evx.timestamp = wxDateTime::Now().GetMillisecond(); evx.head = VSCP_PRIORITY_NORMAL; evx.sizeData = 8; evx.data[ 0 ] = ( 18 << 2 ) + 1; // Bluetooth device + "Touched" evx.data[ 1 ] = zone; evx.data[ 2 ] = subzone; evx.data[ 3 ] = 0; // Frame 0 evx.data[ 4 ] = pDev->m_address[ 0 ]; evx.data[ 5 ] = pDev->m_address[ 1 ]; evx.data[ 6 ] = pDev->m_address[ 2 ]; evx.data[ 7 ] = pDev->m_address[ 3 ]; sendEvent( evx ); // Send the event memset( evx.GUID, 0, 16 ); // Use interface GUID evx.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; evx.vscp_type = VSCP_TYPE_INFORMATION_TOKEN_ACTIVITY; evx.timestamp = wxDateTime::Now().GetMillisecond(); evx.head = VSCP_PRIORITY_NORMAL; evx.sizeData = 6; evx.data[ 0 ] = ( 18 << 2 ) + 1; // Bluetooth device + "Touched" evx.data[ 1 ] = zone; evx.data[ 2 ] = subzone; evx.data[ 3 ] = 1; // Frame 1 evx.data[ 4 ] = pDev->m_address[ 4 ]; evx.data[ 5 ] = pDev->m_address[ 5 ]; sendEvent( evx ); // Send the event } if ( bSendDetect ) { // Tell the world we detected it vscpEventEx evx; memset( evx.GUID, 0, 16 ); // Use interface GUID evx.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; evx.vscp_type = VSCP_TYPE_INFORMATION_DETECT; evx.timestamp = wxDateTime::Now().GetMillisecond(); evx.head = VSCP_PRIORITY_NORMAL; evx.sizeData = 3; evx.data[ 0 ] = detectIndex; evx.data[ 1 ] = detectZone; evx.data[ 2 ] = detectSubzone; sendEvent( evx ); // Send the event } } wxSleep( pausTime ); } // detect loop return NULL; }
CString CDlgDialbox::EnumComPort() { CString sRtn = ""; //161229 list bluetooth com port BLUETOOTH_FIND_RADIO_PARAMS btFindRadioParams = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)}; BLUETOOTH_RADIO_INFO btRadioInfo = {sizeof(BLUETOOTH_RADIO_INFO), 0}; BLUETOOTH_DEVICE_SEARCH_PARAMS btDeviceSearchParams = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS), TRUE, //BOOL fReturnAuthenticated; TRUE, //BOOL fReturnRemembered; TRUE, //BOOL fReturnUnknown; TRUE, //BOOL fReturnConnected; FALSE, //BOOL fIssueInquiry; 10, //UCHAR cTimeoutMultiplier; NULL //HANDLE hRadio; }; BLUETOOTH_DEVICE_INFO btDeviceInfo = {sizeof(BLUETOOTH_DEVICE_INFO), 0}; HANDLE hRadio = NULL; HBLUETOOTH_DEVICE_FIND hbtDeviceFind = NULL; HBLUETOOTH_RADIO_FIND hbtRadioFind = BluetoothFindFirstRadio(&btFindRadioParams, &hRadio); if (hbtRadioFind) { do { if (BluetoothGetRadioInfo(hRadio, &btRadioInfo) == ERROR_SUCCESS) { btDeviceSearchParams.hRadio = hRadio; ZeroMemory(&btDeviceInfo, sizeof(BLUETOOTH_DEVICE_INFO)); btDeviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); hbtDeviceFind = BluetoothFindFirstDevice(&btDeviceSearchParams, &btDeviceInfo); if (hbtDeviceFind) { do { CString sDeviceName(btDeviceInfo.szName); CString sAddr; sAddr.Format("%02x%02x%02x%02x%02x%02x", btDeviceInfo.Address.rgBytes[5],btDeviceInfo.Address.rgBytes[4], btDeviceInfo.Address.rgBytes[3], btDeviceInfo.Address.rgBytes[2], btDeviceInfo.Address.rgBytes[1], btDeviceInfo.Address.rgBytes[0]); sAddr.MakeUpper(); CString sComPort = GetBthComPort(sAddr); if (!sComPort.IsEmpty() && (sDeviceName.Find(BLUETOOTH_DEVICENAME_KEY) >= 0)) { sRtn += sComPort + "\r" + sDeviceName + "\r"; } } while (BluetoothFindNextDevice(hbtDeviceFind, &btDeviceInfo)); } BluetoothFindDeviceClose(hbtDeviceFind); } CloseHandle(hRadio); } while (BluetoothFindNextRadio(hbtRadioFind, &hRadio)); BluetoothFindRadioClose(hbtRadioFind); } //list other serial ports HKEY hKey = NULL; TCHAR tcName[256]; BYTE cData[256]; DWORD dwNameBuffSize = sizeof(tcName); DWORD dwDataBuffSize = sizeof(cData); if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM" ), NULL, KEY_READ, &hKey)) { int i = 0; while (RegEnumValue(hKey, i, tcName, &dwNameBuffSize, NULL, NULL, cData, &dwDataBuffSize) == ERROR_SUCCESS) { if (!strstr(tcName, "BthModem")) { CString sPortName; sPortName.Format("%s\r%s\r", (char*)cData, tcName); sRtn += sPortName; } dwNameBuffSize = sizeof(tcName); dwDataBuffSize = sizeof(cData); i++; } } return sRtn; }
// Scan local network for visible remote devices static VALUE bt_devices_scan(VALUE self) { WORD wVersionRequested = 0x202; HANDLE hRadio; BLUETOOTH_FIND_RADIO_PARAMS btfrp = { sizeof(btfrp) }; HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio( &btfrp, &hRadio ); VALUE devices_array = rb_ary_new(); if ( NULL != hFind ) { do { BLUETOOTH_DEVICE_INFO_STRUCT deviceInfo; deviceInfo.dwSize = sizeof(deviceInfo); BLUETOOTH_DEVICE_SEARCH_PARAMS deviceSearchParams; memset(&deviceSearchParams, 0, sizeof(deviceSearchParams)); deviceSearchParams.dwSize = sizeof(deviceSearchParams); deviceSearchParams.fReturnAuthenticated = true; deviceSearchParams.fReturnRemembered = false; deviceSearchParams.fReturnUnknown = true; deviceSearchParams.fReturnConnected = true; deviceSearchParams.fIssueInquiry = true; deviceSearchParams.cTimeoutMultiplier = 1; deviceSearchParams.hRadio = hRadio; HANDLE hDeviceFind = BluetoothFindFirstDevice(&deviceSearchParams, &deviceInfo); if (NULL != hDeviceFind) { do { BYTE *rgBytes = deviceInfo.Address.rgBytes; //BluetoothDisplayDeviceProperties(0, &deviceInfo); char addr[19] = { 0 }; char name[248] = { 0 }; wcstombs(name, deviceInfo.szName, sizeof(name)); snprintf(addr, sizeof(addr), "%02x:%02x:%02x:%02x:%02x:%02x", rgBytes[5], rgBytes[4], rgBytes[3], rgBytes[2], rgBytes[1], rgBytes[0]); VALUE bt_dev = bt_device_new(bt_device_class, rb_str_new2(name), rb_str_new2(addr)); rb_ary_push(devices_array, bt_dev); } while(BluetoothFindNextDevice(hDeviceFind, &deviceInfo)); BluetoothFindDeviceClose(hDeviceFind); } GUID guidServices[10]; DWORD numServices = sizeof(guidServices); DWORD result = BluetoothEnumerateInstalledServices(hRadio, &deviceInfo, &numServices, guidServices); CloseHandle( hRadio ); } while( BluetoothFindNextRadio( hFind, &hRadio ) ); BluetoothFindRadioClose( hFind ); } return devices_array; }
void BtPCModul ::Find_Device(int time) { //m_device_id=0; BLUETOOTH_DEVICE_INFO m_device_info= {sizeof(BLUETOOTH_DEVICE_INFO),0,}; ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO)); m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = { sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS), 1, 1, 1, 1, 1, time, NULL }; m_search_params.hRadio = m_radio; HBLUETOOTH_DEVICE_FIND m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info); printf("Start searching..."); if(m_bt_dev != NULL) printf("\nBluetoothFindFirstDevice() is working!\n"); else { printf("\nBluetoothFindFirstDevice() failed with error code %d\n", GetLastError()); return; } do { //SaveAddr[m_device_id]=m_device_info.Address; // Well, the found device information can be used for further socket // operation such as creating a socket, bind, listen, connect, send, receive etc.. // If no more device, exit the loop //if(!BluetoothFindNextDevice(m_bt_dev, &m_device_info)) char newaddr[18]; ba2str(m_device_info.Address,newaddr); bool bnew=true; for (int i=0; i<m_device_id; i++) { if (!memcmp(newaddr,BTaddr[i],18)) { bnew=false; } } if (bnew) { wprintf(L"\n\tDevice %d:\r\n", m_device_id); wprintf(L" \tInstance Name: %s\r\n", m_device_info.szName); wprintf(L" \tAddress: %02X:%02X:%02X:%02X:%02X:%02X\r\n", m_device_info.Address.rgBytes[5],m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[2],m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[0]); wprintf(L" \tClass: 0x%08x\r\n", m_device_info.ulClassofDevice); wprintf(L" \tConnected: %s\r\n", m_device_info.fConnected ? L"true" : L"false"); wprintf(L" \tAuthenticated: %s\r\n", m_device_info.fAuthenticated ? L"true" : L"false"); wprintf(L" \tRemembered: %s\r\n", m_device_info.fRemembered ? L"true" : L"false"); memcpy(BTaddr[m_device_id],newaddr,18); //ba2str(m_device_info.Address,BTaddr[m_device_id]); for(int j=0; j<BLUETOOTH_MAX_SIZE_NAME; j++) SaveName[m_device_id][j]=m_device_info.szName[j]; m_device_id++; emit NewDevice(); } } while((BluetoothFindNextDevice(m_bt_dev, &m_device_info))); if(BluetoothFindDeviceClose(m_bt_dev)) printf("\nBluetoothFindDeviceClose(m_bt_dev) is OK!\n"); else printf("\nBluetoothFindDeviceClose(m_bt_dev) failed with error code %d\n", GetLastError()); }