首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于BLE扫描回调的类加载期间NoClassDefFoundError

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

Stack Overflow用户
提问于 2014-04-28 11:47:50
回答 1查看 2.5K关注 0票数 1

当我的类加载时,我一直在获取NoClassDefFoundError

代码摘自BluetoothLeGatt项目-

http://developer.android.com/samples/BluetoothLeGatt/project.html

我的代码:

代码语言:javascript
运行
复制
// 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我的上一篇文章。

将BLE特性检查作为onCreate()的第一行并不能阻止崩溃--

代码语言:javascript
运行
复制
@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;
      }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-28 12:07:28

由于BluetoothAdapter.LeScanCallback是在API 18;Source中添加的,所以这段代码也需要一个API级别检查。下面是我所做的工作,不是将回调声明为私有,而是在以下条件下进行的:

代码语言:javascript
运行
复制
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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23340265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档