先有一个概念,蓝牙分为普通蓝牙和低功耗(BLE)蓝牙,
低功耗蓝牙激活时信号和普通蓝牙信号一样,低功耗时必须搜索低功耗蓝牙信号
如果使用普通代码搜索,不能搜索到低功耗蓝牙信号,
例如打开手机的蓝牙功能,进入蓝牙界面,此时蓝牙是激活状态,可以搜到蓝牙信号,锁屏后蓝牙进入低功耗模式,此时搜不到蓝牙信号,其他手机电脑又是如何搜索到低功耗蓝牙的信号呢,因为他们是增加了这个功能的
他们是即搜索普通蓝牙信号又搜索低功耗蓝牙信号,把2个搜索加起来就是看到的手机蓝牙列表和电脑上的蓝牙设备列表
搜索普通蓝牙信号
public string GetAllNameSearchBluetooth() { string strTemp, strBTName; int iBTCount = 0; strBTName = ""; BluetoothRadio radio = BluetoothRadio.PrimaryRadio; //获取自己蓝牙信息 if (radio == null) { strBTName = ""; } BluetoothClient client = new BluetoothClient(); //处理蓝牙的对象 radio.Mode = RadioMode.Connectable; //设置电脑蓝牙可被搜索到 try { BluetoothDeviceInfo[] devices = client.DiscoverDevices(); //搜索蓝牙设备,10秒 foreach (var item in devices) { iBTCount++; Console.WriteLine(item.DeviceName); strTemp = item.DeviceName + ","; strBTName += strTemp; } strBTName = iBTCount.ToString() + "," + strBTName; } catch (Exception ea) { strBTName = ""; } return strBTName; }
搜索低功耗蓝牙demo
https://www.wch.cn/downloads/WCHBleLib_MultiOS_ZIP.html
用 WCHBLEDLL.dll WCHBLEDLL.h WCHBLEDLL.lib 这个动态库查找低功耗蓝牙信号和普通蓝牙信号
int scanTimes = 1000, scannum = 20; char DevIDFilter[100] = ""; CHAR bleMac[100] = ""; PCHAR mactemp = NULL; if (!WCHBLEIsBluetoothOpened()) { pError = "请先打开系统蓝牙!"; return 1; } memset(g_BLEDev, 0, sizeof(g_BLEDev)); WCHBLEEnumDevice(scanTimes, DevIDFilter, g_BLEDev, (ULONG*)&scannum); CString strMsg; scannum;//搜索到的蓝牙个数 for (int i = 0; i < scannum; ++i) { memcpy(bleMac, g_BLEDev[i].DevID, 100); mactemp = strchr(bleMac, '-'); g_BLEDev[i].Name; strMsg.Format("%s", g_BLEDev[i].Name);//名字 }
搜索WiFi信号
HANDLE hClient = NULL; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; DWORD dwResult = 0; DWORD dwRetVal = 0; int iRet = 0; WCHAR GuidString[39] = { 0 }; unsigned int i, j, k; /* variables used for WlanEnumInterfaces */ PWLAN_INTERFACE_INFO_LIST pIfList = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; PWLAN_AVAILABLE_NETWORK_LIST pBssList = NULL; PWLAN_AVAILABLE_NETWORK pBssEntry = NULL; int iRSSI = 0; dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) { pError = "查找WiFi时发生错误1"; return 1; } dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) { pError = "查找WiFi时发生错误2"; return 1; } else { for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) { pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[i]; iRet = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString, sizeof(GuidString) / sizeof(*GuidString)); dwResult = WlanGetAvailableNetworkList(hClient, &pIfInfo->InterfaceGuid, 0, NULL, &pBssList); if (dwResult != ERROR_SUCCESS) { //wprintf(L"WlanGetAvailableNetworkList failed with error: %u\n",dwResult); dwRetVal = 1; // You can use FormatMessage to find out why the function failed pError = "查找WiFi时发生错误3"; return 1; } else { //获取Wlan的网卡接口数据 dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) { return false; } for (i = 0; i < (int)pIfList->dwNumberOfItems; i++) //遍历每个网卡 { pIfInfo = (WLAN_INTERFACE_INFO*)&pIfList->InterfaceInfo[i]; dwResult = StringFromGUID2(pIfInfo->InterfaceGuid, (LPOLESTR)&GuidString, sizeof(GuidString) / sizeof(*GuidString)); // 向无线网卡发送探测请求,WlanScan探测会在4秒内陆续更新WIFIList dwResult = WlanScan(hClient, (const GUID*)(&pIfInfo->InterfaceGuid), NULL, NULL, NULL); if (dwResult != ERROR_SUCCESS) { return false; } } Sleep(5000); CString strTemp, strMsg; pBssList->dwNumberOfItems;//个数 for (j = 0; j < pBssList->dwNumberOfItems; j++) { pBssEntry = (WLAN_AVAILABLE_NETWORK *)& pBssList->Network[j]; if (pBssEntry->dot11Ssid.uSSIDLength == 0) { //wprintf(L"\n"); } else { strMsg.Empty(); for (k = 0; k < pBssEntry->dot11Ssid.uSSIDLength; k++) { strTemp.Format(_T("%c"), pBssEntry->dot11Ssid.ucSSID[k]); strMsg += strTemp; } AfxMessageBox(strMsg);//WiFi名字 if (j >= 50) { break; } } } } } } if (pBssList != NULL) { WlanFreeMemory(pBssList); pBssList = NULL; }
if (pIfList != NULL)
{
WlanFreeMemory(pIfList);
pIfList = NULL;
}
标签:低功耗,dwResult,WiFi,蓝牙,pIfList,查找,搜索,NULL From: https://www.cnblogs.com/ckrgd/p/17012774.html