回答

2

现在,Windows只能成为GATT客户端;但是,它仍然可以读取和写入GATT服务器的BLE设备。有几个步骤来连​​接到BLE设备在Windows 10

权限

首先,请确保您有正确的功能设置。转到Package.appxmanifest,Capabilities选项卡,然后打开蓝牙。

Package.appxmanifest > Capabilities > Turn on Bluetooth

找到一个BLE装置

的重要注意事项。目前,Windows 10不支持连接到 未配对的BLE设备。您必须在设置页面 中配对设备,或使用应用内配对API。

了解设备已配对,有几种方法可以找到BLE设备。您可以通过外观,BluetoothAddress,ConnectionStatus,DeviceName或PairingState查找。一旦你找到你正在寻找的设备,你使用它的ID来连接它。以下是通过名称查找设备的示例:

string deviceSelector = BluetoothLEDevice.GetDeviceSelectorFromDeviceName("SOME_NAME"); 
var devices = await DeviceInformation.FindAllAsync(deviceSelector); 

// Choose which device you want, name it yourDevice 

BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(yourDevice.Id); 

FromIdAsync方法是Windows将连接到BLE设备的位置。

沟通

您可以读取和写入的特性通过以下的设备上。

// First get the characteristic you're interested in  
var characteristicId = new Guid("SOME_GUID"); 
var serviceId = new Guid("SOME_GUID"); 
var service = device.GetGattService(serviceId); 
var characterstic = service.GetCharacteristics(characteristicId)[0]; 

// Read from the characteristic 
GattReadResult result = await characterstic.ReadValueAsync(BluetoothCacheMode.Uncached); 
byte[] data = (result.Value.ToArray()); 

// Write to the characteristic 
DataWriter writer = new DataWriter(); 
byte[] data = SOME_DATA; 
writer.WriteBytes(data); 
GattCommunicationStatus status = await characteristic.WriteValueAsync(writer.DetachBuffer()); 
+0

如果GATT服务不可用,您应该配对设备并使用DeviceWatcher api。微软的人正在研究更好的API,但现在这是做到这一点的方法。更多信息可以在这里找到:http://stackoverflow.com/questions/35420940/windows-uwp-connect-to-ble-device-after-discovery/39040812#39040812 – LanderV