in src/ble/ble_uart_peripheral.py [0:0]
def _set_ble_connection_state(self):
'''
Provides the connection state of this BLE device. Is private as BluePy is not thread safe
and any request to this method while the thread has an active Rx/Tx proces causes an exception / error.
If a state change is detected will update via the self.ble_state_change_callback()
Don't call this function from outside of this thread, it is called in the thread loop
in a thread safe way and populates the self._conn_state variable. Access that via
get_connection_state() instead or is_connected().
'''
try:
previous_state = self._conn_state.copy()
if hasattr(self,'_ble_peripheral') and self._ble_peripheral:
state = self._ble_peripheral.getState()
self._conn_state = {'connection-state' : state, 'addr-type' : self._ble_peripheral.addrType}
else:
# Used 'disc' = disconnected to match getState() return from BluePy when its initilised
# https://github.com/IanHarvey/bluepy/blob/master/docs/peripheral.rst
self._conn_state = {'connection-state' : 'disc', 'addr-type' : 'N/A'}
except Exception as err:
self._conn_state = {'connection-state' : 'error', 'state-request-error' : str(err)}
finally:
# If connectivt=y state has changed, upfdate status callback.
if (previous_state != self._conn_state):
self.ble_state_change_callback(self.ble_mac, self._conn_state, previous_state)