【问题标题】:Windows 10 bluetooth communication using gatt protocol使用 gatt 协议的 Windows 10 蓝牙通信
【发布时间】:2016-04-28 19:25:22
【问题描述】:

我有多个需要与之通信的 BLE 设备。如何连接到特定设备并与之通信?

在 Windows 10 中,似乎没有连接方法。

谢谢

【问题讨论】:

    标签: bluetooth win-universal-app windows-10-universal bluetooth-lowenergy


    【解决方案1】:

    我认为它尚不可用。您需要等到周年纪念更新(希望如此)。在 Windows 开发人员反馈用户语音页面上查看此内容https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7176829-gatt-server-api

    GATT 服务器 API 将在该更新中提供给开发者,敬请期待

    他指出了 Build 2016 中显示的更新,即周年更新

    【讨论】:

      【解决方案2】:

      目前,Windows 只能是 GATT 客户端;但是,它仍然可以读写作为 GATT 服务器的 BLE 设备。在 Windows 10 中连接到 BLE 设备有几个步骤。

      权限

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

      Package.appxmanifest > Capabilities > Turn on Bluetooth

      查找 BLE 设备

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

      知道设备已配对,有几种方法可以找到 BLE 设备。您可以按 Appearance、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());
      

      【讨论】:

      • 如果 GATT 服务不可用,您应该与设备配对并使用 DeviceWatcher api。 Microsoft 的人员正在开发更好的 API,但目前这是实现它的方法。更多信息可以在这里找到:stackoverflow.com/questions/35420940/…
      • 所以,我猜这是在 C# 中?它没有在任何地方提及。
      猜你喜欢
      • 2018-04-05
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      相关资源
      最近更新 更多