Disconnecting followed by a new connection (same board)

Following the android tutorial and after looking at the sample app, I started to develop a simple android application for the MetaWear CPRO.

I'm using the Bletoolbox for the scan (as it looks to be what the sample app uses) and I'm able to connect to the board and change the view to the mainactivity where I have a layout for some simple tests. However I noticed that after using my disconnect button (which stops the sensors and does a teardown of the board) and returning to the scan activity I'm unable to reconnect to the board - it stay on the connecting message forever.

Any ideas what I might be doing wrong? Since this is my first experience with android development, it might be the case that I'm making an obvious mistake.

Is it ok to use an intent to switch to the scan activity after the disconnectAsync() ?

Thanks in advance for any help

Comments

  • Something else may have connected to the board.  What does Logcat display when you're in the connect loop and what is the error message shown when the connect task fails?

    You'll also want to delay the disconnect call as it is possible you close the connection before all commands are sent.

    Depends on what you want to do after disconnecting.  If you are intending to return to the previous activity, then you need to call finish from the current one.

    I suggest you take a look at the app template on our GitHub page for some pointers.
  • There's nothing else connected to the board.
    The Logcat displays the usual:
    connectGatt
    connectGatt: 0 

    However it seems to be stuck there (showing those messages multiple times) and never connects.
    I was following the template app to get an idea of how to do things, but I don't get what might be the problem.

    If I restart the app, the connection will work right away, so it is definitely a mistake on my part.
  • If definitely sounds like some other part of your app is connecting to that board first which results in the connect loop.  Try putting some log messages before all of the connectAsync method to see which ones are being called.
  • I actually only have 2 connectAsync in the scanner activity (in the connect and the reconnect attempt).

    The way I leave the other activity is by calling the disconnectAsync followed by finish, as you suggested. Do I need to setup anything else before returning to the scanner activity?

    Right now, after returning to the scanner activity and trying to connect to the same device, it tries to connect to it and then gets stuck on the reconnect forever.
  • edited April 2017
    Can you share your project?  I would like to see if i am getting the same behavior on my android devices.

    Also, do you see the same issues if you put your code into the starter app template?
  • Here is the project where I get the described error.

    I don't thing the problem is related with the android device I use, but with the code itself. It might even be a problem with the way I'm interpreting that some things work on android/java since this is my first time going for android development.

    Thanks so much for the help, btw.
  • Is anyone able to spot any glaring mistakes in the way I connect/disconnect or even how I transition between activies?
    The error still persists and I don't understand what's wrong.
  • edited April 2017
    Reworking the OnClickListener to this code chunk fixes the issue on my Nexus 6.
    disconnect.setOnClickListener(v -> {
        ledModule.stop(true);
    
        gyroModule.stop();
        gyroModule.angularVelocity().stop();
    
        accelModule.stop();
        accelModule.acceleration().stop();
    
        board.tearDown();
    
        board.getModule(Debug.class).disconnectAsync();
        finish();
    });
    
  • So, what does really solve the problem? Is it the use of the Debug.class in the getModule?
  • The root cause is mostly like that the connection was closed while commands were still being issued to the board which disturbed the internal BluetoothGatt state.  The disconnectAsync from the Debug module commands the board to close the connection which can be useful if you want to issue some commands beforehand.
  • Great, thanks so much for the help and the explanation!
This discussion has been closed.