Our device sends data via Bluetooth , In android app we need to read this data.
I am able to establish Bluetooth connection, and next I am calling a Thread to establish BluetoothSocket connection using BluetoothDevice. Here when the bytes are read it is returning as 0 (zero) Also 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.
Please can anyone help?.If you give me the useful answer,it will be highly appreciated.
//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