Exemplo n.º 1
0
def ble_evt_handler(ble_event):
    global connection_handle
    global advertisement_timed_out
    try:
        if ble_event is None:
            print "Received empty ble_event"
            return

        evt_id = ble_event.header.evt_id

        if evt_id == ble_driver.BLE_GAP_EVT_CONNECTED:
            connection_handle = ble_event.evt.gap_evt.conn_handle
            print "Connected, connection handle 0x{:04X}".format(connection_handle)

        elif evt_id == ble_driver.BLE_GAP_EVT_DISCONNECTED:
            print "Disconnected"
            start_advertising()

        elif evt_id == ble_driver.BLE_GAP_EVT_TIMEOUT:
            print "Advertisement timed out"
            advertisement_timed_out = True

        elif evt_id == ble_driver.BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            print "Received Security Parameters request"
            ble_driver.sd_ble_gap_sec_params_reply(connection_handle,
                                                   ble_driver.BLE_GAP_SEC_STATUS_SUCCESS,
                                                   None, None)

        elif evt_id == ble_driver.BLE_GATTS_EVT_SYS_ATTR_MISSING:
            print "Received System Attribute Missing event"
            ble_driver.sd_ble_gatts_sys_attr_set(connection_handle, None, 0, 0)

        elif evt_id == ble_driver.BLE_GATTS_EVT_WRITE:
            write_evt = ble_event.evt.gatts_evt.params.write
            handle = write_evt.handle
            operation = write_evt.op
            offset = write_evt.offset
            length = write_evt.len
            data = write_evt.data

            data_list = util.uint8_array_to_list(data, length)

            print "Received Write event: Handle:{}, op:{}, offset:{}, len:{}, data:{}".format(
                handle, operation, offset, len, data_list)

        else:
            print "Received event with ID: 0x{0:02X}".format(evt_id)

    except Exception, ex:
        print "Exception: {}".format(str(ex))
        print traceback.extract_tb(sys.exc_info()[2])
Exemplo n.º 2
0
def ble_evt_handler(ble_event):
    global connection_handle
    global advertisement_timed_out
    try:
        if ble_event is None:
            print "Received empty ble_event"
            return

        evt_id = ble_event.header.evt_id

        if evt_id == ble_driver.BLE_GAP_EVT_CONNECTED:
            connection_handle = ble_event.evt.gap_evt.conn_handle
            print "Connected, connection handle 0x{:04X}".format(connection_handle)

        elif evt_id == ble_driver.BLE_GAP_EVT_DISCONNECTED:
            print "Disconnected"
            start_advertising()
            connection_handle = ble_driver.BLE_CONN_HANDLE_INVALID

        elif evt_id == ble_driver.BLE_GAP_EVT_TIMEOUT:
            print "Advertisement timed out"
            advertisement_timed_out = True

        elif evt_id == ble_driver.BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            ble_driver.sd_ble_gap_sec_params_reply(connection_handle,
                                                   ble_driver.BLE_GAP_SEC_STATUS_SUCCESS,
                                                   None, None)

        elif evt_id == ble_driver.BLE_GATTS_EVT_SYS_ATTR_MISSING:
            ble_driver.sd_ble_gatts_sys_attr_set(connection_handle, None, 0, 0)

        elif evt_id == ble_driver.BLE_GATTS_EVT_WRITE:
            on_write(ble_event.evt.gatts_evt)

        else:
            print "Received event with ID: {}".format(evt_id)

    except Exception, ex:
        print "Exception: {}".format(str(ex))
        print traceback.extract_tb(sys.exc_info()[2])
Exemplo n.º 3
0
def ble_evt_handler(ble_event):
    global connection_handle
    global advertisement_timed_out
    try:
        if ble_event is None:
            print "Received empty ble_event"
            return

        evt_id = ble_event.header.evt_id

        if evt_id == ble_driver.BLE_GAP_EVT_CONNECTED:
            connection_handle = ble_event.evt.gap_evt.conn_handle
            print "Connected, connection handle 0x{:04X}".format(connection_handle)

        elif evt_id == ble_driver.BLE_GAP_EVT_DISCONNECTED:
            print "Disconnected"
            start_advertising()
            connection_handle = ble_driver.BLE_CONN_HANDLE_INVALID

        elif evt_id == ble_driver.BLE_GAP_EVT_TIMEOUT:
            print "Advertisement timed out"
            advertisement_timed_out = True

        elif evt_id == ble_driver.BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            ble_driver.sd_ble_gap_sec_params_reply(connection_handle, ble_driver.BLE_GAP_SEC_STATUS_SUCCESS, None, None)

        elif evt_id == ble_driver.BLE_GATTS_EVT_SYS_ATTR_MISSING:
            ble_driver.sd_ble_gatts_sys_attr_set(connection_handle, None, 0, 0)

        elif evt_id == ble_driver.BLE_GATTS_EVT_WRITE:
            on_write(ble_event.evt.gatts_evt)

        else:
            print "Received event with ID: {}".format(evt_id)

    except Exception, ex:
        print "Exception: {}".format(str(ex))
        print traceback.extract_tb(sys.exc_info()[2])
Exemplo n.º 4
0
def on_sys_attr_missing(gatts_event):
    ble_driver.sd_ble_gatts_sys_attr_set(peripheral_connection_handle, None, 0,
                                         0)
Exemplo n.º 5
0
def on_sys_attr_missing(gatts_event):
    ble_driver.sd_ble_gatts_sys_attr_set(peripheral_connection_handle, None, 0, 0)