Problem connecting to MetaWear Motion R

Hi,

I have used the MetaWear starter app as a framework for my own code. When connecting to the MetaWear Motion R board it works perfect. So I've ported all the code relating to connecting via BLE to my own app, but every other time I connect I get the following error:

03-16 14:31:38.301 28712-28712/com.example.android.vpt E/MetaWear: Error connecting
                                                                   java.lang.RuntimeException: onConnectionStateChanged reported non-zero status: 133
                                                                       at com.mbientlab.metawear.MetaWearBleService$2$2.run(MetaWearBleService.java:1008)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

My code for connecting looks like this:
...
serviceBinder = (MetaWearBleService.LocalBinder) service;
serviceBinder.executeOnUiThread();
... 
[btDevice is returned in the same way as the in com.mbientlab.bletoolbox.scanner startBleScan method 
and seems to contain a valid object when it's debugged]
...
serviceBinder.clearCachedState(btDevice);
// Create a MetaWear board object for the Bluetooth Device
mMWBoard1 = serviceBinder.getMetaWearBoard(btDevice);
mMWBoard1.setConnectionStateHandler(stateHandler);
mMWBoard1.connect();
...
Any ideas for what is causing the error and how to solve it?

Comments

  • Are you disconnecting from the board before closing your app?
  • Yes I am, and I also tried to remove the disconnect board call from the MetaWear starter app, but it didn't change anything. Therefore I don't think the disconnect call has anything to do with the error.
    I have debugged a little more and it turns out that the MetaWear starter app actually also gives the error, but for some reason it doesn't show as detailed error information. This is a dump from the debugger running the code that "works":

    I/MetaWear: Just before call to setConnectionStateHandler
    I/MetaWear: Just before call to connect
    D/BluetoothGatt: connect() - device: DC:A8:5D:64:75:63, auto: false
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=ed085e1b-6105-4bad-8ae8-d9cfabb63b76
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
    D/mali_winsys: new_window_surface returns 0x3000,  [1592x1165]-format:1
    D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7device=DC:A8:5D:64:75:63
    D/BluetoothGatt: refresh() - device: DC:A8:5D:64:75:63
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=7
    D/BluetoothGatt: connect() - device: DC:A8:5D:64:75:63, auto: false
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=26f40a58-ef9d-4692-a18b-a0101d22bf20
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7device=DC:A8:5D:64:75:63
    D/BluetoothGatt: refresh() - device: DC:A8:5D:64:75:63
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=7
    D/BluetoothGatt: connect() - device: DC:A8:5D:64:75:63, auto: false
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=4d5d4481-b12c-42c7-bc5d-7e9cb0d0ccf3
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
    D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=DC:A8:5D:64:75:63
    D/BluetoothGatt: discoverServices() - device: DC:A8:5D:64:75:63
    D/BluetoothGatt: onClientConnParamsChanged() - Device=DC:A8:5D:64:75:63 interval=6 status=0
    D/BluetoothGatt: onSearchComplete() = Device=DC:A8:5D:64:75:63 Status=0
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: onClientConnParamsChanged() - Device=DC:A8:5D:64:75:63 interval=39 status=0
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 326a9006-85cb-9195-d9dd-464cfbbae75a enable: true
    I/MetaWear: Board is connected

    It turns out that the MetaWear starter app actually calls connect 2 times but get the same error (onClientConnectionState() - status=133) before it succeeds the third time! But something in the code immediately calls the connect after the error 


  • For comparison a debugger dump from my own app (which is a copy of the MetaWear code when it comes to connecting the MetaWear board to bluetooth) shows the same behavior, excect the connect call is not done automatically... maybe there is something I'm missing...:

    D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7device=DC:A8:5D:64:75:63
    D/BluetoothGatt: refresh() - device: DC:A8:5D:64:75:63
    D/BluetoothGatt: close()
    D/BluetoothGatt: unregisterApp() - mClientIf=7
    E/MetaWear: Error connecting  (this part is not shown in the MetaWear starter app)
                java.lang.RuntimeException: onConnectionStateChanged reported non-zero status: 133
                    at com.mbientlab.metawear.MetaWearBleService$2$2.run(MetaWearBleService.java:1008)
                    at android.os.Handler.handleCallback(Handler.java:739)
                    at android.os.Handler.dispatchMessage(Handler.java:95)
                    at android.os.Looper.loop(Looper.java:158)
                    at android.app.ActivityThread.main(ActivityThread.java:7229)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    I/MetaWear: Trying to connect to dumbbells again!!
    D/BluetoothGatt: connect() - device: DC:A8:5D:64:75:63, auto: false
    D/BluetoothGatt: registerApp()
    D/BluetoothGatt: registerApp() - UUID=a971cd7e-370c-41f0-ae13-e041c2c00f50
    D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7
    D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=DC:A8:5D:64:75:63
    D/BluetoothGatt: discoverServices() - device: DC:A8:5D:64:75:63
    D/BluetoothGatt: onClientConnParamsChanged() - Device=DC:A8:5D:64:75:63 interval=6 status=0
    D/BluetoothGatt: onSearchComplete() = Device=DC:A8:5D:64:75:63 Status=0
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
    D/BluetoothGatt: setCharacteristicNotification() - uuid: 326a9006-85cb-9195-d9dd-464cfbbae75a enable: true


  • I have traced the error to originate from the BlueToothGattCallBack in the MetaWearBleService.java file. 
    private final BluetoothGattCallback gattCallback= new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    final GattConnectionState state= gattConnectionStates.get(gatt.getDevice());

    switch (newState) {
    ...
    case BluetoothProfile.STATE_DISCONNECTED:
    mwBoards.get(gatt.getDevice()).close();
    handlerThreadPool.removeCallbacks(state.connectionTimeoutHandler);


    if (status != 0) {
    final int paramStatus= status;
    queueRunnable(new Runnable() {
    @Override
    public void run() {
    if (state.connectionHandler != null) {
    state.connectionHandler.failure(paramStatus, new RuntimeException("onConnectionStateChanged reported non-zero status: " + paramStatus));
    }

    For now I have made a workaround that sets a timer to wait 10 seconds and then checks if the connection has been established, but I would really like to know how the MetaWear Starter app does the automatic reconnection?

  • Hi kra

    I'm having the same problem - do you mind to share your code (the relevant part where you connect succesfully) ?

    Thanks a lot !!
  • Same here, I get lots of "Non-zero connection changed status (133)" or "Timed out establishing Bluetooth Connection".

    It is very frustrating, because I took the code from the example applications, but these work well.
  • @aribes
    Post the code you are currently running
This discussion has been closed.