1 year ago

#333344

test-img

ososnilknarf

BTLE ServiceData is always null

I am working on a react-native Android app using react-native-ble-plx for BTLE support, and Windows 10 using the .NET API Windows.Devices.Bluetooth.GenericAttributeProfile for the GATT server/peripheral.

When I add any "ServiceData" to the advertising payload in the Windows GATT service, the scanned device's 'serviceData' payload is always null in the react-native client.

In the Windows 10 server, if the GattServiceProviderAdvertisingParameters.ServiceData property is not null, then the service advertisement status is GattServiceProviderAdvertisementStatus.StartedWithoutAllAdvertisementData. (When leaving 'ServiceData' set to null, it is GattServiceProviderAdvertisementStatus.Started, as expected).

Using the Silicon Labs "EFR Connect" app in android, it also does not show any ServiceData for the device in the advertising packet.

Using WireShark with BTVS to inspect the packets on the Windows machine, it does show the Service Data bytes, and the controller shows a 'Success' response.

Here is the code where I set up the ServiceData in Windows:

...
            GattServiceProviderAdvertisingParameters advParameters = new GattServiceProviderAdvertisingParameters
            {
                IsConnectable = _peripheralSupported,
                IsDiscoverable = true,
                ServiceData = GetServiceData().AsBuffer()
            };

            _serviceProvider.AdvertisementStatusChanged += ServiceProvider_AdvertisementStatusChanged;
            _serviceProvider.StartAdvertising(advParameters);
...

        private byte[] GetServiceData()
        {
            var flagsData = new List<byte> { 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            flagsData.AddRange(new List<byte>(Encoding.ASCII.GetBytes("ABCDEFGHIJKLMNOP")));
            return flagsData.ToArray();
        }

Here is the code in the react-native scanner:

  public scanDevices(
    timeoutSeconds: number,
    listener: (error: string | null, scannedDevice: IPhoenixDevice | null) => void,
  ): void {
    console.log('Entered PhoenixDeviceManager.scanDevices');

    this.isScanning = true;
    try {
      this.scannedDevices.length = 0; // Clear array of devices

      this.bleManager.startDeviceScan(this.serviceUUIDs, null, (error, scannedDevice) => {
        console.log('In device scan callback');

        if (error) {
          console.warn(error);
          listener(`Error message: ${error.message}, reason: ${error.reason}`, null);
        }

        if (!scannedDevice) {
          console.log('scannedDevice is null');
        } else if (!this.scannedDevices.some((d) => d.id === scannedDevice.id)) {
          this.scannedDevices.push(scannedDevice);
          listener(null, new PhoenixDevice(scannedDevice.id, scannedDevice.name));
          console.log(
            `Device discovered, id: ${scannedDevice.id}, name: ${scannedDevice.name}, localName: ${scannedDevice.localName}, serviceData: ${scannedDevice.serviceData}`,
          );
        }
      });

      const timeoutMs = timeoutSeconds * 1000;

      // stop scanning devices after specified number of seconds
      setTimeout(() => {
        this.stopScanning();
      }, timeoutMs);
    } catch (error) {
      console.error(error);
      this.stopScanning();
    }
  }

Here is the WireShark trace running on Windows 10 showing the packet containing the ServiceData (and the Success response):

Frame 499: 84 bytes on wire (672 bits), 84 bytes captured (672 bits) on interface TCP@127.0.0.1:24352, id 0
Bluetooth
Bluetooth HCI H4
Bluetooth HCI Command - LE Set Extended Advertising Data
    Command Opcode: LE Set Extended Advertising Data (0x2037)
    Parameter Total Length: 80
    Advertising Handle: 0x02
    Data Operation: Complete scan response data (0x03)
    Fragment Preference: The Controller should not fragment or should minimize fragmentation of Host data (0x01)
    Data Length: 76
    Advertising Data
        Flags
            Length: 2
            Type: Flags (0x01)
            000. .... = Reserved: 0x0
            ...1 .... = Simultaneous LE and BR/EDR to Same Device Capable (Host): true (0x1)
            .... 1... = Simultaneous LE and BR/EDR to Same Device Capable (Controller): true (0x1)
            .... .0.. = BR/EDR Not Supported: false (0x0)
            .... ..1. = LE General Discoverable Mode: true (0x1)
            .... ...0 = LE Limited Discoverable Mode: false (0x0)
        16-bit Service Class UUIDs
            Length: 3
            Type: 16-bit Service Class UUIDs (0x03)
            UUID 16: Device Information (0x180a)
        128-bit Service Class UUIDs
            Length: 17
            Type: 128-bit Service Class UUIDs (0x07)
            Custom UUID: caecface-e1d9-11e6-bf01-fe55135034f0 (Unknown)
        Service Data - 128 bit UUID
            Length: 50
            Type: Service Data - 128 bit UUID (0x21)
            Custom UUID: caecface-e1d9-11e6-bf01-fe55135034f0 (Unknown)
            Service Data: ffff0000000000000000000000000000004142434445464748494a4b4c4d4e4f50
    [Response in frame: 500]
    [Command-Response Delta: 1.889ms]

Frame 500: 7 bytes on wire (56 bits), 7 bytes captured (56 bits) on interface TCP@127.0.0.1:24352, id 0
Bluetooth
Bluetooth HCI H4
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: LE Set Extended Advertising Data (0x2037)
        0010 00.. .... .... = Opcode Group Field: LE Controller Commands (0x08)
        .... ..00 0011 0111 = Opcode Command Field: LE Set Extended Advertising Data (0x037)
    Status: Success (0x00)
    [Command in frame: 499]
    [Command-Response Delta: 1.889ms]

I've also tried using the exact code verbatim in this example and it has the exact same problem: https://github.com/ProH4Ck/treadmill-bridge/blob/98e683e2380178319972af522d9251f44350a448/src/TreadmillBridge/Services/VirtualTreadmill/VirtualTreadmillService.cs#L90

Does anyone know what the problem might be?

react-native

bluetooth

bluetooth-lowenergy

gatt

btle

0 Answers

Your Answer

Accepted video resources