CPRO not always connecting first time. Second time usually works.

I have code to connect to a given CPRO sensor, by id. It works fine, except it does not always connect the first time (going to the failure() method of ConnectionStateHandler after trying for a couple seconds). However, if I cause it to retry immediately after failure, it nearly always connects the second time.

What could be the possible causes of this, and how could I make it more robust?

----------------------
private MetaWearBleService.LocalBinder metawear_binder;
private MetaWearBoard metawear_board; 
HashMap<String, BluetoothDevice> devices = new HashMap<String, BluetoothDevice>();
...
private void sensor_connect(String id) {
    final BluetoothDevice device = devices.get(id);
    metawear_board = metawear_binder.getMetaWearBoard(device);
    metawear_board.setConnectionStateHandler(new MetaWearBoard.ConnectionStateHandler() {
        @Override
        public void connected() {
            connected_device = device;
            log("Metawear board connected");
        }

        @Override
        public void disconnected() {
            connected_device = null;
            log("Metawear board disconnected");
        }

        @Override
        public void failure(int status, Throwable error) {
            connected_device = null;
            Log.e("Breather", "Metawear connection failure", error);
            // If I retry a single time here, it nearly always connects second time.
        }
    });
    log("Metawear board connecting...");
    metawear_board.connect();
}

Comments

  • edited April 2017
    • What Android device and OS are you using?
    • What is the status code returned to the failure callback?

    Try resetting the BT adapter and turning off the wifi adapter.  These types of issues vary from device to device so it's hard to say for sure what is the root cause.  If the board connects within a reasonable amount of time, then I wouldn't worry about something this trivial.
  • I have the same problem (Nexus 5).
    When I connects for the first time (the first time when my app is on) it works. 
    Then I disconnect, and when I try to connect again it fails for the first time and works for the second. 
    No exception is thrown.


  • That issue indicates that the device is still connected even after the disconnect method is called which doesn't seem like the same issue mentioned in this thread.
This discussion has been closed.