Android 为BLE扫描回调加载类期间出现NoClassDefFoundError
当我的类被加载时,我一直在获取Android 为BLE扫描回调加载类期间出现NoClassDefFoundError,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,当我的类被加载时,我一直在获取NoClassDefFoundError 代码取自BluetoothLeGatt项目- 我的代码: // Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { //java.lang.NoClassDefFoundError... @Override p
NoClassDefFoundError
代码取自BluetoothLeGatt项目-
我的代码:
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() { //java.lang.NoClassDefFoundError...
@Override
public void onLeScan(final BluetoothDevice device,
final int rssi, final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg= device.getAddress();
Log.d(TAG,msg);
addItems(msg);
}
});
}
};
有人建议,错误是因为我的设备不支持BLE,但我想消除任何设备的此错误。因此,如果它不支持BLE功能,只需跳过此错误,否则继续调用此BluetoothAdapter.LeScanCallback
注意:
请参阅我之前的SO帖子以了解更多说明
将BLE特性检查作为onCreate()的第一行并不能停止崩溃--
As
BluetoothAdapter.LeScanCallback
是在API级别18中添加的,此代码还需要进行API级别检查。我是这样做的,没有将回调声明为private,而是在以下条件下:
boolean apiJBorAbove = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 ? true
: false;
boolean isBleAvailable = getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE) ? true : false;
// Use this check to determine whether BLE is supported on the device.
if (isBleAvailable && apiJBorAbove) {
// Initializes a Bluetooth adapter.
// For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled.
// If not, displays a dialog requesting user permission to enable
// Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
}
BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device,
final int rssi, final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg= device.getAddress();
// Log.d(TAG,msg);
// addItems(msg);
}
});
}
};
}
NoClassDefFoundError
是由API引起的,而不是基于PackageManager.FEATURE_BLUETOOTH_LE
作为BluetoothAdapter。LeScanCallback
是在API级别18中添加的,此代码还需要进行API级别检查。我是这样做的,没有将回调声明为private,而是在以下条件下:
boolean apiJBorAbove = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 ? true
: false;
boolean isBleAvailable = getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE) ? true : false;
// Use this check to determine whether BLE is supported on the device.
if (isBleAvailable && apiJBorAbove) {
// Initializes a Bluetooth adapter.
// For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled.
// If not, displays a dialog requesting user permission to enable
// Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
}
BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device,
final int rssi, final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String msg= device.getAddress();
// Log.d(TAG,msg);
// addItems(msg);
}
});
}
};
}
NoClassDefFoundError
是由于API造成的,而不是基于PackageManager。功能\u BLUETOOTH\u LE
希望答案有帮助,BLE for Android目前还很棘手,正在研究iHope答案帮助,BLE for Android目前还很棘手,正在研究itAh,非常感谢。你不知道你节省了多少时间。欢迎,很高兴它能帮上忙,如果你还遇到其他问题,请告诉我。安卓系统中的BLE对我来说非常陌生。如果卡住了,会让你知道的。谢谢我坚持通过BLE发送命令。让我知道,如果你能帮助-检查我的答案和参考项目,将有助于你理解啊,老兄,非常感谢。你不知道你节省了多少时间。欢迎,很高兴它能帮上忙,如果你还遇到其他问题,请告诉我。安卓系统中的BLE对我来说非常陌生。如果卡住了,会让你知道的。谢谢我坚持通过BLE发送命令。让我知道,如果你可以帮助在-检查我的答案和参考项目,将帮助你了解这一点