- Forum posts: 1
Apr 27, 2016, 9:41:26 AM via Website
Apr 27, 2016 9:41:26 AM via Website
Our device sends data via Bluetooth , In android app we need to read this data.
I am able to establish Bluetooth connection, and then I am calling the a Thread to establish BluetoothSocket connection with BluetoothDevice. Here when the bytes are read it is returning as 0 (zero) and The while loop is running for only once.
Also the UUID that I have used in below code is from some Bluetooth Snippet code. Do I need to obtain the correct UUID of the device.
//Calling ConnectThread after Bluetooth is paired
public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
String name = device.getName();
Log.e("Device Name ", name);
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// mAdapter.cancelDiscovery();
try {
mmSocket.connect();
ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
} catch (IOException connectException) {
// try {
// mSocket.close();
// } catch (IOException closeException) { }
return;
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
// bytes += mmInStream.read(buffer, bytes, buffer.length
// - bytes);
if (mmSocket.isConnected()) {
Log.e("Socket is connected", "Socket is connected");
}
int numOfBytes = mmInStream.available();
Log.e("numOfBytes", String.valueOf(+numOfBytes));
bytes = mmInStream.read(buffer);
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
} catch (IOException e) {
break;
}
}
}
i am struck and unable to read the data from the Bluetooth
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.