I finally figured out what the problem was. The problem is in BluetoothAdapter.getDefaultAdapter(). This method create implicitely a Handler connected to the looper of the current thread. And this method is static and returns a singleton.
This mean that the first call, and only the first call, must be done on a thread that will call Looper.loop() and not just exit ! In BluetoothChat sample, the adapter is created in onCreate() of the activity : // Get local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // If the adapter is null, then Bluetooth is not supported if (mBluetoothAdapter == null) { ... } According to comments, we may think taht this is just to check if a bluetoothAdapter exists but in fact, if it is not done here (but for example on AcceptThread), it will not work at all ! So I think that 1/ the implicit usage of the first thread calling a method that return a singleton is a really bad design 2/ Bluetooth chat sample should maybe have a comment added 3/ BluetoothAdapter.getDefaultAdapter() should maybe start its own thread. Maybe something like : private BluetoothAdapter _btAdapter = null; public synchronized BluetoothAdapter getAdapter() { if (_btAdapter==null) { final boolean[] ok = new boolean[] { false }; Thread t = new Thread() { public void run() { Looper.prepare(); _btAdapter = BluetoothAdapter.getDefaultAdapter(); synchronized (ok) { ok[0]=true; ok.notify(); } Looper.loop(); }; }; t.setDaemon(true); t.setName("bt adapter"); t.start(); synchronized (ok) { if (ok[0]==false) { try { ok.wait(); } catch (InterruptedException e) { } } } } return _btAdapter; } (this is what I've finally done to correct ...) my 2 cents ... On Apr 26, 9:53 pm, mbaroukh <mike.baro...@gmail.com> wrote: > Hi all. > > I'm facing a problem I don't find how to handle. > > I'm doing an application that listen forbluetoothincomming > connection. > As recommanded, > - I have a BluetoothServerSocket that call > listenUsingRfcommWithServiceRecord(() > - I'm asking for the BluetoothSocket with accept() method of > BluetoothServerSocket. > - when a connection is received, the BluetoothServerSocket is closed. > > First, notice > thathttp://developer.android.com/guide/topics/wireless/bluetooth.html > says on "Connecting as a server" section : > "Unless you want to accept additional connections, call > [BluetoothServerSocket] close(). " > > that's what I've done but, unless I'm wrong, the chat > Samplehttp://developer.android.com/resources/samples/BluetoothChat/src/com/... > does not do it although it accept only one connection ... > > So the problem is that when listenUsingRfcommWithServiceRecord() is > called again, the channel change. > But clients continues to think that the service is on the previous > channel. > The problem is not on client side because just stopping/startingbluetoothon > the android server make the client find the new channel > for the service. > > For now, all I can do is not closing BluetoothServerSocket but if the > client reconnect quicker than the server calling accept(), the client > is connected on ... nothing. the call to accept will not return this > connection. > > Does anybody figure out the same problem or have any advice ? > > Thanks for any help ! > > Mike -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en