This example creates and updates a standard Battery Level service containing a single GATT characteristic. The battery service transmits a device's battery level in percentage, with 100% being a fully charged battery and 0% being a fully drained battery. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-ble/tree/master/BLE_BatteryLevel
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2014 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <events/mbed_events.h> 00018 #include <mbed.h> 00019 #include "ble/BLE.h" 00020 #include "ble/Gap.h" 00021 #include "ble/services/BatteryService.h" 00022 #include "pretty_printer.h" 00023 00024 static DigitalOut led1(LED1, 1); 00025 00026 const static char DEVICE_NAME[] = "BATTERY"; 00027 00028 static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00029 00030 class BatteryDemo : ble::Gap::EventHandler { 00031 public: 00032 BatteryDemo(BLE &ble, events::EventQueue &event_queue) : 00033 _ble(ble), 00034 _event_queue(event_queue), 00035 _battery_uuid(GattService::UUID_BATTERY_SERVICE), 00036 _battery_level(50), 00037 _battery_service(ble, _battery_level), 00038 _adv_data_builder(_adv_buffer) { } 00039 00040 void start() { 00041 _ble.gap().setEventHandler(this); 00042 00043 _ble.init(this, &BatteryDemo::on_init_complete); 00044 00045 _event_queue.call_every(500, this, &BatteryDemo::blink); 00046 _event_queue.call_every(1000, this, &BatteryDemo::update_sensor_value); 00047 00048 _event_queue.dispatch_forever(); 00049 } 00050 00051 private: 00052 /** Callback triggered when the ble initialization process has finished */ 00053 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { 00054 if (params->error != BLE_ERROR_NONE) { 00055 print_error(params->error, "Ble initialization failed."); 00056 return; 00057 } 00058 00059 print_mac_address(); 00060 00061 start_advertising(); 00062 } 00063 00064 void start_advertising() { 00065 /* Create advertising parameters and payload */ 00066 00067 ble::AdvertisingParameters adv_parameters( 00068 ble::advertising_type_t::CONNECTABLE_UNDIRECTED, 00069 ble::adv_interval_t(ble::millisecond_t(1000)) 00070 ); 00071 00072 _adv_data_builder.setFlags(); 00073 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_battery_uuid, 1)); 00074 _adv_data_builder.setName(DEVICE_NAME); 00075 00076 /* Setup advertising */ 00077 00078 ble_error_t error = _ble.gap().setAdvertisingParameters( 00079 ble::LEGACY_ADVERTISING_HANDLE, 00080 adv_parameters 00081 ); 00082 00083 if (error) { 00084 print_error(error, "_ble.gap().setAdvertisingParameters() failed"); 00085 return; 00086 } 00087 00088 error = _ble.gap().setAdvertisingPayload( 00089 ble::LEGACY_ADVERTISING_HANDLE, 00090 _adv_data_builder.getAdvertisingData() 00091 ); 00092 00093 if (error) { 00094 print_error(error, "_ble.gap().setAdvertisingPayload() failed"); 00095 return; 00096 } 00097 00098 /* Start advertising */ 00099 00100 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); 00101 00102 if (error) { 00103 print_error(error, "_ble.gap().startAdvertising() failed"); 00104 return; 00105 } 00106 } 00107 00108 void update_sensor_value() { 00109 if (_ble.gap().getState().connected) { 00110 _battery_level++; 00111 if (_battery_level > 100) { 00112 _battery_level = 20; 00113 } 00114 00115 _battery_service.updateBatteryLevel(_battery_level); 00116 } 00117 } 00118 00119 void blink(void) { 00120 led1 = !led1; 00121 } 00122 00123 private: 00124 /* Event handler */ 00125 00126 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { 00127 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); 00128 } 00129 00130 private: 00131 BLE &_ble; 00132 events::EventQueue &_event_queue; 00133 00134 UUID _battery_uuid; 00135 00136 uint8_t _battery_level; 00137 BatteryService _battery_service; 00138 00139 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; 00140 ble::AdvertisingDataBuilder _adv_data_builder; 00141 }; 00142 00143 /** Schedule processing of events from the BLE middleware in the event queue. */ 00144 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { 00145 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); 00146 } 00147 00148 int main() 00149 { 00150 BLE &ble = BLE::Instance(); 00151 ble.onEventsToProcess(schedule_ble_events); 00152 00153 BatteryDemo demo(ble, event_queue); 00154 demo.start(); 00155 00156 return 0; 00157 }
Generated on Sat Jul 16 2022 01:26:04 by 1.7.2