找了很久的低功耗蓝牙程序开发,终于在国外找到了部分资料
2017-06-29 02:11
1086 查看
#include <stdio.h> #include <windows.h> #include <setupapi.h> #include <devguid.h> #include <regstr.h> #include <bthdef.h> #include <Bluetoothleapis.h> #pragma comment(lib, "SetupAPI") #pragma comment(lib, "BluetoothApis.lib") #define TO_SEARCH_DEVICE_UUID "{0000180d-0000-1000-8000-00805f9b34fb}" HANDLE GetBLEHandle(__in GUID svcGuid) { HDEVINFO hDevInfo; SP_DEVICE_INTERFACE_DATA devIntfData; SP_DEVINFO_DATA devInfoData; GUID deviceGUID = svcGuid; HANDLE hFind = NULL; hDevInfo = SetupDiGetClassDevs(&deviceGUID, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); if (hDevInfo == INVALID_HANDLE_VALUE) return NULL; devIntfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &deviceGUID, i, &devIntfData); i++) { SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData; DeviceInterfaceDetailData.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); DWORD size = 0; if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &devIntfData, NULL, 0, &size, 0)) { int err = GetLastError(); if (err == ERROR_NO_MORE_ITEMS) break; PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)GlobalAlloc(GPTR, size); pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &devIntfData, pInterfaceDetailData, size, &size, &devInfoData)) break; hFind = CreateFile( pInterfaceDetailData->DevicePath, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); GlobalFree(pInterfaceDetailData); } } SetupDiDestroyDeviceInfoList(hDevInfo); return hFind; } int main() { GUID svcGuid; CLSIDFromString(TEXT(TO_SEARCH_DEVICE_UUID), &svcGuid); HANDLE hLEDevice = GetBLEHandle(svcGuid); printf("Find Handle: 0x%x \r\n", hLEDevice); //------------------------------------------- USHORT serviceBufferCount; HRESULT hr = BluetoothGATTGetServices( hLEDevice, 0, NULL, &serviceBufferCount, BLUETOOTH_GATT_FLAG_NONE); if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) { printf("BluetoothGATTGetServices - Need Alloc Buffer Size: %d \r\n", serviceBufferCount); } PBTH_LE_GATT_SERVICE pServiceBuffer = (PBTH_LE_GATT_SERVICE) malloc(sizeof(BTH_LE_GATT_SERVICE) * serviceBufferCount); if (pServiceBuffer == NULL) { printf("pServiceBuffer out of memory\r\n"); } else { RtlZeroMemory(pServiceBuffer, sizeof(BTH_LE_GATT_SERVICE) * serviceBufferCount); } USHORT numServices; hr = BluetoothGATTGetServices( hLEDevice, serviceBufferCount, pServiceBuffer, &numServices, BLUETOOTH_GATT_FLAG_NONE); if (hr == S_OK) { printf("BluetoothGATTGetServices - Service Count %d \r\n", numServices); } //------------------------------------------- USHORT charBufferSize; hr = BluetoothGATTGetCharacteristics( hLEDevice, pServiceBuffer, 0, NULL, &charBufferSize, BLUETOOTH_GATT_FLAG_NONE); if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) == hr) { printf("BluetoothGATTGetCharacteristics - Buffer Size %d \r\n", charBufferSize); } PBTH_LE_GATT_CHARACTERISTIC pCharBuffer; if (charBufferSize > 0) { pCharBuffer = (PBTH_LE_GATT_CHARACTERISTIC) malloc(charBufferSize * sizeof(BTH_LE_GATT_CHARACTERISTIC)); if (pCharBuffer == NULL) { printf("pCharBuffer out of memory\r\n"); } else { RtlZeroMemory(pCharBuffer, charBufferSize * sizeof(BTH_LE_GATT_CHARACTERISTIC)); } USHORT numChars; hr = BluetoothGATTGetCharacteristics( hLEDevice, pServiceBuffer, charBufferSize, pCharBuffer, &numChars, BLUETOOTH_GATT_FLAG_NONE); if (hr == S_OK) { printf("BluetoothGATTGetCharacteristics - Actual Data %d \r\n", numChars); } if (numChars != charBufferSize) { printf("buffer size and buffer size actual size mismatch\r\n"); } } //------------------------------------------- //------------------------------------------- system("pause"); return 0; }
相关文章推荐
- 最近帮公司开发一个邮件营销软件 用到XPTable 但找了很多方法 摸索了很久都不知道如何更新进度条 不过在国外的一个网站 终于找到答案了
- 低功耗蓝牙开发权威指南第二部分-控制器
- 低功耗蓝牙开发权威指南第四部分-应用
- 低功耗蓝牙开发权威指南第三部分-主机
- Android蓝牙开发(二) BLE4.0低功耗蓝牙
- iOS 蓝牙开发BLE4.0的资料收集,基于原生Core Bluetooth
- Windows Phone(一) 正式开发之前的准备资料(主要注册开发者账号,手机解锁,激活码,程序部署)
- iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- Bluetooth LE(低功耗蓝牙) - 第一部分
- Bluetooth LE(低功耗蓝牙) - 第六部分(完)
- Bluetooth LE(低功耗蓝牙) - 第二部分
- 【WinCE】WinCE程序开发中出现“无法找到PInvoke DLL"Kernel32.dll"”问题的一种解决方法
- 纠结了很久,不知道为什么除了什么原因,用任何方查都查不到根源,后来ArrayList存放了Bitmap,结果出错了,终于找到了事情的根源,一阵无语,不能存放竟静态的,为什么不提示呢, 害我以为是那些自
- 软件工程资料 - 优秀的大学怎么教程序开发和软件工程课
- 史上最强Android 的低功耗蓝牙BLE开发实践
- ArcGIS程序开发资料收集
- iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- J2ME蓝牙程序开发实战入门
- android开发之动画的详解 整理资料 Android开发程序小冰整理