在使用Android 4.4的Nexus 7上运行时,onScanResult会抛出NullPointerExceptionas,如下面的日志中所示:
03-18 17:59:34.170: D/BluetoothAdapter(5092): onScanResult() - Device=78:4B:08:02:7C:91 RSSI=-77
03-18 17:59:34.170: W/BluetoothAdapter(5092): Unhandled exception: java.lang.NullPointerException
然而,其他设备没有这样的问题。
上找到了这个 public void onScanResult(String address, int rssi, byte[] advData) {
if (DBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi);
// Check null in case the scan has been stopped
synchronized(this) {
if (mLeHandle <= 0) return;
}
try {
BluetoothAdapter adapter = mBluetoothAdapter.get();
if (adapter == null) {
Log.d(TAG, "onScanResult, BluetoothAdapter null");
return;
}
mLeScanCb.onLeScan(adapter.getRemoteDevice(address), rssi, advData);
} catch (Exception ex) {
Log.w(TAG, "Unhandled exception: " + ex);
}
}
其中有许多潜在的罪魁祸首,但当然我无法为这些变量设置值。
为什么这对Nexus 7失败而对其他设备失败?任何解决方法的想法?
答案 0 :(得分:0)
Android 4.3, 4.4: BLE filtering in startLeScan(UUIDs, callback) doesn't work for 128-bit UUIDs
适用于三星s5,使用Android 4.4.2测试但由于某些原因,它在Nexus上失败了。等待来自Google堆栈的此修复程序。
如果您只想搜索特定地址,可以使用this解决方案。基本上,您必须使用scanRec [],需要额外的努力来解析它,然后将具有匹配地址的设备添加到列表适配器中。
[我知道,....希望简单的API能够运作! :P]
答案 1 :(得分:0)
关闭WIFI:
我也可以确认,关闭WIFI会使蓝牙4.0更稳定,尤其是在谷歌Nexus上(我有一台Nexus 7)。 问题
我正在开发的应用程序需要WIFI和连续蓝牙LE扫描。所以关闭WIFI对我来说是不可取的。
此外我已经意识到,持续蓝牙LE扫描实际上可以杀死WIFI连接并使WIFI适配器无法重新连接到任何WIFI网络,直到BLE扫描开启。 (我不确定移动网络和移动互联网)。 这肯定发生在以下设备上:
Nexus 7
Motorola Moto G
然而,使用WIFI进行BLE扫描似乎非常稳定:
Samsung S4
HTC One
我的解决方法
我在短时间内扫描BLE 3-4秒然后关闭扫描3-4秒。然后再次开启。
Obviously I always turn BLE scan OFF when I'm connecting to a BLE device.
When I disconnect from a device I restart BLE (turn adapter OFF and then ON) to reset the stack before starting scan again.
I also reset BLE when discovering services or characteristics fails.
When I get advertisement data from a device that the app should connect to (lets say 500 times without being able to connect - thats about 5-10 seconds of advertising) I reset BLE again.