Bluetooth connection for Bluetooth Bluetooth returns zero

Our device sends data via Bluetooth. In the Android application, we must read this data.

I can establish a Bluetooth connection, and then I call Thread to establish a BluetoothSocket connection using BluetoothDevice. Here, when the bytes are read, it returns as 0 (zero). Also, the while loop only works once.

Also, the UUID that I used in the code below is the code for some pieces of Bluetooth code. Do I need to get the correct device UUID.

Please help someone help. If you give me a helpful 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 
+5
source share
1 answer

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:

 //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 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){ //YOUR CODE FOR ACTION 1 } else if(BluetoothDevice.YOUR_OTHER_ACTION_2){ //YOUR CODE FOR ACTION 2 } } }; 

Note. Not every bluetooth device shows its service uuids

+1
source

Source: https://habr.com/ru/post/1247953/


All Articles