用于BLE扫描回调的类加载期间的NoClassDefFoundError

时间:2014-04-28 11:47:50

标签: android bluetooth-lowenergy android-bluetooth

我的课程加载后,我继续学习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;
      }

1 个答案:

答案 0 :(得分:6)

由于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