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
Do I need to obtain the correct UUID of the device.
Yes. You could use getUuids()
of your BluetoothDevice
which returns the supported features (UUIDs) of the remote device, or fetchUuidsWithSdp()
if fresh UUIDs are desired.
The while loop is running for only once.
It's because your code is throwing an error and your are handling this error and exit the loop
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) {
// THIS BREAK INTERRUPT YOUR WHILE-LOOP SENTENCE
break;
}
}
Here when the bytes are read it is returning as 0
Because you created a InputStream of a socket with It's not connected to anything (Because the UUID which you created to generate the socket doesn't exist in the device where you made your bluetooth connection)
EDIT
Other way to get uuids is config the broadcast receiver to get back the uuids:
//Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
//Set your other filters
registerReceiver(ActionFoundReceiver, filter); // Don't forget to unregister during onDestroy
And create your BroadcastRecievier to get the UUIDs:
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i=0; i<uuidExtra.length; i++) {
Log.i("TEST-UUIDS","Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
}
}
else if(BluetoothDevice.YOUR_OTHER_ACTION_1){
//YOUR CODE FOR ACTION 1
}
else if(BluetoothDevice.YOUR_OTHER_ACTION_2){
//YOUR CODE FOR ACTION 2
}
}
};
Note: Not every bluetooth device show their service uuids'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With