即使蓝牙关闭,Android蓝牙扫描结果也可用

时间:2017-08-07 12:43:48

标签: android nullpointerexception bluetooth-lowenergy android-bluetooth

    BluetoothAdapter bluetooth = null;

    private BluetoothLeScanner scanner;

private ScanCallback scanCallback;


private BluetoothAdapter.LeScanCallback leScanCallback;

if (Build.VERSION.SDK_INT >= 18) {
                    BluetoothManager bluetoothMan = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
                    bluetooth = bluetoothMan.getAdapter();
                } else {
                    bluetooth = BluetoothAdapter.getDefaultAdapter();
                }


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    scanCallback = new ScanCallback() {
                        @Override
                        public void onScanResult(int callbackType, ScanResult result) {
                            super.onScanResult(callbackType, result);
                            btDevices.add(new BluetoothDeviceModel(result.getDevice().getName(), result.getDevice().getAddress(), result.getRssi()));
                            timeStamp = System.currentTimeMillis();
                        }
                    };
                }else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    leScanCallback = new BluetoothAdapter.LeScanCallback() {
                        @Override
                        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                            btDevices.add(new BluetoothDeviceModel(device.getName(), device.getAddress(), rssi));
                            timeStamp = System.currentTimeMillis();
                        }
                    };
                }


    if (!allPermissionsGranted(applicationContext, REQUIRED_PERMISSIONS)) {
                return false;
            }
            try {
                if (!applicationContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                    return false;
                }
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    return false;
                }



                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (permissionGranted(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
                        scanner = bluetooth.getBluetoothLeScanner();
                        scanner.startScan(scanCallback);
                        return true;
                    }
                }
                else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                    bluetooth.startLeScan(leScanCallback);
                    return true;
                }

            } catch (Exception e) {

            }
            return false;

如果我在三星手机上使用Marshmallow测试此代码会发生什么情况,它工作正常,即使蓝牙关闭也不会抛出任何异常,而是返回ScanCallback中的蓝牙扫描结果。 但是当我使用Android N和Lava与Android Lollipop使用Moto测试它时,它会在蓝牙关闭时抛出NullPointerException。

问题是当我试图在此声明中获取BluetoothLeScanner时:

scanner = bluetooth.getBluetoothLeScanner();

这里,当蓝牙关闭时,getBluetoothLeScanner方法返回null。 谁能告诉我它是如何发生的?

1 个答案:

答案 0 :(得分:0)

Android上的蓝牙API将返回已知的BT设备,因此如果您之前已与设备配对,它也会显示结果。

我相信,就像蒂姆所说的那样,你正面临一个角落的情况,你应该真的检查蓝牙是否已经打开,以避免将来出现问题。