Do I need to get the correct device UUID.
Yes. You can use the getUuids() your BluetoothDevice , which returns the supported functions (UUIDs) of the remote device, or fetchUuidsWithSdp() if you need fresh UUIDs.
The while loop only works once.
This is because your code throws an error and you process 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 bytes are read, it returns as 0
Since you created an InputStream socket with which it is not connected to anything (since the UUID you created to create the socket does not exist on the device on which you established the connection with Bluetooth)
EDIT
Another way to get uuids is to configure the broadcast receiver to return uuids:
And create your BroadcastRecievier to get the UUID:
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){
Note. Not every bluetooth device shows its service uuids