Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare

这个错误翻译的意思是:不能在没有Looper.prepare的线程里面创建handler。

起初我很疑惑,我根本没有用到工作线程,也没有创建handler。报错的代码如下:

    // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

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

                    final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord);

                    if (mFilter.isDevice(ibeacon)) {
                        scanLeDevice(false);//停止扫描

                        T.showShort(getActivity(), getString(R.string.sign_in_success));
                        AppSettings.setPrefString(getActivity(), Config.ISSIGNIN, mCurrentDate);
                        setButtonState();//改变按钮的状态
                        DialogUtils.proGone();
                       // mControl.closeBlue();//关闭蓝牙
                      }
                }
            };

百度了下,网上的做法是如下:

Looper.prepare();
BluetoothAdapter.getDefaultAdapter();

在获取BluetoothAdapter的之前,加上Looper.prepare();,然并卵。问了很多人,都说不知,这时候这能自力更生了。最后终于被我找到解决办法。

思路:我查看了SDK的源码,发现原来我再调用Toast的时候,创建了handler,源码如下,

这里写图片描述
当我查看TN这个类的时候,就发现了问题的所在
这里写图片描述

类 TN 是一个aidl的.Stub的子类,而Toast的显示和隐藏都是通过此类进行通信的,而里面使用handler,具体原理我就不深究下去了。我于是猜想 new BluetoothAdapter.LeScanCallback()的onLeScan可能并没有运行在主线程,于是我调用了runOnUiThread()方法,结果果然解决了。修改后代码如下:

 // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

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

                    final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord);

                    if (mFilter.isDevice(ibeacon)) {
                        scanLeDevice(false);//停止扫描
                        getActivity().runOnUiThread(new Runnable() {//UI线程的控件改变状态,需要调用此方法,不然可能会无效
                            @Override
                            public void run() {
                                T.showShort(getActivity(), getString(R.string.sign_in_success));
                                AppSettings.setPrefString(getActivity(), Config.ISSIGNIN, mCurrentDate);
                                setButtonState();
                                DialogUtils.proGone();
                                // mControl.closeBlue();//关闭蓝牙
                            }
                        });

                    }

                }
            };

到此,问题已解决,为了让其他同道碰到此问题,而减少寻找bug的时间,特写此博客,告知!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值