Wednesday, 23 September 2020

Android - Issue collecting data with two Bluetooth devices on specific Android devices

I have an Android app that connects between two different Bluetooth devices via RFCOMM. The two Android devices I am using are a Lenovo M10 HD and a Dragon Touch Max10. Now, there is an issue that is specific to the Dragon Touch Max10 that seems to be occurring when running the two Bluetooth devices simultaneously. Apparently, the Dragon Touch Max10 seems to stop communication with the second Bluetooth device after running for a few seconds.

Here is my code to communicate with the second device:

private static class BluetoothSocketListener implements Runnable {
    private BluetoothSocket socket;

    private Handler handler;

    private final WeakReference<TherapistActivity> wrActivity;

    BluetoothSocketListener(BluetoothSocket socket, Handler handler, TherapistActivity activity) {
        wrActivity = new WeakReference<TherapistActivity>(activity);
        this.socket = socket;
        this.handler = handler;
    }

    @Override
    public void run() {
        TherapistActivity activity = wrActivity.get();
        if (activity != null) {
            activity.inStream = null;
            if (!Thread.currentThread().isInterrupted()) {
                int bufferSize = 1024;
                byte[] buffer = new byte[bufferSize];
                Log.i("Bluetooth bytes", new String(buffer));
                activity.inStream = BluetoothConnectionService.inStream;

                int bytesRead = -1;
                String message = "";

                while (activity.processThread) {
                    message = "";
                    try {
                        bytesRead = activity.inStream.read(buffer);
                    } catch (IOException e) {
                        Log.e("BLUETOOTH_COMMS", "Error reading bytes");
                        try {
                            socket.close();
                        } catch (IOException e1) {
                            Log.e("BLUETOOTH_COMMS", "Could not close socket");
                        }
                        activity.processThread = false;
                    }
                    if (bytesRead != -1) {
                        message = new String(buffer, 0, bytesRead);
                        handler.post(new MessagePoster(message, activity));
                    }
                }
            }
        }
    }
}

private static class MessagePoster implements Runnable {
    private String message;
    private final WeakReference<TherapistActivity> wrActivity;

    MessagePoster(String message, TherapistActivity activity) {
        this.message = message;
        wrActivity = new WeakReference<TherapistActivity>(activity);
    }

    @Override
    public void run() {
        final TherapistActivity activity = wrActivity.get();
        if (activity != null) {
            activity.seprateData(message);
        }
    }
}

private void seprateData(String message) {
    try {
        message = message.replaceAll("(\\r\\n|\\n|\\r)", ",");
        String[] a = message.split(",");
        for (String data : a) {
            if (data.length() > 0 && !data.equals(" ")) {
                if (data.length() >= 10 && data.startsWith("5A")) {
                    al_sepratedMessageList.add(data);
                } else {
                    if (ConnectThrough.equalsIgnoreCase("bt")) {
                        if (data.length() <= 5) {
                            char a1 = data.charAt(data.length() - 1);
                            if (Character.isDigit(a1) || Character.isLetter(a1)) {
                                breakSignal.append(data);
                            } else {
                                breakSignal.setLength(0);
                                breakSignal.setLength(10);
                            }
                        } else {
                            breakSignal.append(data);
                        }
                        String s = breakSignal.toString();
                        if (s.length() >= 10
                                && s.startsWith("5A")) {
                            al_sepratedMessageList.add(s);
                            breakSignal.setLength(0);
                            breakSignal.setLength(10);
                        }
                    }
                }
            }
        }
        biSymCalculation();
        if (ConnectThrough.equalsIgnoreCase("usb")) {
            UsbConnectionSerivce.sendMessage("K");
        } else {
            BluetoothConnectionService.sendMessage(mApp.biSymSocket, "K");  //keep alive command
        }
    } catch (Exception e) {
        Log.e("Error", "Error parsing data!");
    }
}

Apparently, the Dragon Touch Max10 seems to have an issue handling two simultaneous RFCOMM connections. The Lenovo M10 HD does not have this issue. Is there any way to get around this?

EDIT - it appears the Dragon Touch Max10 throws an IOException after a few seconds. Does anyone know why this would happen?

EDIT 2 - Just tried another tablet, the Lenovo M10 FHD Plus, no issue either.



from Android - Issue collecting data with two Bluetooth devices on specific Android devices

No comments:

Post a Comment