Check board connected state and (clean) disconnect

How can I check the connection status of the board in python? The MetaWear class has a _on_disconnect() function but it has no code inside. 

I would like to detect when a board gets disconnected and why, that is, difference between calling disconnect() and other reasons (out-of-range or battery depleted). 

Also, when I disconnect and reconnect again, the subscribed handlers are still there. Which is the preferrred way to cleanly disconnect? Do i have to call mbl_mw_metawearboard_tear_down before disconnect()?


  • I'm not aware of any way to register for disconnect events through the various Python BLE libraries.  This doesn't even seem to be possible at the BlueZ level.

    That depends on whether you care about the current state of the board or not.  
    • Don't care - reset the board, free unmanaged memory, discard MetaWear Python object
    • Preserve everything - unsubscribe from active signals
    • Preserve only sensor config - call tear_down, unsubscribe from active signals
  • Shouldn't the MetaWear object have sort of an is_connected property that checks if the btle connection is still open? The problem is that when the sensor gets out of range, it is automatically disconnected, and a call to disconnect from my side results in a NotConnectedError exception. 

    Regarding the cleanly disconnect: I don't quite understand the difference between the three states, but I guess I would go with the "don't care" option. Shouldn't I first free memory, then reset the board? What api calls have to be used for that?

    Thanks for your support.
  • edited November 2017
    As stated previously, it does not appear that you can register a handler for disconnect events with the various Python ble libraries. In other words, disconnects not initiated by the SDK go unnoticed.

    No, if you free the memory first, then you can no longer issue commands with that object.

    Freeing memory is discussed in the documentation.

    You can reset the board with "mbl_mw_debug_reset".
  • What about the debug_disconnect function? Some examples directly call the MetaWear.disconnect() method (, but others call mbl_mw_debug_disconnect. What about mbl_mw_metawearboard_tear_down?

    Looks there are different combinations of library functions that can be called, but I am sorry I cannot see clearly how to do it properly.

    Right now my disconnect sequence is:




    But maybe I should add before the self.c.disconnect() these two lines:


    Am I missing something?
  • All you need is mbl_mw_debug_reset then mbl_mw_metawearboard_free.   When you want to connect to the board again, create another MetaWear object.
  • Ok, understood. Thanks for your help. Now it seems to work better, but I will open another thread for a different problem.
This discussion has been closed.