2013-02-04 107 views
0

我正在开发基于BluetoothChat示例的android蓝牙游戏。 它似乎工作得很好,但每5 - 10尝试连接应用程序崩溃。看来,一些缓冲区溢出。我使用一个设备作为服务器,另一个作为客户端。客户端(HTC EVO 4G)与此跟踪崩溃:蓝牙聊天android连接/发现bug

02-04 21:11:54.139: DEBUG/BluetoothService(2048): start 
02-04 21:11:54.139: DEBUG/BluetoothService(2048): setState() 0 -> 1 
02-04 21:11:54.169: DEBUG/BluetoothService(130): new handle 9 
02-04 21:11:54.179: DEBUG/BluetoothService(2048): Socket Type: InsecureBEGIN mAcceptThreadThread[Thread-47,5,main] 
02-04 21:11:55.870: ERROR/BluetoothService.cpp(130): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 
02-04 21:11:55.890: DEBUG/BluetoothService(2048): connect to: 10:BF:48:E8:C1:26 
02-04 21:11:55.890: DEBUG/BluetoothService(2048): setState() 1 -> 2 
02-04 21:11:55.900: INFO/BluetoothService(2048): BEGIN mConnectThread SocketType:Insecure 
02-04 21:11:55.900: ERROR/BluetoothService.cpp(130): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 
02-04 21:11:57.222: DEBUG/BluetoothService(130): updateDeviceServiceChannelCache(10:BF:48:E8:C1:26) 
02-04 21:11:57.222: DEBUG/BluetoothService(130): uuid(system): 00001105-0000-1000-8000-00805f9b34fb -1 
02-04 21:11:57.232: DEBUG/BluetoothService(130): uuid(application): fa87c0d0-afac-11de-8a39-0800200c9a66 3 
02-04 21:11:57.232: DEBUG/BluetoothService(130): Making callback for fa87c0d0-afac-11de-8a39-0800200c9a66 with result 3 
02-04 21:12:05.910: ERROR/BluetoothService.cpp(130): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 
02-04 21:12:06.491: DEBUG/BluetoothService(2048): stop 
02-04 21:12:35.779: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.779: DEBUG/BluetoothService(130): Removing service record 8 for pid 2048 
02-04 21:12:35.789: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.789: DEBUG/BluetoothService(130): Removing service record 2 for pid 2048 
02-04 21:12:35.789: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.789: DEBUG/BluetoothService(130): Removing service record 1 for pid 2048 
02-04 21:12:35.799: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.799: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.799: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.799: DEBUG/BluetoothService(130): Removing service record 7 for pid 2048 
02-04 21:12:35.809: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.809: DEBUG/BluetoothService(130): Removing service record 4 for pid 2048 
02-04 21:12:35.819: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.819: DEBUG/BluetoothService(130): Removing service record 6 for pid 2048 
02-04 21:12:35.819: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.819: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.819: DEBUG/BluetoothService(130): Removing service record 5 for pid 2048 
02-04 21:12:35.829: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.829: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.829: DEBUG/BluetoothService(130): Removing service record 3 for pid 2048 
02-04 21:12:35.829: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.829: DEBUG/BluetoothService(130): Removing service record 9 for pid 2048 
02-04 21:12:35.839: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.839: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.839: DEBUG/BluetoothService(130): Tracked app 2048 died 
02-04 21:12:35.839: DEBUG/BluetoothService(130): Tracked app 2048 died 

而最糟糕的事情,我不能重新开始连接 - 直到我重新启动我的phone.It抛出:

02-04 21:15:11.381: DEBUG/BluetoothService(2322): start 
02-04 21:15:11.381: DEBUG/BluetoothService(2322): setState() 0 -> 1 
02-04 21:15:12.432: ERROR/BluetoothService(2322): Socket Type: Insecurelisten() failed 
     java.io.IOException: Bad file number 
     at android.bluetooth.BluetoothSocket.throwErrnoNative(Native Method) 
     at android.bluetooth.BluetoothAdapter.createNewRfcommSocketAndRecord(BluetoothAdapter.java:884) 
     at android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:820) 
     at com.insomniacpro.pillows.Bluetooth.BluetoothService$AcceptThread.<init>(BluetoothService.java:312) 
     at com.insomniacpro.pillows.Bluetooth.BluetoothService.start(BluetoothService.java:132) 
     at com.insomniacpro.pillows.engine.MultiGameActivity.onResume(MultiGameActivity.java:159) 
     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242) 
     at android.app.Activity.performResume(Activity.java:3904) 

有时服务器设备( Nexus 7)崩溃 - 我必须重新启动才能开始工作。 堆栈:

java.io.IOException: Error: -1 
    at android.bluetooth.BluetoothAdapter.createNewRfcommSocketAndRecord(BluetoothAdapter.java:1035) 
    at android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:951) 
    at com.insomniacpro.pillows.Bluetooth.BluetoothService$AcceptThread.<init>(BluetoothService.java:312) 
    at com.insomniacpro.pillows.Bluetooth.BluetoothService.start(BluetoothService.java:132) 
    at com.insomniacpro.pillows.engine.MultiGameActivity.onResume(MultiGameActivity.java:159) 
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185) 

Maybe BluetoothChat app doesnt closes sockets well, when connection is closed? 

任何帮助将是有益的!

回答

0

最后我结束了这样糟糕的解决方案:每次连接丢失或失败时调用onFinish()。这适合我的情况,但不适用于其他情况。在我看来,BluetoothChat中的connectionLost()函数是不完整的。它应该以另一种方式清除端口。

+1

即使我面对这种情况。你在onFinish()上做什么?你能分享一下吗? – Basher51