Android Bluetooth development, reported Bluetoothadapter:can ' t create handler inside thread that have not called Looper.prepare

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.