当前位置: 动力学知识库 > 问答 > 编程问答 >

bluetooth - Android app not being able to connect to more than 2 devices

问题描述:

i am creating an application which connects 4 devices. one of them acts as a server and the 3 others as client. the server also acts as a client. it is a card game. i have given it 7 UUIDs. when i created the server, 2 of the 3 clients are able to connect but when the 3rd one joins, the app crashes. here is part of the logcat when the 3rd devices try to connect.

 05-12 15:41:55.453: E/ConnectionService(10793): getConnectedSocket : 503c7432-bc23-11de-8a39-0800200c9a66

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): IOException in getConnectedSocket

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): java.io.IOException: Service discovery failed

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at net.clc.bt.ConnectionService.getConnectedSocket(ConnectionService.java:191)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at net.clc.bt.ConnectionService.access$5(ConnectionService.java:185)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at net.clc.bt.ConnectionService$1.connect(ConnectionService.java:225)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at net.clc.bt.Connection.connect(Connection.java:191)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at net.dillen.satat.Satat.onActivityResult(Satat.java:308)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.Activity.dispatchActivityResult(Activity.java:3835)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.ActivityThread.deliverResults(ActivityThread.java:3332)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3378)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.ActivityThread.access$2700(ActivityThread.java:123)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1900)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.os.Handler.dispatchMessage(Handler.java:99)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.os.Looper.loop(Looper.java:123)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at android.app.ActivityThread.main(ActivityThread.java:4370)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at java.lang.reflect.Method.invokeNative(Native Method)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at java.lang.reflect.Method.invoke(Method.java:521)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

05-12 15:41:55.463: I/net.clc.bt.ConnectionService(10793): at dalvik.system.NativeStart.main(Native Method)

minSdkVersion="7"

device OS = 2.3.3 , 2.3.3, 2.1.1, 2.1.1

the problem does not seem to arise with the OS version.

网友答案:

I have no comment on the stack trace that you've provided, but the fact that the problem occurs when you try to add your third peer makes me suspicious that you are bumping into a core Bluetooth limitation. Let me explain.

Bluetooth comm takes place in piconets with 1 master (who sets the timing) and up to 7 slaves. For one device to participate in multiple piconets is tricky and inefficient because it has to jump back and forth between the clock cycles of the different piconets. In my experience, a device can only be a slave in 2 piconets, so it would fail when you try to enter a 3rd piconet (though it should fail more gracefully and informatively then this, of course).

The general solution is to designate one device as the piconet master and all the other devices should only connect to this one device. In practise, I've only tried this on systems that allow much more control then Android (where I can, for example, explicitly control master vs. slave) so I can't say how this will work on Android, but you should start with the rule of the thumb that the device that initiates the connection will be the master.

Or I could be over thinking the whole thing. I'm finding the Bluetooth stacks on Android to be very buggy so you might just have encountered a bug. BTW, I'm assuming that the four phones you tested it on are from more then one manufacturer?

分享给朋友:
您可能感兴趣的文章:
随机阅读: