device.GattServices在Windows通用应用程序上为BLE设备返回一个空集

时间:2017-05-19 13:38:55

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

我正在使用Microsoft中的示例。当收到广告时,我正在打电话

BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);

然后

device.GattServices()

但总是会返回一个空列表。为什么会这样?我没有找到任何答案。

2 个答案:

答案 0 :(得分:1)

如果您希望通过使用广告观察器来实现此功能,则需要定位Windows 10创建者更新(10.0; Build 15063)并使用最新的SDK,否则您必须首先配对设备。 要获取GattServices,首先检查设备是否为空。 然后使用:

var serviceResult = await bluetoothLeDevice.GetGattServicesAsync();
if (serviceResult.Status == GattCommunicationStatus.Success)
{
//Do something with servivicResult list
}

但是有一个问题;可能是serviceResult.Status返回成功,但尚未找到所有没有服务。 我的解决方案是将它放在一个有短暂延迟的循环中并尝试几次,直到serviceResult计数保持不变。

答案 1 :(得分:0)

我想在我的评论中加入更多解释,但出了点问题,无法编辑我的评论,所以我会将其添加为答案。 我有完全相同的问题。出于某种原因,您必须将BLE设备初始化为null。

private BluetoothLEDevice device = null;

另外为了防止advertWatcher一遍又一遍地设置你的设备,使用if语句只在​​设备为空时才设置它,

 if (device == null)
 {
    device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
 }  

或者如果您想设置多个设备而不是将它们添加到集合中,并确保每个设备只添加一次。

这是我的工作代码:

  private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher,
                                            BluetoothLEAdvertisementReceivedEventArgs eventArgs)
  {
     BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;
     short rssi = eventArgs.RawSignalStrengthInDBm;
     string localName = eventArgs.Advertisement.LocalName;
     string manufacturerDataString = "";
     var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
     if (manufacturerSections.Count > 0)
     {
        // Only print the first one of the list
        var manufacturerData = manufacturerSections[0];
        var data = new byte[manufacturerData.Data.Length];
        using (var reader = DataReader.FromBuffer(manufacturerData.Data))
        {
           reader.ReadBytes(data);
        }
        manufacturerDataString = string.Format("0x{0}: {1}",
        manufacturerData.CompanyId.ToString("X"),
        BitConverter.ToString(data));
     }
     string res = string.Format("type={0}, rssi={1}, name={2}, manufacturerData=[{3}]",
            advertisementType.ToString(),
            rssi.ToString(),
            localName,
            manufacturerDataString);
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
        textBoxWatcher.Text = res;
     });
     if (device == null)
     {
        device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);

        if (device != null)
        {
           var deviceInfo = device.DeviceInformation;
           await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
           {
              if (device.Name != string.Empty)
              {
                 //ResultCollection is a observable observable collection of blueutooth devices
                 // to bind to a listvieuw,it is not needed!
                 ResultCollection.Add(new BleDevice(device));
                 if (deviceInfo.Name == "HMSoft")
                 {
                    if (ResultCollection[0] is BleDevice bleDevice)
                    {
                       BleDeviceId = bleDevice.Id;
                       SelectedBleDeviceName = bleDevice.Name;
                    }                        
                    Connect();
                 }
              }
           });
        }
     }