Thursday 29 December 2022

Android Slow Bluetooth RFCOMM Transfer Rate with RN4678

We are experimenting with a bunch of new tablets, and every one we tried is having issues with slow transfer rates with the RN4678 board. We currently use the Lenovo M10 FHD Plus. We tried a few such as the Teclast M40S, Nokia T20, and Samsung Galaxy Tab A8. The first two had horrible transfer rates, while the latter was okay but not ideal. We cannot use the Lenovo M10 Plus 3rd Gen because the buttons are too close to the corner to use with our tablet holders.

Here is my code:

public void SendMessage(BluetoothSocket socket, String msg) {
    OutputStream outStream;
    try {
        outStream = BluetoothConnectionService.outputStream;
        outStream.write("S".getBytes());
        Thread.sleep(4000);
        processThread = true;
        mApp.running = true;
        BluetoothSocketListener bsl = new BluetoothSocketListener(socket,
                CollectingDetail.this);
        Thread messageListener = new Thread(bsl);

        messageListener.start();
        timer = new CounterClass(remaingTime, 1000);
        timer.start();
        bt_stop.setText("Stop");

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(bluetoothReceiver, filter);
        bluetoothReceiver.setRegistered(true);
    } catch (IOException | InterruptedException e) {
        Log.e("BLUETOOTH_COMMS", e.getMessage());

        connectSocket();
    } 
}

public static class BluetoothSocketListener implements Runnable {
    private final WeakReference<CollectingDetail> wrActivity;
    private BluetoothSocket socket;

    public BluetoothSocketListener(BluetoothSocket socket, CollectingDetail collectingDetail) {
        this.socket = socket;
        wrActivity = new WeakReference<CollectingDetail>(collectingDetail);
    }

    @Override
    public void run() {
        final CollectingDetail activity = wrActivity.get();
        if (activity != null) {
            activity.inStream = null;
            if (!Thread.currentThread().isInterrupted()) {
                int bufferSize = 512;
                byte[] buffer = new byte[bufferSize];
                Log.i("Bluetooth bytes", new String(buffer));
                activity.inStream = BluetoothConnectionService.inputStream;
                int availableBytes;
                int bytesRead = -1;
                String message = "";

                while (activity.processThread) {
                    message = "";
                    try {
                        availableBytes = activity.inStream.available();
                        if (availableBytes > 0) {
                            bytesRead = activity.inStream.read(buffer);
                            if (bytesRead != -1 && bytesRead < bufferSize) {
                                message = new String(buffer, 0, bytesRead);

                                if (activity.mainHandler != null) {
                                    activity.mainHandler.post(new MessagePoster(message, activity));

                                }


                            }
                        }
                    } 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;
                    }
                }
            }
        }
    }
}


public void seprateData(String message) {
    try {
        message = message.replaceAll("(\\r\\n|\\n|\\r)", ",");
        String[] a = message.split(",");
        boolean goodData = false;

        for (int i = 0; i < a.length; i++) {
            final String data = a[i];
            if (data.length() > 0 && !data.equals(" ")) {
                if (data.length() >= 10 && data.startsWith("5A")) {
                    al_sepratedMessageList.add(data);
                    goodData = true;

                }
            }
        }

        if (goodData) {
            calculation();
            if (ConnectThrough.equalsIgnoreCase("usb")) {
                UsbConnectionSerivce.sendMessage("K");
            } else {
                BluetoothConnectionService.sendMessage(socket, "K");
            }
        }

    } catch (Exception e) {
        Log.e("BiSym", "Error Parsing BiSym Data");
    }
}

Is there any way we can increase the transfer rate without changing the firmware? It appears others have faced similar problems, but none of the answers have a real solution. Could you please help me out. Thanks.



from Android Slow Bluetooth RFCOMM Transfer Rate with RN4678

No comments:

Post a Comment