简体   繁体   中英

How to pair and search Bluetooth Low Energy services on phone in windows desktop application?

We got phone app that hosts GATT server with service and characteristic. From the desktop application we trying to search it with UWP API using DeviceWatcher.

var deviceWatcher = DeviceInformation.CreateWatcher(
        BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
        new List<string>(),
        DeviceInformationKind.AssociationEndpoint);

Then we trying to get service from device

var serviceResult = await device.GetGattServicesForUuidAsync(ServiceId);

But this approach is very unstable. It takes 30-60 seconds to find and connect to device. Sometimes it didn't finds the device or finds the device but fails to get service.

Also we tried to pair device with PC and check only paired or connected devices

var deviceWatcher = DeviceInformation.CreateWatcher(
        BluetoothLEDevice.GetDeviceSelectorFromPairingState(true),
        new List<string>(),
        DeviceInformationKind.AssociationEndpoint);

But this watcher didn't find anything. We've tried different AQS filters and got the same results.

Also we tried to use 32feet lib to get only paired connected devices.

var client = new BluetoothClient();
var paired = client.DiscoverDevices(5, true, true, false);
foreach (var bluetoothDeviceInfo in paired)
{
   var addressBytes = bluetoothDeviceInfo.DeviceAddress.ToByteArray();
   var addr = BitConverter.ToUInt64(addressBytes, 0);
   var device = await BluetoothLEDevice.FromBluetoothAddressAsync(addr));
   var serviceResult = await device.GetGattServicesForUuidAsync(ServiceId);
}

It works fast and finds phone, but found device does not contain our GATT service and it's bluetooth address differs from devices that has been found by DeviceWatcher. It looks like there is a two bluetooth devices on one phone: first device has our BLE service, second is paired but have no service.

Is there any way to pair a correct BLE device and search only paired?

Finally I found out how to search for services directly using BluetoothLEAdvertisementWatcher.

_watcher = new BluetoothLEAdvertisementWatcher();
_watcher.ScanningMode = BluetoothLEScanningMode.Active;
_watcher.SignalStrengthFilter = new BluetoothSignalStrengthFilter
                                    {
                                         InRangeThresholdInDBm = -75,
                                         OutOfRangeThresholdInDBm = -76,
                                         OutOfRangeTimeout  = TimeSpan.FromSeconds(2),
                                         SamplingInterval = TimeSpan.FromSeconds(2)
                                    };
_watcher.AdvertisementFilter =
     new BluetoothLEAdvertisementFilter
         {
              Advertisement =
                  new BluetoothLEAdvertisement
                      {
                            ServiceUuids =
                                {
                                        BLEHelper.ServiceId
                                }
                       }
        };
_watcher.Received += OnWatcherOnReceived;
_watcher.Start();

In OnWatcherOnReceived we can get bluetooth address to connect to service and get characteristic

    private void OnWatcherOnReceived(BluetoothLEAdvertisementWatcher w, BluetoothLEAdvertisementReceivedEventArgs a)
    {
        // use a.BluetoothAddress to connect to device
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM