我有多个BLE设备,我需要与之沟通。如何连接到特定的设备并与其通信?
在Windows 10中,似乎没有连接方法。
谢谢
发布于 2016-04-29 04:37:15
我想现在还没有。你需要等到周年更新(希望如此)。在Windows反馈用户语音页面https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7176829-gatt-server-api上查看
在该更新中,开发人员可以使用GATT,因此请继续关注
他指出了Build 2016“周年更新”中显示的更新
发布于 2016-05-11 00:58:22
现在,Windows只能是GATT客户端,但是它仍然可以读写关贸总协定服务器的BLE设备。在Windows 10中,有几个步骤可以连接到BLE设备。
权限
首先,确保设置了正确的功能。转到Package.appxmanifest,功能选项卡,打开蓝牙。
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());https://stackoverflow.com/questions/36923759
复制相似问题