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.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 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 "pretty_printer.h" 00021 00022 static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00023 00024 /** @deprecated This demo is deprecated and no replacement is currently available. 00025 */ 00026 MBED_DEPRECATED_SINCE( 00027 "mbed-os-5.11", 00028 "This demo is deprecated and no replacement is currently available." 00029 ) 00030 class BeaconDemo : ble::Gap::EventHandler { 00031 public: 00032 BeaconDemo(BLE &ble, events::EventQueue &event_queue) : 00033 _ble(ble), 00034 _event_queue(event_queue), 00035 _adv_data_builder(_adv_buffer) { } 00036 00037 void start() { 00038 _ble.gap().setEventHandler(this); 00039 00040 _ble.init(this, &BeaconDemo::on_init_complete); 00041 00042 _event_queue.dispatch_forever(); 00043 } 00044 00045 private: 00046 /** 00047 * iBeacon payload builder. 00048 * 00049 * This data structure contains the payload of an iBeacon. The payload is 00050 * built at construction time and application code can set up an iBeacon by 00051 * injecting the raw field into the GAP advertising payload as a 00052 * GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA. 00053 */ 00054 union Payload { 00055 /** 00056 * Raw data of the payload. 00057 */ 00058 uint8_t raw[25]; 00059 struct { 00060 /** 00061 * Beacon manufacturer identifier. 00062 */ 00063 uint16_t companyID; 00064 00065 /** 00066 * Packet ID; Equal to 2 for an iBeacon. 00067 */ 00068 uint8_t ID; 00069 00070 /** 00071 * Length of the remaining data presents in the payload. 00072 */ 00073 uint8_t len; 00074 00075 /** 00076 * Beacon UUID. 00077 */ 00078 uint8_t proximityUUID[16]; 00079 00080 /** 00081 * Beacon Major group ID. 00082 */ 00083 uint16_t majorNumber; 00084 00085 /** 00086 * Beacon minor ID. 00087 */ 00088 uint16_t minorNumber; 00089 00090 /** 00091 * Tx power received at 1 meter; in dBm. 00092 */ 00093 uint8_t txPower; 00094 }; 00095 00096 /** 00097 * Assemble an iBeacon payload. 00098 * 00099 * @param[in] uuid Beacon network ID. iBeacon operators use this value 00100 * to group their iBeacons into a single network, a single region and 00101 * identify their organization among others. 00102 * 00103 * @param[in] majNum Beacon major group ID. iBeacon exploitants may use 00104 * this field to divide the region into subregions, their network into 00105 * subnetworks. 00106 * 00107 * @param[in] minNum Identifier of the Beacon in its subregion. 00108 * 00109 * @param[in] transmitPower Measured transmit power of the beacon at 1 00110 * meter. Scanners use this parameter to approximate the distance 00111 * to the beacon. 00112 * 00113 * @param[in] companyIDIn ID of the beacon manufacturer. 00114 */ 00115 Payload( 00116 const uint8_t *uuid, 00117 uint16_t majNum, 00118 uint16_t minNum, 00119 uint8_t transmitPower, 00120 uint16_t companyIDIn 00121 ) : companyID(companyIDIn), 00122 ID(0x02), 00123 len(0x15), 00124 majorNumber(__REV16(majNum)), 00125 minorNumber(__REV16(minNum)), 00126 txPower(transmitPower) 00127 { 00128 memcpy(proximityUUID, uuid, 16); 00129 } 00130 }; 00131 00132 /** Callback triggered when the ble initialization process has finished */ 00133 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { 00134 if (params->error != BLE_ERROR_NONE) { 00135 printf("Ble initialization failed."); 00136 return; 00137 } 00138 00139 print_mac_address(); 00140 00141 start_advertising(); 00142 } 00143 00144 void start_advertising() { 00145 /* Create advertising parameters and payload */ 00146 00147 ble::AdvertisingParameters adv_parameters( 00148 ble::advertising_type_t::CONNECTABLE_UNDIRECTED, 00149 ble::adv_interval_t(ble::millisecond_t(1000)) 00150 ); 00151 00152 _adv_data_builder.setFlags(); 00153 00154 /** 00155 * The Beacon payload has the following composition: 00156 * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00157 * Major/Minor = 0x1122 / 0x3344 00158 * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB) 00159 * 00160 * Note: please remember to calibrate your beacons TX Power for more accurate results. 00161 */ 00162 static const uint8_t uuid[] = { 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 00163 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61 }; 00164 uint16_t major_number = 1122; 00165 uint16_t minor_number = 3344; 00166 uint16_t tx_power = 0xC8; 00167 uint16_t comp_id = 0x004C; 00168 00169 Payload ibeacon(uuid, major_number, minor_number, tx_power, comp_id); 00170 00171 _adv_data_builder.setManufacturerSpecificData(ibeacon.raw); 00172 00173 /* Setup advertising */ 00174 00175 ble_error_t error = _ble.gap().setAdvertisingParameters( 00176 ble::LEGACY_ADVERTISING_HANDLE, 00177 adv_parameters 00178 ); 00179 00180 if (error) { 00181 print_error(error, "_ble.gap().setAdvertisingParameters() failed"); 00182 return; 00183 } 00184 00185 error = _ble.gap().setAdvertisingPayload( 00186 ble::LEGACY_ADVERTISING_HANDLE, 00187 _adv_data_builder.getAdvertisingData() 00188 ); 00189 00190 if (error) { 00191 print_error(error, "_ble.gap().setAdvertisingPayload() failed"); 00192 return; 00193 } 00194 00195 /* Start advertising */ 00196 00197 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); 00198 00199 if (error) { 00200 print_error(error, "_ble.gap().startAdvertising() failed"); 00201 return; 00202 } 00203 } 00204 00205 private: 00206 /* Event handler */ 00207 00208 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) { 00209 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); 00210 } 00211 00212 private: 00213 BLE &_ble; 00214 events::EventQueue &_event_queue; 00215 00216 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE]; 00217 ble::AdvertisingDataBuilder _adv_data_builder; 00218 }; 00219 00220 /** Schedule processing of events from the BLE middleware in the event queue. */ 00221 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { 00222 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); 00223 } 00224 00225 int main() 00226 { 00227 BLE &ble = BLE::Instance(); 00228 ble.onEventsToProcess(schedule_ble_events); 00229 00230 BeaconDemo demo(ble, event_queue); 00231 demo.start(); 00232 00233 return 0; 00234 }
Generated on Wed Jul 13 2022 11:56:39 by
1.7.2