1

I am working with BluetoothAdapter and trying to connect BLE device. While I am trying to detect BLE device it throws following error.

2018-12-17 18:19:03.374 15642-15663/? E/ScanRecord: unable to parse scan record: [2, 1, 5, 17, 6, 67, 79, 77, 46, 72, 65, 78, 78, 65, 73, 78, 83, 84, 95, -80, -1, 2, -1, 0, 21, 9, 72, 73, 49, 48, 56, 51, 50, 32, 112, 72, 32, 49, 49, 32, 97, 110, 100, 32, 50, 32, 5, 18, -112, 0, -96, 0, 2, 10, 0, 0, 0, 0, 0, 0, 0, 0]
2018-12-17 18:19:03.375 15642-15663/? E/BtGatt.JNI: An exception was thrown by callback 'btgattc_scan_result_cb'.
2018-12-17 18:19:03.376 15642-15663/? E/BtGatt.JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
        at android.bluetooth.le.ScanRecord.getManufacturerSpecificData(ScanRecord.java:104)
        at android.bluetooth.le.ScanFilter.matches(ScanFilter.java:317)
        at com.android.bluetooth.gatt.GattService.matchesFilters(GattService.java:883)
        at com.android.bluetooth.gatt.GattService.onScanResult(GattService.java:781)

I am running this app on pixel (android version 28) and nokia mobiles which are stock android devices. unable to find any solution. I have asked for Location permission. Attaching some parts of my code to understand it better.

Android Manifest

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:minSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:hardwareAccelerated="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
  <service
    android:name="com.lab.BluetoothLeService"
    android:enabled="true"
    android:exported="true"
    tools:ignore="ExportedService">
   </service>

calling for startLeScan

 if(bluetoothAdapter!= null) {
            bluetoothAdapter.startLeScan(mLeScanCallback);
        }

callback

 private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
                printScanRecord(scanRecord);
                StringBuilder scanData = new StringBuilder(scanRecord.length);
                for (byte dataByte : scanRecord) {
                    scanData.append(String.format("%02X ", dataByte).trim());
                }

                String scanResult = scanData.toString().trim();

                Log.e("SCAN DATA", scanResult );

                });

But in non android one device I have not found null pointer exception.

Looking for solution which can connect to my BLE device.

hannna patait
  • 355
  • 2
  • 11

1 Answers1

0

It's because the advertised manufacturer data is invalid

0xff 0x00

Manufacturer data must be at least two bytes after 0xff. Those two bytes should identify the manufacturer.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • Emil it gets connected in samsung and other non stock android devices – hannna patait Dec 19 '18 at 11:19
  • Well I guess the behaviour of detecting a device with invalid advertisement data should be undefined, so please fix the device so it advertises correct data. Anyway, the bug that the thread throws an exception was recently fixed in https://android.googlesource.com/platform/frameworks/base/+/4c2aa61202f906f52d3e5bd7f0910b0a4d837fae%5E%21/#F0. – Emil Dec 19 '18 at 12:33
  • Emil is it handled in android 28 or it will be next update – hannna patait Dec 20 '18 at 06:56
  • It was just fixed so it might take a while to get rolled out. – Emil Dec 20 '18 at 08:50
  • Emil one more thing in this case as you said scanRecord.getManufacturerSpecificData() is returning null. But what is pinching is that in samsung J7 device I am able to get result.getRssi() and other device information. But in pixel 1 device unable to fetch same BLE device. May I know why this is happening – hannna patait Dec 20 '18 at 11:38
  • Is not having Manufacturer data only reason why **pixel or stock android devices** are unable to detect BLE device. – hannna patait Dec 20 '18 at 11:45
  • If you don't have manufacturer data, just remove the field completely from the advertising data (i.e. just remove 0xff 0x00). From the exception it seems like the advertising data parser crashed due to the incorrect data and therefore dropped the packet. – Emil Dec 20 '18 at 13:21