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()?

Comments

  • 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.
    https://mbientlab.com/cppdocs/latest/mblmwmetawearboard.html#freeing-memory

    You can reset the board with "mbl_mw_debug_reset".
    http://mbientlab.com/docs/metawear/cpp/0/debug_8h.html#a7c08136c14e58170fe5c1cd8d43a26f1
  • What about the debug_disconnect function? Some examples directly call the MetaWear.disconnect() method (led.py), 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:

    libmetawear.mbl_mw_sensor_fusion_stop(self.c.board);
    libmetawear.mbl_mw_sensor_fusion_clear_enabled_mask(self.c.board)

    libmetawear.mbl_mw_acc_stop(self.c.board)
    libmetawear.mbl_mw_gyro_bmi160_stop(self.c.board)
    libmetawear.mbl_mw_acc_disable_acceleration_sampling(self.c.board)
    libmetawear.mbl_mw_gyro_bmi160_disable_rotation_sampling(self.c.board)
     libmetawear.mbl_mw_datasignal_unsubscribe(self.acc_signal)
    libmetawear.mbl_mw_datasignal_unsubscribe(self.gyro_signal)
    libmetawear.mbl_mw_datasignal_unsubscribe(self.quaternion_signal)
    libmetawear.mbl_mw_datasignal_unsubscribe(self.button_signal)
    libmetawear.mbl_mw_datasignal_unsubscribe(self.battery_signal)
    libmetawear.mbl_mw_led_stop_and_clear(self.c.board)

    self.c.disconnect()

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

    libmetawear.mbl_mw_debug_reset(self.c.board)
    libmetawear.mbl_mw_metawearboard_free(self.c.board)

    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.