How to make a Bluetooth SPP connection process more reliable?
Asked Answered
P

1

11

We are about to release the new version of our software, and for the version afterward, our goal is to make the connection process for our Bluetooth SPP connections more reliable. We use the RN42 module in our products, and currently, at times it may take more than one try to connect to our boards.

Here is my current code:

class ConnectThread extends Thread {
    BluetoothDevice mDevice;

    public ConnectThread(BluetoothDevice device) throws SecurityException, NoSuchMethodException {
        mDevice = device;
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
        try {
            btSocket = mDevice.createInsecureRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Log.e("Error", "Could not create socket!");
        }
    }

    public void cancel() {
        interrupt();
        try {
            Log.i("Treadmill", "in connect thread cancellation");
            btSocket.close();
        } catch (IOException localIOException) {
            Log.e("Treadmill", "exception + " + localIOException.getMessage());
        }
    }

    public void run() {
        btAdapter.cancelDiscovery();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Log.e("whatever", "InterruptedException: " + e.getMessage(), e);
        }
        try {
            btSocket.connect();
            Log.i("Treadmill", "After Connect");
        } catch (IOException ioe) {
            Log.i("Treadmill", "Trying Fallback");
            try {
                Method m;
                try {
                    btSocket.close();
                    m = mDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[]{int.class});
                    btSocket = (BluetoothSocket) m.invoke(mDevice, 1);
                    Thread.sleep(500);
                    btSocket.connect();
                } catch (IllegalArgumentException e) {
                    Log.e("whatever", "IllegalArgumentException: " + e.getMessage(), e);
                } catch (IllegalAccessException e) {
                    Log.e("whatever", "IllegalAccessException: " + e.getMessage(), e);
                } catch (InvocationTargetException e) {
                    Log.e("whatever", "InvocationTargetException: " + e.getMessage(), e);
                } catch (NoSuchMethodException e) {
                    Log.e("whatever", "NoSuchMethodException: " + e.getMessage(), e);
                } catch (InterruptedException e) {
                    Log.e("whatever", "InterruptedException: " + e.getMessage(), e);
                }

            } catch (IOException ioe2) {
                Log.e("Treadmill", "Failed to connect to Bluetooth device: " + ioe2.getMessage());
                eventHandler.obtainMessage(MESSAGE_ERRORCONNECT, 0, 0, getResources().getString(R.string.connerr) + ": " + ioe2.getMessage()).sendToTarget();
                try {
                    btSocket.close();
                } catch (IOException localIOException2) {
                    Log.e("Error", "IO Exception!");
                }
                return;
            }
        }
        eventHandler.obtainMessage(MESSAGE_CONNECT, 0, 0, "").sendToTarget();
        synchronized (this) {
            connectThread = null;
        }
        manageConnectedSocket(btSocket);
    }
}

Even with the fallback to reflection the connection intermittently fails on some devices. I get the following error:

find_rfc_slot_by_id unable to find RFCOMM slot id: XX (XX being a number that increments on each attempted connection).

followed by this: Failed to connect to Bluetooth device: read failed, socket might closed or timeout, read ret: -1

Does anyone know how to avoid these errors.

Interestingly, for comparison. I am testing on two tablets. One tablet, the Samsung Galaxy Tab 4 seems to work extremely well, while another, the Astro Tab A10, seems to be a bit more intermittent unless you wait several seconds between connecting and disconnecting.

Paraldehyde answered 26/4, 2018 at 15:49 Comment(0)
B
2

For more reliable connection means even app was closed, Bluetooth should be keep connected in the background. Below is the working solution I followed in my app to keep Bluetooth connection background. First create a class which extends service, because service runs in the background even app closed until you call stopService or stopSelf methods

while starting BluetoothService class pass Bluetooth Mac address to connect and run in the background. Sample code:

   @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent != null){
        String deviceg = intent.getStringExtra("bluetooth_device");

        if (deviceg != null){
            connectToDevice(deviceg);
        }

    }

    return START_STICKY;
}

Below is the connect to device method which identifies mac Address into Bluetooth Device.

   public synchronized void connectToDevice(String macAddress){
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress);

    if (mConnectedThread != null){
        mConnectedThread.cancel();
        mConnectedThread = null;
    }
    mConnectThread = new ConnectBtThread(device);
    toast("connecting");
    mConnectThread.start();

}

This is my Thread class inside BluetoothService which runs in a separate thread Code:

 private class ConnectBtThread extends Thread{
    private final BluetoothSocket mSocket;
    private final BluetoothDevice mDevice;

    public ConnectBtThread(BluetoothDevice device){
        mDevice = device;
        BluetoothSocket socket = null;
        try {
            socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(B_UUID));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mSocket = socket;

    }

    @Override
    public void run() {
        if (mBluetoothAdapter.isDiscovering()){
            mBluetoothAdapter.cancelDiscovery();
        }

        try {
            mSocket.connect();
            Log.d("service","Bluetooth one running (connected)");

        } catch (IOException e) {

            try {
                mSocket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        connected(mSocket);

    }

    public void cancel(){

        try {
            mSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

It works perfectly fine for our app. If you want to access service methods bind this service to your activity

Burgos answered 8/5, 2018 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.