Comments on: BluetoothAdapter.getDefaultAdapter() throws RuntimeException: Can’t create handler inside thread that has not called Looper.prepare() https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/ After eliminating all other possibilities, the one remaining-no matter how unlikely-must be the truth. Wed, 28 Dec 2016 17:08:53 +0000 hourly 1 https://wordpress.org/?v=7.1.4 By: Krunk https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-576695 Wed, 28 Dec 2016 17:08:53 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-576695 In reply to DD.

I haven’t touched this code in a while, but my guess is that you’re calling Looper.prepare() multiple times in the same thread.

My suggestion here would be for you to only call Looper.prepare() once, remember that you called it, and don’t call it on subsequent calls to BluetoothAdapter.getAdapter().

]]>
By: DD https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-576684 Mon, 26 Dec 2016 07:50:21 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-576684 Hi,

I am having the same problem for devices of API Level 18.
Even if I put Looper.prepare() before the BluetoothAdapter.getAdapter(), I get the error saying

W/System.err: java.lang.RuntimeException: Unable to start receiver sos.android.blesos.receivers.ScanReceiver: java.lang.RuntimeException: Only one Looper may be created per thread

which means there is a Looper already started.

But without the Looper.prepare() statement, I get the

W/BluetoothAdapter Unhandled exception: Runtime Exception can’t create handler inside thread that has not called looper.prepare() android

]]>
By: Martin https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569923 Tue, 28 May 2013 07:25:05 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569923 Hi HelderSi,

if you are having exactly the same problem as I did, the following two lines of code should help:

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

– meaning that (only the first time) before you call BluetoothAdapter.getDefaultAdapter() you must call Looper.prepare(). I think, it does not matter, how you do this.

– Either calling getDefaultAdapter() in the UI thread, because the UI thread calls Looper.prepare() after it starts automatically, so you do not have to do it any more
– or create a separate thread to do nothing else but call Looper.prepare() and getDefaultAdapter() and then finish.

The easiest way to test this is to put Looper.prepare() just before your getDefaultAdapter() call. If this does not work, I am afraid, I can not help 🙁

But I hope I could help 🙂
Martin

]]>
By: HelderSi https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569921 Mon, 27 May 2013 21:36:02 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569921 Hi, Martin.
I’m having the same problem. I follow your suggestion, but it does not work for me.
Please, may you give a example?
Thanks

]]>
By: Krunk https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569614 Mon, 22 Apr 2013 07:19:32 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569614 In reply to Krunk.

Ah. Gotcha.

]]>
By: Martin https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569613 Mon, 22 Apr 2013 07:16:26 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569613 In reply to Krunk.

Yes, you are right, if you already have a UI thread or any other thread, that has called Looper.prepare(), you can use it. I just wanted to clarify, that it does not have to be exactly an UI thread.

]]>
By: Krunk https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569612 Mon, 22 Apr 2013 07:07:49 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569612 In reply to Martin.

If you’re going to go and create a special thread just to initialize BluetoothAdapter.getDefaultAdapter();, is it really that different than initializing it on the UI thread.

I understand in the event your object doesn’t have access to the UI thread, your solution is a better workaround. But if you’re already inside an Activity or Application, why not take advantage that the looper has already been prepared?

In other words, if you’re going to spawn a new thread when the Application first starts up, why not just call BluetoothAdapter.getDefaultAdapter(); where you’re about to spawn that new thread and initialize BluetoothAdapter in the UI thread instead. Therefore you won’t have to spawn any new thread nor call Looper.prepare();

]]>
By: Martin https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569611 Mon, 22 Apr 2013 06:58:43 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569611 In reply to Krunk.

Yes, calling looper.prepare() every time would be the most ugly solution. But it seems to be enough to call getDefaultAdapter() once – for the first time in a thread that has called Looper.prepare(). And you do not always have a UI Thread (starting a service in a BOOT_COMPLETED receiver was my case)

So one (not so ugly) solution could be to create a special thread for it only for the first time. That thread wouldn’t do anything else than:

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

There is always a point (may be more, but not a lot), where the application starts and that is the place where I would put the “LooperBluetoothAdapterInitializingThread”.

I am not saying, that this is the only/best solution, it just works for me so far. But if there is a better way to do this, I would like to know it too.

Greetings
Martin

]]>
By: Krunk https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569610 Sun, 21 Apr 2013 22:21:13 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569610 In reply to Martin.

Are you suggesting that every time before you call BluetoothAdapter.getDefaultAdapter(), you should call Looper.prepare()? That seems way more impractical to me given that we only need to initialize BluetoothAdapter once in the UI thread vs having to call Looper.prepare() every single time we call BluetoothAdapter.getDefaultAdapter()… or did I misunderstand what you’re saying.

]]>
By: Martin https://www.krunk4ever.com/blog/2013/02/22/bluetoothadapter-getdefaultadapter-throws-runtimeexception-cant-create-handler-inside-thread-that-has-not-called-looper-prepare/comment-page-1/#comment-569609 Sun, 21 Apr 2013 15:29:44 +0000 http://www.krunk4ever.com/blog/?p=4388#comment-569609 Hi,
calling BluetoothAdapter.getDefaultAdapter() in the UI thread works, but is not very practical. Actually, I think, it works just because the UI thread calls Looper.prepare() by default.

So calling Looper.prepare() just before calling BluetoothAdapter.getDefaultAdapter() will solve the problem anywhere, not just in a UI thread. Works fine for me.

greetings

]]>