NoClassDefFoundError的类加载了BLE扫描回调过程 [英] NoClassDefFoundError during class load for BLE scanning callback
问题描述
我不断获取的的NoClassDefFoundError
时,我的类加载。
在code摘自 BluetoothLeGatt 项目 -
http://developer.android.com/samples/BluetoothLeGatt/project.html
我的code:
//设备扫描回调。
私人BluetoothAdapter.LeScanCallback mLeScanCallback =
新BluetoothAdapter.LeScanCallback(){... //java.lang.NoClassDefFoundError @覆盖
公共无效onLeScan(最终BluetoothDevice类设备,
最终诠释RSSI,最后一个字节[] scanRecord){
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
弦乐味精= device.getAddress(); Log.d(TAG,味精);
为addItems(MSG);
}
});
}
};
有人建议该错误是因为我的设备不支持BLE但我想摆脱这种错误的任何设备。所以,如果它不支持BLE功能,则直接跳过此错误与其他调用此继续 BluetoothAdapter.LeScanCallback
。
注意:
请参阅this我的previous SO发布更多的澄清。
把BLE功能检查作为第一线的onCreate()不停止崩溃 -
@覆盖
公共无效的onCreate(捆绑savedInstanceState){ 如果(!bleCheck()){
Toast.makeText(getApplicationContext(),R.string.ble_not_supported,
Toast.LENGTH_SHORT).show();
完();
} 在code的//休息
//调用到BLE扫描按钮单击导致错误..
} 私人布尔bleCheck(){
布尔结果= FALSE;
如果(getPackageManager()。
hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
结果=真;
}
返回结果;
}
由于 BluetoothAdapter.LeScanCallback
为在API级别18 ; 来源,这code将需要一个API等级检定也。下面是我去了这一点,没有宣布回调作为私人使用,但条件下:
布尔apiJBorAbove = android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.JELLY_BEAN_MR2?真正
:假的;布尔isBleAvailable = getPackageManager()。hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE)?真假;
//使用这个检查,以确定是否BLE支持设备上。
如果(isBleAvailable&安培;&安培; apiJBorAbove){
//初始化一个蓝牙适配器。
//对于API 18级以上,获得一个参考
// BluetoothAdapter通过BluetoothManager。
最后BluetoothManager bluetoothManager =(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();//确保蓝牙设备上可用,并已启用。
//如果没有,则显示请求的用户的权限,以使一个对话
// 蓝牙。
如果(mBluetoothAdapter == NULL ||!mBluetoothAdapter.isEnabled()){
startActivity(新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE));
}BluetoothAdapter.LeScanCallback mLeScanCallback =
新BluetoothAdapter.LeScanCallback(){ @覆盖
公共无效onLeScan(最终BluetoothDevice类设备,
最终诠释RSSI,最后一个字节[] scanRecord){
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
弦乐味精= device.getAddress();
// Log.d(TAG,味精);
//为addItems(MSG);
}
});
}
};
}
的的NoClassDefFoundError
是由于API,而不是依据 PackageManager.FEATURE_BLUETOOTH_LE
在
I am keep on getting the NoClassDefFoundError
when my class is loaded.
The code is taken from BluetoothLeGatt project -
http://developer.android.com/samples/BluetoothLeGatt/project.html
My code:
// 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);
}
});
}
};
Someone suggested that the error is because my device doesn't support BLE but I want to get rid of this error for any device. So if it doesn't support BLE feature then simply skip this error else continue with the call to this BluetoothAdapter.LeScanCallback
.
NOTE:
See this my previous SO post for more clarification.
Putting the BLE feature check as the first line onCreate() doesn't stop the crash --
@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;
}
As BluetoothAdapter.LeScanCallback
was Added in API level 18 ; Source, this code would need a API level check also. Here's how i have gone about this, not declared the callback as private but under the condition:
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);
}
});
}
};
}
The NoClassDefFoundError
is due to the API , not on the basis of PackageManager.FEATURE_BLUETOOTH_LE
.
这篇关于NoClassDefFoundError的类加载了BLE扫描回调过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!