1

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;
      }
Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185

1 Answers1

6

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.

Pararth
  • 8,114
  • 4
  • 34
  • 51