Android Bluetooth: Software Caused Connection Abort

时光怂恿深爱的人放手 提交于 2020-01-21 13:37:53

问题


I am getting exception whenever I am trying to connect with android device to bluettoth enabled device, it is connecting and withina couple of a minute it is getting exception. To connect using BLuettoth device(Spp profile), I am using

Method m;
m = mmDevice.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });

tmp = (BluetoothSocket) m.invoke(mmDevice, 1); 

Using this code, I am able to to connect to the device. But at this line bytes = mmInStream.read(buffer), I am getting exception withina couple of a min. I can use

if(mmInStream.available() > 0) {
bytes = mmInStream.read(buffer);
} 

But if i put the condition here, it can't understand whether device is still connected or not with other device. Becuase exception will not be occurred. I couldn't find out the solution and tried for 2 days. But I couldn't found the solution.

Can you please help me?

Thanks in advance


回答1:


I had the same problem with a server which started after a bluetooth broadcast. The solution was to send a byte after the server established a connection via BluetoothServerSocket.accept(). The client reads after BluetoothSocket.connect() one byte. So both ends are in sync.



来源:https://stackoverflow.com/questions/12384833/android-bluetooth-software-caused-connection-abort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!