Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
src/BLE_EH.cpp@1:628e1249eaa1, 2019-03-01 (annotated)
- Committer:
- Condo2k4
- Date:
- Fri Mar 01 10:05:55 2019 +0000
- Revision:
- 1:628e1249eaa1
- Parent:
- 0:f43a2a24edc2
Minor clean up
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Condo2k4 | 0:f43a2a24edc2 | 1 | #include "BLE_EH.h" |
| Condo2k4 | 0:f43a2a24edc2 | 2 | |
| Condo2k4 | 0:f43a2a24edc2 | 3 | BLE_EH::BLE_EH(BLE &ble, events::EventQueue &event_queue) : |
| Condo2k4 | 0:f43a2a24edc2 | 4 | _ble(ble), |
| Condo2k4 | 0:f43a2a24edc2 | 5 | _battery_uuid(GattService::UUID_BATTERY_SERVICE), |
| Condo2k4 | 0:f43a2a24edc2 | 6 | _battery_service(ble, 50), |
| Condo2k4 | 0:f43a2a24edc2 | 7 | _device_information_service(ble, "MANUFACTURER", "MODEL", "SERIAL", "HARDWARE", "FIRMWARE", "SOFTWARE") |
| Condo2k4 | 0:f43a2a24edc2 | 8 | { |
| Condo2k4 | 0:f43a2a24edc2 | 9 | } |
| Condo2k4 | 0:f43a2a24edc2 | 10 | |
| Condo2k4 | 0:f43a2a24edc2 | 11 | void BLE_EH::start() { |
| Condo2k4 | 0:f43a2a24edc2 | 12 | _ble.gap().setEventHandler(this); |
| Condo2k4 | 0:f43a2a24edc2 | 13 | |
| Condo2k4 | 0:f43a2a24edc2 | 14 | _ble.init(this, &BLE_EH::on_init_complete); |
| Condo2k4 | 0:f43a2a24edc2 | 15 | } |
| Condo2k4 | 0:f43a2a24edc2 | 16 | |
| Condo2k4 | 0:f43a2a24edc2 | 17 | /** Callback triggered when the ble initialization process has finished */ |
| Condo2k4 | 0:f43a2a24edc2 | 18 | void BLE_EH::on_init_complete(BLE::InitializationCompleteCallbackContext *params) { |
| Condo2k4 | 0:f43a2a24edc2 | 19 | if (params->error != BLE_ERROR_NONE) { |
| Condo2k4 | 0:f43a2a24edc2 | 20 | //print_error(params->error, "Ble initialization failed."); |
| Condo2k4 | 0:f43a2a24edc2 | 21 | return; |
| Condo2k4 | 0:f43a2a24edc2 | 22 | } |
| Condo2k4 | 0:f43a2a24edc2 | 23 | start_advertising(); |
| Condo2k4 | 0:f43a2a24edc2 | 24 | } |
| Condo2k4 | 0:f43a2a24edc2 | 25 | |
| Condo2k4 | 0:f43a2a24edc2 | 26 | void BLE_EH::start_advertising() { |
| Condo2k4 | 0:f43a2a24edc2 | 27 | /* Create advertising parameters and payload */ |
| Condo2k4 | 0:f43a2a24edc2 | 28 | uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; |
| Condo2k4 | 0:f43a2a24edc2 | 29 | ble::AdvertisingDataBuilder _adv_data_builder(_adv_buffer); |
| Condo2k4 | 0:f43a2a24edc2 | 30 | |
| Condo2k4 | 0:f43a2a24edc2 | 31 | ble::AdvertisingParameters adv_parameters( |
| Condo2k4 | 0:f43a2a24edc2 | 32 | ble::advertising_type_t::CONNECTABLE_UNDIRECTED, |
| Condo2k4 | 0:f43a2a24edc2 | 33 | ble::adv_interval_t(ble::millisecond_t(1000)) |
| Condo2k4 | 0:f43a2a24edc2 | 34 | ); |
| Condo2k4 | 0:f43a2a24edc2 | 35 | |
| Condo2k4 | 0:f43a2a24edc2 | 36 | _adv_data_builder.setFlags(); |
| Condo2k4 | 0:f43a2a24edc2 | 37 | _adv_data_builder.setLocalServiceList(mbed::make_Span(&_battery_uuid, 1)); |
| Condo2k4 | 0:f43a2a24edc2 | 38 | _adv_data_builder.setName("DEVICE_NAME"); |
| Condo2k4 | 0:f43a2a24edc2 | 39 | |
| Condo2k4 | 0:f43a2a24edc2 | 40 | /* Setup advertising */ |
| Condo2k4 | 0:f43a2a24edc2 | 41 | |
| Condo2k4 | 0:f43a2a24edc2 | 42 | ble_error_t error = _ble.gap().setAdvertisingParameters( |
| Condo2k4 | 0:f43a2a24edc2 | 43 | ble::LEGACY_ADVERTISING_HANDLE, |
| Condo2k4 | 0:f43a2a24edc2 | 44 | adv_parameters |
| Condo2k4 | 0:f43a2a24edc2 | 45 | ); |
| Condo2k4 | 0:f43a2a24edc2 | 46 | |
| Condo2k4 | 0:f43a2a24edc2 | 47 | if (error) { |
| Condo2k4 | 0:f43a2a24edc2 | 48 | //print_error(error, "_ble.gap().setAdvertisingParameters() failed"); |
| Condo2k4 | 0:f43a2a24edc2 | 49 | return; |
| Condo2k4 | 0:f43a2a24edc2 | 50 | } |
| Condo2k4 | 0:f43a2a24edc2 | 51 | |
| Condo2k4 | 0:f43a2a24edc2 | 52 | error = _ble.gap().setAdvertisingPayload(ble::LEGACY_ADVERTISING_HANDLE, _adv_data_builder.getAdvertisingData()); |
| Condo2k4 | 0:f43a2a24edc2 | 53 | |
| Condo2k4 | 0:f43a2a24edc2 | 54 | if (error) { |
| Condo2k4 | 0:f43a2a24edc2 | 55 | //print_error(error, "_ble.gap().setAdvertisingPayload() failed"); |
| Condo2k4 | 0:f43a2a24edc2 | 56 | return; |
| Condo2k4 | 0:f43a2a24edc2 | 57 | } |
| Condo2k4 | 0:f43a2a24edc2 | 58 | |
| Condo2k4 | 0:f43a2a24edc2 | 59 | /* Start advertising */ |
| Condo2k4 | 0:f43a2a24edc2 | 60 | |
| Condo2k4 | 0:f43a2a24edc2 | 61 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
| Condo2k4 | 0:f43a2a24edc2 | 62 | |
| Condo2k4 | 0:f43a2a24edc2 | 63 | if (error) { |
| Condo2k4 | 0:f43a2a24edc2 | 64 | //print_error(error, "_ble.gap().startAdvertising() failed"); |
| Condo2k4 | 0:f43a2a24edc2 | 65 | return; |
| Condo2k4 | 0:f43a2a24edc2 | 66 | } |
| Condo2k4 | 0:f43a2a24edc2 | 67 | } |
| Condo2k4 | 0:f43a2a24edc2 | 68 | |
| Condo2k4 | 0:f43a2a24edc2 | 69 | void BLE_EH::setBatteryLevel(uint8_t level) { |
| Condo2k4 | 0:f43a2a24edc2 | 70 | _battery_service.updateBatteryLevel(level); |
| Condo2k4 | 0:f43a2a24edc2 | 71 | } |
| Condo2k4 | 0:f43a2a24edc2 | 72 | |
| Condo2k4 | 0:f43a2a24edc2 | 73 | /* Event handler */ |
| Condo2k4 | 0:f43a2a24edc2 | 74 | |
| Condo2k4 | 0:f43a2a24edc2 | 75 | void BLE_EH::onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { |
| Condo2k4 | 0:f43a2a24edc2 | 76 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
| Condo2k4 | 0:f43a2a24edc2 | 77 | } |