connectAsyc

Hello,

Our team is having problem in
establishing a connection with MetaWear boards after a long period of
inactivity using an Android application.



When the application we are
developing is launched for the first time the user is asked to associate
3 boards with his profile. During this "pairing" procedure, we scan the
BLE devices, retrieve the MAC addresses of
the boards selected by the user and store them locally on the Android
device. After that the application connects to the board without
performing a scan but using the MAC addresses saved before as you
described in the documentation:
// Other required imports
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;

public class MainActivity extends Activity implements ServiceConnection {
private final String MW_MAC_ADDRESS= "EC:2C:09:81:22:AC";
private MetaWearBoard board;

public void retrieveBoard() {
final BluetoothManager btManager=
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
final BluetoothDevice remoteDevice=
btManager.getAdapter().getRemoteDevice(MW_MAC_ADDRESS);

// Create a MetaWear board object for the Bluetooth Device
board= serviceBinder.getMetaWearBoard(remoteDevice);
}
}
The aforementioned procedure
works perfectly the first time, when we previously made a scan to find
the devices. The problem we are seeing is that the boards do not
successfully connect after a long period of inactivity
(e.g. the next day); connectAsyc always fails. If we perform a BLE scan
before attempting to connect to the boards everything comes back to work
perfectly.

Currently, in order to get
around the problem, we perform a BLE scan of 5 seconds when the
application is started. I would like to know if this is the correct way
to handle the situation or if there is another solution
to get the result; the scan needs quite a lot of time (we tried also
with shorter scan periods but sometimes is not enough).



Why the boards do not
respond? Do they automatically enter some sort of standby mode after a
long period of inactivity? Is there a way to understand if a board has
entered this state?

Thanks






Comments

    • What is the error returned from connectAsync?
    • If you simply try to connect again after the first call fails, without doing a scan, does it eventually succeed?
    • What are the steps to reproduce this issue?
    • What Android devices and OSes are you testing with?

    No, the boards do not stop responding after a period of inactivity.  My guess is it is probably just a quirk with your device's BT stack and can be handled by scanning for the device rather than calling getRemoteDevice.
  • Hello Eric, I'm a team mate of lmoro.

    Unfortunately I've already started working with the sensors today so I'm not able to reproduce the problem right now and sending you the logcat. I'll do it tomorrow morning. I have to check the error returned by connectAsync.

    The application automatically tries for three times to reconnect without doing any scan. Each attempt fails.

    In our application we have created a class wich is in charge of managing the connection with a MetaWear sensor. The class has a method that tries to establish a connection to a sensor. If the connection fails, the procedure automatically tries to re-connect three times before give up and alert the system. Then we have a background Service holding all the MetaConnection objects.
    Finally we have created a class inheriting from HandlerThread which when run it connects with three MetaWear sensors using their mac addresses. If in the Service a MetaConnection object does not exist for a mac address, first the handler retrieve the BluetoothDevice using the method getRemoteDevice of the BluetoothAdapter. Then using the BtLEService retrieve a MetaWearBoard (getMetaWearBoard), creates a new MetaConnection passing the retrieved board and tries to connect.

    We are developing the application using a Samsung Galaxy Tab S2 running Android Nougat (7.0). We are using the 3.3.0 version of the Android SDK.

  • Hello Eric, this is the logcat with the errors (I have to make more than on post cause it is saying that the message is too long)

    I/AMLTask: The board E2:56:E9:E6:7E:DB must be loaded first
    I/AMLTask: retrieved bth device E2:56:E9:E6:7E:DB
    D/BluetoothGatt: connect() - device: E2:56:E9:E6:7E:DB, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=d2e2987f-3d6d-4e23-bc6b-34fcf3025c93
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
    I/AMLTask: The board F6:62:85:58:9C:42 must be loaded first
    I/AMLTask: retrieved bth device F6:62:85:58:9C:42
    D/BluetoothGatt: connect() - device: F6:62:85:58:9C:42, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=bee102ff-d066-4d24-930f-16b58fbb330b
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
    I/AMLTask: The board E1:DA:57:F1:3B:27 must be loaded first
    I/AMLTask: retrieved bth device E1:DA:57:F1:3B:27
    D/BluetoothGatt: connect() - device: E1:DA:57:F1:3B:27, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=00061090-1916-4349-a439-565bedfa0348
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=E2:56:E9:E6:7E:DB
    D/BluetoothGatt: refresh() - device: E2:56:E9:E6:7E:DB
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=6
    W/System.err: java.lang.IllegalStateException: Non-zero onConnectionStateChange status (133)
    W/System.err:     at com.mbientlab.metawear.android.BtleService$1.onConnectionStateChange(BtleService.java:144)
    W/System.err:     at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:227)
    W/System.err:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
    W/System.err:     at android.os.Binder.execTransact(Binder.java:573)
    W/MetaConnection: Attempting to reconnect, remaining tries: 3
    D/BluetoothGatt: connect() - device: E2:56:E9:E6:7E:DB, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=d7abae2d-e357-4be8-9f5d-fc7a3fdde683
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=F6:62:85:58:9C:42
    D/BluetoothGatt: refresh() - device: F6:62:85:58:9C:42
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=8
    W/System.err: java.lang.IllegalStateException: Non-zero onConnectionStateChange status (133)
    W/System.err:     at com.mbientlab.metawear.android.BtleService$1.onConnectionStateChange(BtleService.java:144)
    W/System.err:     at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:227)
    W/System.err:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
    W/System.err:     at android.os.Binder.execTransact(Binder.java:573)
    W/MetaConnection: Attempting to reconnect, remaining tries: 3
  • D/BluetoothGatt: connect() - device: F6:62:85:58:9C:42, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=8a541d34-c92e-46ff-bc96-584a341551b8
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
    D/BluetoothGatt: cancelOpen() - device: E1:DA:57:F1:3B:27
    D/BluetoothGatt: cancelOpen() - device: E2:56:E9:E6:7E:DB
    D/BluetoothGatt: cancelOpen() - device: F6:62:85:58:9C:42
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=F6:62:85:58:9C:42
    D/BluetoothGatt: refresh() - device: F6:62:85:58:9C:42
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=8
    W/System.err: java.util.concurrent.TimeoutException: Failed to connect and discover services within 10000ms
    W/System.err:     at com.mbientlab.metawear.impl.platform.TimedTask.lambda$execute$0$TimedTask(TimedTask.java:35)
    W/System.err:     at com.mbientlab.metawear.impl.platform.TimedTask$$Lambda$0.then(Unknown Source)
    W/System.err:     at bolts.Task$14.run(Task.java:872)
    W/System.err:     at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
    W/System.err:     at bolts.Task.completeImmediately(Task.java:863)
    W/System.err:     at bolts.Task.access$000(Task.java:32)
    W/System.err:     at bolts.Task$10.then(Task.java:654)
    W/System.err:     at bolts.Task$10.then(Task.java:651)
    W/System.err:     at bolts.Task.runContinuations(Task.java:956)
    W/System.err:     at bolts.Task.trySetResult(Task.java:994)
    W/System.err:     at bolts.TaskCompletionSource.trySetResult(TaskCompletionSource.java:39)
    W/System.err:     at bolts.TaskCompletionSource.setResult(TaskCompletionSource.java:62)
    W/System.err:     at bolts.Task$6.then(Task.java:451)
    W/System.err:     at bolts.Task$6.then(Task.java:447)
    W/System.err:     at bolts.Task$14.run(Task.java:872)
    W/System.err:     at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
    W/System.err:     at bolts.Task.completeImmediately(Task.java:863)
    W/System.err:     at bolts.Task.access$000(Task.java:32)
    W/System.err:     at bolts.Task$10.then(Task.java:654)
    W/System.err:     at bolts.Task$10.then(Task.java:651)
    W/System.err:     at bolts.Task.runContinuations(Task.java:956)
    W/System.err:     at bolts.Task.trySetResult(Task.java:994)
    W/System.err:     at bolts.TaskCompletionSource.trySetResult(TaskCompletionSource.java:39)
    W/System.err:     at bolts.Task$1.run(Task.java:269)
    W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
    W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
    W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    W/System.err:     at java.lang.Thread.run(Thread.java:762)
    W/MetaConnection: Attempting to reconnect, remaining tries: 2
    D/BluetoothGatt: connect() - device: F6:62:85:58:9C:42, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=584dde6b-9dc6-44af-953e-3d9cd4795b33
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=F6:62:85:58:9C:42
    D/BluetoothGatt: refresh() - device: F6:62:85:58:9C:42
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=8
    W/System.err: java.lang.IllegalStateException: Non-zero onConnectionStateChange status (133)
    W/System.err:     at com.mbientlab.metawear.android.BtleService$1.onConnectionStateChange(BtleService.java:144)
    W/System.err:     at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:227)
    W/System.err:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
    W/System.err:     at android.os.Binder.execTransact(Binder.java:573)
    W/MetaConnection: Attempting to reconnect, remaining tries: 1
    D/BluetoothGatt: connect() - device: F6:62:85:58:9C:42, auto: false
    D/BluetoothAdapter: isSecureModeEnabled
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=cda067f0-70c3-4cd1-b517-d9ed07a383ec
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=8 device=F6:62:85:58:9C:42
    D/BluetoothGatt: refresh() - device: F6:62:85:58:9C:42
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=8
    W/System.err: java.lang.IllegalStateException: Non-zero onConnectionStateChange status (133)
    W/System.err:     at com.mbientlab.metawear.android.BtleService$1.onConnectionStateChange(BtleService.java:144)
    W/System.err:     at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:227)
    W/System.err:     at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
    W/System.err:     at android.os.Binder.execTransact(Binder.java:573)
    W/AMLTask: Connection fails
    W/AMLTask: temrinating MLTASK
  • Hello Eric, any update on this?
  • No, I don't think any further discussion is needed.  The 133 status is a generic gatt error code returned from Android's BT stack that plagues every Android device, and it sounds like you already have a working solution to this issue.
This discussion has been closed.