为什么在连接BLE设备时bluetoothAdapter回调引发错误?

时间:2018-12-19 04:40:04

标签: java android bluetooth-lowenergy

我正在使用BluetoothAdapter,并尝试连接BLE设备。 当我尝试检测BLE设备时,它会在出现错误之后引发。

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)

我正在像素(Android 28版)和诺基亚移动电话(均为库存的Android设备)上运行此应用程序。无法找到任何解决方案。 我已请求位置许可。附加我的代码的某些部分以更好地理解它。

Android清单

<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>

呼叫startLeScan

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

回调

 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 );

                });

但是在非android设备中,我没有找到空指针异常。

正在寻找可以连接到我的BLE设备的解决方案。

1 个答案:

答案 0 :(得分:0)

这是因为所宣传的制造商数据无效

0xff 0x00

制造商数据必须在0xff之后至少两个字节。这两个字节应该标识制造商。