NoClassDefFoundError during class load for BLE scanning callback

家住魔仙堡 提交于 2019-12-23 02:30:39

问题


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;
      }

回答1:


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.



来源:https://stackoverflow.com/questions/23340265/noclassdeffounderror-during-class-load-for-ble-scanning-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!