问题
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