BluetoothLeScanner找不到回调包装器 [英] BluetoothLeScanner could not find callback wrapper

查看:839
本文介绍了BluetoothLeScanner找不到回调包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在Android Lollipop上的蓝牙出现问题,因此我尝试更改扫描仪方法.
因此,我尝试使用新程序包. 在以前的版本中,我调用了startScan(mLeScanCallback),并且一切正常,但是现在,当我调用startScan(mScanCallback)时,出现错误:"D/BluetoothLeScanner:找不到回调包装器".
找不到设备,用于显示设备的ListAdapter为空.

Because of I had problems with Bluetooth on Android Lollipop, I have tried to change the scanner method.
So I have tried to use the new package. In the previous version, I called startScan(mLeScanCallback) and everything works but now, when I call startScan(mScanCallback) I have the error: "D/BluetoothLeScanner: could not find callback wrapper".
No devices are found and the ListAdapter, I use to show the devices, is empty.

注释行是先前的代码(它起作用了!). 这是我的代码:

The comment lines are the previous code (and it worked!). This my code:

public class Selection extends ListActivity implements ServiceConnection {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHandler = new Handler();

        // Initializes a Bluetooth adapter through BluetoothManager.
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();



        getApplicationContext().bindService(new Intent(this, MetaWearBleService.class), this, Context.BIND_AUTO_CREATE);

}

private void scanLeDevice(final boolean enable) {
    final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

    if (enable) {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //mBluetoothAdapter.stopLeScan(mLeScanCallback);
                bluetoothLeScanner.stopScan(mScanCallback);
                setListAdapter(listAdapter);

            }
        }, SCAN_PERIOD);


        //mBluetoothAdapter.startLeScan(mLeScanCallback);
        bluetoothLeScanner.startScan(mScanCallback);

   } else {

        //mBluetoothAdapter.stopLeScan(mLeScanCallback);
        bluetoothLeScanner.stopScan(mScanCallback);
        setListAdapter(listAdapter);

    }


}

private ScanCallback mScanCallback =
        new ScanCallback() {

            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        listAdapter.addDevice(device);

                    }
                });
            }
        };

相反,ListAdapter扩展了BaseAdapter并使用ViewHolder.如有必要,我将其发布.

Instead the ListAdapter extends BaseAdapter and use a ViewHolder. If it necessary, I post it.

那么"D/BluetoothLeScanner:找不到回调包装器"是什么意思?怎么了?

So what does it mean "D/BluetoothLeScanner: could not find callback wrapper"? What is it wrong?

否则,我如何无法解决使用Android Lollipop进行扫描的问题? 在棒棒糖中,我经常会遇到有关BluetoothGatt的错误.我不知道要缩小(或解决).

Otherwise how I can't resolve the problem of scanning with the Android Lollipop? In Lollipop I have often errors about BluetoothGatt. I don't know to minized it (or solve it).

谢谢

推荐答案

在提示Android蓝牙扫描API认为尚未开始扫描应用的情况下,他们首先停止扫描应用时会出现日志消息D/BluetoothLeScanner: could not find callback wrapper >.您可以通过查看Android BluetoothLeScanner 这通常可以忽略,因为有很多原因导致我的扫描尚未真正开始(该扫描已停止,蓝牙已关闭,未授予权限等).客户端软件进行扫描的过程通常会停止对计时器的扫描,而不管它是否已成功启动,还是在计时器关闭之前是否已手动停止. Android的示例代码(以及上面显示的代码)正是这样做的,通常会导致显示这些日志消息.

This is usually safe to ignore as there are lot of reasons that scanning my not have actually started (it was already stopped, bluetooth is off, permissions have not been granted, etc.) Client software that does scanning often stops scanning on a timer regardless of whether it has been successfully started, or whether it was manually stopped before the timer goes off. Android's example code (and the code shown above) does exactly this, often causing these log messages to show up.

如果您真的想最小化这些消息,则需要跟踪扫描是否真正开始,并且仅在扫描确实开始时才停止扫描.不幸的是,如果扫描成功启动,您将不会获得返回码,并且如果无法成功启动,您将仅获得对onScanFailed(errorCode)的异步回调.因此,一种方法是在调用开始扫描时设置scanStartCount++;,并在收到onScanFailed(errorCode)的回调时设置scanStartCount--;.然后,当计时器关闭以停止扫描时,只有在scanStartCount> 0时才真正停止扫描.

If you really want to minimize these messages, you need to keep track of whether scanning actually started and only stop scanning if it actually did. Unfortunately, you don't get a return code if scanning starts successfully, and you only get an asynchronous callback to onScanFailed(errorCode) if you cannot start successfully. So one approach would be to set scanStartCount++; when you call start scan, and set scanStartCount--; when you get a callback to onScanFailed(errorCode). Then when your timer goes off to stop the scan, only actually stop it if the scanStartCount > 0.

请记住,您只能最小化来自您的应用程序的这些消息.手机上进行蓝牙扫描的其他应用程序也可能导致这些消息也发出.

Keep in mind that you can only minimize these messages coming from your application. Other applications on the phone doing bluetooth scanning may be causing these messages to be emitted as well.

这篇关于BluetoothLeScanner找不到回调包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆