This error translation means: You cannot create handler in a thread that does not have looper.prepare.
At first I was puzzled that I did not use the worker thread at all, nor did I create a handler. The code for the error is as follows:
//Device scan callback. PrivateBluetoothadapter.lescancallback Mlescancallback =NewBluetoothadapter.lescancallback () {@Override Public void Onlescan(FinalBluetoothdevice device,intRssibyte[] Scanrecord) {FinalIbeaconclass.ibeacon IBeacon = Ibeaconclass.fromscandata (device, Rssi, Scanrecord);if(Mfilter.isdevice (ibeacon)) {Scanledevice (false);//Stop scanningT.showshort (Getactivity (), getString (r.string.sign_in_success)); Appsettings.setprefstring (Getactivity (), Config.issignin, mcurrentdate); Setbuttonstate ();//Change the state of the buttonDialogutils.progone ();//Mcontrol.closeblue ();//Turn off Bluetooth} } };
Baidu down, the online approach is as follows:
Looper.prepare();BluetoothAdapter.getDefaultAdapter();
Before acquiring the Bluetoothadapter, add Looper.prepare (), and then the egg. Ask a lot of people, all say do not know, at this time this can self-reliance. Finally I found a solution.
Idea: I looked at the source code of the SDK, found that I call the toast again, the creation of handler, the source is as follows,
When I looked at the TN class, I found the problem.
The class TN is a aidl. Stubs, and the display and concealment of toasts are communicated through this class, and the use of handler in the inside, the specific principle I will not go into the drill down. I then assumed that the onlescan of new Bluetoothadapter.lescancallback () might not be running in the main thread, so I called the Runonuithread () method, and the result was solved. The modified code is as follows:
//Device scan callback. PrivateBluetoothadapter.lescancallback Mlescancallback =NewBluetoothadapter.lescancallback () {@Override Public void Onlescan(FinalBluetoothdevice device,intRssibyte[] Scanrecord) {FinalIbeaconclass.ibeacon IBeacon = Ibeaconclass.fromscandata (device, Rssi, Scanrecord);if(Mfilter.isdevice (ibeacon)) {Scanledevice (false);//Stop scanningGetactivity (). Runonuithread (NewRunnable () {//ui The control of a thread changes state, this method needs to be called, or it may not be valid @Override Public void Run() {T.showshort (Getactivity (), getString (r.string.sign_in_success)); Appsettings.setprefstring (Getactivity (), Config.issignin, mcurrentdate); Setbuttonstate (); Dialogutils.progone ();//Mcontrol.closeblue ();//Turn off Bluetooth} }); } } };
To this, the problem has been resolved, in order to let other colleagues encounter this problem, while reducing the time to find bugs, close up this blog, tell!
Copyright NOTICE: This article is the original blogger article, reproduced please specify from: http://blog.csdn.net/kimyo8337373/article/category/2608037
Android Bluetooth development, reported Bluetoothadapter:can ' t create handler inside thread that have not called Looper.prepare