BLE 扫描回调的类加载期间的 NoClassDefFoundError
Posted
技术标签:
【中文标题】BLE 扫描回调的类加载期间的 NoClassDefFoundError【英文标题】:NoClassDefFoundError during class load for BLE scanning callback 【发布时间】:2014-04-28 11:47:50 【问题描述】:当我的课程被加载时,我会不断收到NoClassDefFoundError
。
代码取自 BluetoothLeGatt 项目 -
http://developer.android.com/samples/BluetoothLeGatt/project.html
我的代码:
// 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
。
注意:
请参阅this我以前的 SO 帖子以获得更多说明。
将 BLE 功能检查作为第一行 onCreate() 并不能阻止崩溃 --
@Override
public void onCreate(Bundle savedInstanceState)
if (!bleCheck())
Toast.makeText(getApplicationContext(), R.string.ble_not_supported,
Toast.LENGTH_SHORT).show();
finish();
//Rest of the code
//Call to BLE Scan on button click that causes error..
private boolean bleCheck()
boolean result = false;
if (getPackageManager().
hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
result = true;
return result;
【问题讨论】:
希望答案对您有所帮助,Android 的 BLE 目前还很棘手,一直在努力 【参考方案1】:BluetoothAdapter.LeScanCallback
是 在 API 级别 18 中添加的 ; Source,此代码还需要 API 级别检查。以下是我的处理方式,没有将回调声明为私有,而是在以下条件下:
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
。
【讨论】:
啊,非常感谢。你不知道你节省了多少时间。 欢迎,很高兴它有帮助,如果您遇到其他问题,请告诉我 当然,android 中的 BLE 对我来说非常新。如果卡住了会通知你。谢谢! 我一直坚持通过 BLE 发送命令。让我知道您是否可以提供帮助 - ***.com/questions/23393010/… 检查我的答案和参考项目,将帮助您理解以上是关于BLE 扫描回调的类加载期间的 NoClassDefFoundError的主要内容,如果未能解决你的问题,请参考以下文章