为什么在连接BLE设备时bluetoothAdapter回调引发错误? [英] Why bluetoothAdapter callback is throwing error while connecting BLE device?

查看:161
本文介绍了为什么在连接BLE设备时bluetoothAdapter回调引发错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

<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设备中,我没有发现空指针异常.

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

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

Looking for solution which can connect to my BLE device.

推荐答案

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

It's because the advertised manufacturer data is invalid

0xff 0x00

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

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

这篇关于为什么在连接BLE设备时bluetoothAdapter回调引发错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆