简体   繁体   中英

Windows BluetoothLeAdvertisement does not contain service uuid broadcasted by Android device

Im trying to read the ServiceUuids broadcasted by an Android peripheral from Windows 10 like this:

private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs advertisementArg)
{
    Debug.WriteLine(" serviceUuids for advertisement " + advertisementArg.Advertisement.ServiceUuids.Count);
    foreach(Guid guid in advertisementArg.Advertisement.ServiceUuids)
    {
        Debug.WriteLine("uuid is " + guid.ToString() + " address is " +advertisementArg.BluetoothAddress+ " name is "+ advertisementArg.Advertisement.LocalName);
    }
}

However, when an Advertisement from an Android peripheral is received, the size of the ServiceUuids list is always 0.

This is weird because the service data is correctly placed in the BluetoothLeAdvertisementDataSection of the advertisement:

IList<BluetoothLEAdvertisementDataSection> dataSection = advertisementArg.Advertisement.DataSections;

foreach (BluetoothLEAdvertisementDataSection ad in dataSection)
{
    if (ad.Data.Length > 0)
    {
        DataReader dataReader = DataReader.FromBuffer(ad.Data);
        byte[] bytes = new byte[ad.Data.Length];

        dataReader.ReadBytes(bytes);

        if (bytes.Length > 0)
        {
            string data = System.Text.Encoding.UTF8.GetString(crcBytes, 0, crcBytes.Length);
            Debug.WriteLine("ANDROID DEVICE FOUND " + data);
        }         
    }
}

So it's just the ServiceUuid that is missing.

The service data is added in the Android peripheral like this:

AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
dataBuilder.addServiceData(new ParcelUuid(bluetoothUuid), advertiseData.getBytes());

The Android data is a “Service Data” Section type and not a “List of service UUIDs” section type. The service UUIDs property only works for list of service UUID and not the service data.

For Bluetooth Advertisement (Beacons) can be used to send a packet of information contain a Universally Unique Identifier (UUID),the data section of the packet is customizable by the app developer.

We should be able to parse the data in BluetoothLEManufacturerData to get the UUID.

For example:

private  void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
    var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
    if (manufacturerSections.Count > 0)
    {
        var manufacturerData = manufacturerSections[0];
        var data = new byte[manufacturerData.Data.Length];
        using (var reader = DataReader.FromBuffer(manufacturerData.Data))
        {
            reader.ReadBytes(data);
        }
    }
}

There is an official sample about BluetoothAdvertisement , it shows how to use the Bluetooth Advertisement API to send and receive Bluetooth Low Energy advertisements.

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