Why bluetoothAdapter callback is throwing error while connecting BLE device?

谁都会走 提交于 2019-12-13 17:20:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/53844676/why-bluetoothadapter-callback-is-throwing-error-while-connecting-ble-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!