void onButtonA(MicroBitEvent) { if (advertising == 1) { return; } startAdvertising(); }
ble_error_t Gap::startAdvertising(void) { ble_error_t rc; if ((rc = startAdvertising(_advParams)) == BLE_ERROR_NONE) { state.advertising = 1; } return rc; }
int main() { // Initialise the micro:bit runtime. uBit.init(); uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); startAdvertising(); release_fiber(); }
void AppInit(sleep_state last_sleep_state) { /* set all PIOs to inputs and pull them down */ PioSetModes(0xFFFFFFFFUL, pio_mode_user); PioSetDirs(0xFFFFFFFFUL, FALSE); PioSetPullModes(0xFFFFFFFFUL, pio_mode_strong_pull_down); /* disable wake up on UART RX */ SleepWakeOnUartRX(FALSE); /* pull down the I2C lines */ PioSetI2CPullMode(pio_i2c_pull_mode_strong_pull_down); /* Start advertising */ startAdvertising(); }
/** * @brief Handle a GATT Server Event. * * @param [in] event * @param [in] gatts_if * @param [in] param * */ void BLEServer::handleGATTServerEvent( esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param) { ESP_LOGD(LOG_TAG, ">> handleGATTServerEvent: %s", BLEUtils::gattServerEventTypeToString(event).c_str()); // Invoke the handler for every Service we have. m_serviceMap.handleGATTServerEvent(event, gatts_if, param); switch(event) { // ESP_GATTS_CONNECT_EVT // connect: // - uint16_t conn_id // - esp_bd_addr_t remote_bda // - bool is_connected case ESP_GATTS_CONNECT_EVT: { m_connId = param->connect.conn_id; // Save the connection id. if (m_pServerCallbacks != nullptr) { m_pServerCallbacks->onConnect(this); } m_connectedCount++; break; } // ESP_GATTS_CONNECT_EVT // ESP_GATTS_REG_EVT // reg: // - esp_gatt_status_t status // - uint16_t app_id case ESP_GATTS_REG_EVT: { m_gatts_if = gatts_if; m_semaphoreRegisterAppEvt.give(); break; } // ESP_GATTS_REG_EVT // ESP_GATTS_CREATE_EVT // Called when a new service is registered as having been created. // // create: // * esp_gatt_status_t status // * uint16_t service_handle // * esp_gatt_srvc_id_t service_id // case ESP_GATTS_CREATE_EVT: { BLEService* pService = m_serviceMap.getByUUID(param->create.service_id.id.uuid); m_serviceMap.setByHandle(param->create.service_handle, pService); //pService->setHandle(param->create.service_handle); m_semaphoreCreateEvt.give(); break; } // ESP_GATTS_CREATE_EVT // ESP_GATTS_READ_EVT - A request to read the value of a characteristic has arrived. // // read: // - uint16_t conn_id // - uint32_t trans_id // - esp_bd_addr_t bda // - uint16_t handle // - uint16_t offset // - bool is_long // - bool need_rsp // case ESP_GATTS_READ_EVT: { break; } // ESP_GATTS_READ_EVT // ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived. // // write: // - uint16_t conn_id // - uint16_t trans_id // - esp_bd_addr_t bda // - uint16_t handle // - uint16_t offset // - bool need_rsp // - bool is_prep // - uint16_t len // - uint8_t *value case ESP_GATTS_WRITE_EVT: { break; } // ESP_GATTS_DISCONNECT_EVT // If we receive a disconnect event then invoke the callback for disconnects (if one is present). // we also want to start advertising again. case ESP_GATTS_DISCONNECT_EVT: { m_connectedCount--; if (m_pServerCallbacks != nullptr) { m_pServerCallbacks->onDisconnect(this); } startAdvertising(); break; } // ESP_GATTS_DISCONNECT_EVT // ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service. // add_char: // - esp_gatt_status_t status // - uint16_t attr_handle // - uint16_t service_handle // - esp_bt_uuid_t char_uuid case ESP_GATTS_ADD_CHAR_EVT: { break; } // ESP_GATTS_ADD_CHAR_EVT default: { break; } } ESP_LOGD(LOG_TAG, "<< handleGATTServerEvent"); } // handleGATTServerEvent
/** * @brief Handle a GATT Server Event. * * @param [in] event * @param [in] gatts_if * @param [in] param * */ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t* param) { ESP_LOGD(LOG_TAG, ">> handleGATTServerEvent: %s", BLEUtils::gattServerEventTypeToString(event).c_str()); switch(event) { // ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service. // add_char: // - esp_gatt_status_t status // - uint16_t attr_handle // - uint16_t service_handle // - esp_bt_uuid_t char_uuid // case ESP_GATTS_ADD_CHAR_EVT: { break; } // ESP_GATTS_ADD_CHAR_EVT case ESP_GATTS_MTU_EVT: updatePeerMTU(param->mtu.conn_id, param->mtu.mtu); break; // ESP_GATTS_CONNECT_EVT // connect: // - uint16_t conn_id // - esp_bd_addr_t remote_bda // case ESP_GATTS_CONNECT_EVT: { m_connId = param->connect.conn_id; addPeerDevice((void*)this, false, m_connId); if (m_pServerCallbacks != nullptr) { m_pServerCallbacks->onConnect(this); m_pServerCallbacks->onConnect(this, param); } m_connectedCount++; // Increment the number of connected devices count. break; } // ESP_GATTS_CONNECT_EVT // ESP_GATTS_CREATE_EVT // Called when a new service is registered as having been created. // // create: // * esp_gatt_status_t status // * uint16_t service_handle // * esp_gatt_srvc_id_t service_id // case ESP_GATTS_CREATE_EVT: { BLEService* pService = m_serviceMap.getByUUID(param->create.service_id.id.uuid, param->create.service_id.id.inst_id); // <--- very big bug for multi services with the same uuid m_serviceMap.setByHandle(param->create.service_handle, pService); m_semaphoreCreateEvt.give(); break; } // ESP_GATTS_CREATE_EVT // ESP_GATTS_DISCONNECT_EVT // // disconnect // - uint16_t conn_id // - esp_bd_addr_t remote_bda // - esp_gatt_conn_reason_t reason // // If we receive a disconnect event then invoke the callback for disconnects (if one is present). // we also want to start advertising again. case ESP_GATTS_DISCONNECT_EVT: { m_connectedCount--; // Decrement the number of connected devices count. if (m_pServerCallbacks != nullptr) { // If we have callbacks, call now. m_pServerCallbacks->onDisconnect(this); } startAdvertising(); //- do this with some delay from the loop() removePeerDevice(param->disconnect.conn_id, false); break; } // ESP_GATTS_DISCONNECT_EVT // ESP_GATTS_READ_EVT - A request to read the value of a characteristic has arrived. // // read: // - uint16_t conn_id // - uint32_t trans_id // - esp_bd_addr_t bda // - uint16_t handle // - uint16_t offset // - bool is_long // - bool need_rsp // case ESP_GATTS_READ_EVT: { break; } // ESP_GATTS_READ_EVT // ESP_GATTS_REG_EVT // reg: // - esp_gatt_status_t status // - uint16_t app_id // case ESP_GATTS_REG_EVT: { m_gatts_if = gatts_if; m_semaphoreRegisterAppEvt.give(); // Unlock the mutex waiting for the registration of the app. break; } // ESP_GATTS_REG_EVT // ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived. // // write: // - uint16_t conn_id // - uint16_t trans_id // - esp_bd_addr_t bda // - uint16_t handle // - uint16_t offset // - bool need_rsp // - bool is_prep // - uint16_t len // - uint8_t* value // case ESP_GATTS_WRITE_EVT: { break; } case ESP_GATTS_OPEN_EVT: m_semaphoreOpenEvt.give(param->open.status); break; default: break; } // Invoke the handler for every Service we have. m_serviceMap.handleGATTServerEvent(event, gatts_if, param); ESP_LOGD(LOG_TAG, "<< handleGATTServerEvent"); } // handleGATTServerEvent