找不到类'android.bluetooth.BluetoothManager'

时间:2015-12-16 14:20:11

标签: android bluetooth android-bluetooth bluetooth-lowenergy

在API<的手机上打开活动时18它给出了例外

Could not find class 'android.bluetooth.BluetoothManager'

尽管进行了以下检查:

private void activateBluetoothSmart() {

    if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) && (Build.VERSION.SDK_INT >= 18)) {
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        myBluetoothAdapter = bluetoothManager.getAdapter();

        if (myBluetoothAdapter == null || myBluetoothAdapter.isEnabled() == false) {
            Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BT);
        } else {
            scanForHrm();
        }
    }
}

除了为什么忽略版本检查之外,我不明白为什么在打开活动时调用代码,只应在用户按下按钮时调用它。

打开活动时是否所有类都“预加载”了?我看过这个问题:NoClassDefFoundError during class load for BLE scanning callback建议使用版本代码检查以及功能检查,但没有找到其他任何内容。

1 个答案:

答案 0 :(得分:0)

所以问题是在打开活动时会导入所有引用的类。解决方案是创建一个新类并将所有蓝牙LE相关代码放在该新类中,然后使用正确的条件检查调用该新类的方法。

我认为这是因为"延迟类加载",引用的类现在在一个新类中,并且只有在调用该类时才会导入它们。