简体   繁体   中英

Reading from Bluetooth socket

I have a problem with reading from Bluetooth socket. I create the socket:

private OutputStream outputStream;
private InputStream inStream;
socket=device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream = socket.getOutputStream();
inStream = socket.getInputStream();

After connection is established I send some instructions to the Bluetooth device after which I need to get an answer back(answer is always the same 5 bytes). But from time to time when I do this:

byte[] packetBytes = new byte[5];
inStream.read(packetBytes);

the app freezes. I tried inputstream.available and used it with previous devices, but it would always return 0 here. And I tried waiting before reading but still nothing. When I try those same commands on PC I get an answer.

For anybody that is having the same problem as me. For now the solution that I am using is a new Thread that constantly reads from an input socket with inStream.read(packetBytes);

When something is read we just send it to our main Thread to be processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM