【问题标题】:NoSuchMethod ExceptionNoSuchMethod 异常
【发布时间】:2016-07-12 18:01:39
【问题描述】:

我使用的是BLE startLescan,但它现在已经过时了。现在我已经更改了我的 API level to 23 (from 20) 并为此目的使用了 BluetoothLeScanner。 我的开始扫描功能是:

public void startScan(){
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

     mBluetoothLeScanner.startScan(new ScanCallback() {
            @Override
            public void onScanResult(int callbackType, ScanResult result) {
                super.onScanResult(callbackType, result);

                String s = "\nRssi : "+result.getRssi()+"" +
                        "\nName (Get Device) : "+result.getDevice().getName()+"" +
                        "\nBytes"+result.getScanRecord().getBytes()+"" +
                        "\nGet Device : " + result.getDevice()+"" +
                        "\nAddress : "+result.getDevice().getAddress()+"" +
                        "\nService UUIds : "+result.getScanRecord().getServiceUuids().get(0)+"" +       //Unique
                        "\nName (Scan Record) : "+result.getScanRecord().getDeviceName()+"" +
                        "\nUuids device : "+result.getDevice().getUuids()+"" +
                        "\nDescribe contents : "+result.describeContents();

                //This will show you all the data in logs.
                Log.e("All Data",s);



            }

            @Override
            public void onBatchScanResults(List<ScanResult> results) {
                super.onBatchScanResults(results);
            }

            @Override
            public void onScanFailed(int errorCode) {
                super.onScanFailed(errorCode);
            }
        });

当我到达第一行时,

java 抛出 nosuchmethod 异常:

选择器“getBluetoothLeScanner”的方法查找失败 签名“()Landroid/bluetooth/le/BluetoothLeScanner;”

【问题讨论】:

  • 向我们展示startScan方法上面的代码。你是如何初始化mBluetoothAdapter 的?/

标签: java android android-5.0-lollipop bluetooth-lowenergy nosuchmethoderror


【解决方案1】:

在这种情况下需要一个蓝牙适配器的实例。做这样的事情:

Context mContext = getBaseContext();
BluetoothAdapter mBluetoothAdapter = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE).getAdapter();
BluetoothLeScanner mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

mBluetoothLeScanner.startScan(new ScanCallback() {….

}

【讨论】:

  • 蓝牙适配器在那里,我一开始就实例化了它:BluetoothManager manager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE); mBluetoothAdapter = manager.getAdapter();
【解决方案2】:

如果您的设备的 API 版本为 20 或更低版本,它将无法使用新的 API。因此,您需要实现这两种扫描方法并根据设备操作系统版本检查应该使用哪个版本。 (Kitkat 将不支持新的扫描 API!)

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2012-01-10
    • 2014-10-27
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多