Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read raw data from scanned BLE device?

I am using RxAndroidBle Library for my BLE application. Like nrfConnect application I want to read RAW data from BLE device.enter image description here

How to read RAW data from device?

like image 558
Pallavi Tapkir Avatar asked Nov 27 '25 07:11

Pallavi Tapkir


2 Answers

The raw data you see are the hexadecimal values advertised by your bluetooth device.

Your app can read these data using android.bluetooth.le.BluetoothLeScanner's method:

public void startScan(List<ScanFilter> filters, ScanSettings settings,
            final ScanCallback callback);

Here is a sample code of a ScanCallback implementation you can pass as parameter to read the advertisement data:

ScanCallback scanCallback = new ScanCallback() {

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        BluetoothDevice device = result.getDevice();
        byte[] scanRecord = result.getScanRecord().getBytes();
        int rssi = result.getRssi();

        // Search through raw data for the type identifier 0xFF, decode 
        // the following bytes over the encoded packet length ...
        // yourCallback.onScanResult(device, scanRecord, rssi);
    }
};

In your case, the raw data only includes type 0xFF, which is the Manufacturer Specific Data type, on a length of 30 bytes. Your callback handling should search through raw data for the type identifier 0xFF, and decode the following bytes over the encoded packet length.

The types of data included in advertising data varies depending on the device manufacturer, but it should includes at least the Manufacturer Specific Data, which starts with two bytes for the company identifier.

There are other types of BLE advertising data, such as:

  • Service UUID: used to include a list of Service UUIDs
  • Local Name: the device name (either Shortened or Complete)
  • Flags: one-bit flags that are included when an advertising packet is connectable.
  • Advertising Interval: self-explanatory.

This page lists various types of BLE advertising data:

https://www.novelbits.io/bluetooth-low-energy-advertisements-part-1/

like image 151
matdev Avatar answered Nov 28 '25 21:11

matdev


To access raw bytes of the advertisement data using RxAndroidBle you need to:

  1. Perform a scan to obtain ScanResult (RxBleClient#scanBleDevices(ScanSettings, ScanFilter...))
  2. From a result you need to get the ScanRecord (ScanResult#getScanRecord)
  3. From the record you can access raw byte[] (ScanRecord#getBytes)
like image 25
Dariusz Seweryn Avatar answered Nov 28 '25 22:11

Dariusz Seweryn