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.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 00002 // Headers necessary for mbed and BLE device mode 00003 #include "mbed.h" 00004 #include "BLEDevice.h" 00005 00006 // BLE object 00007 BLEDevice ble; 00008 00009 // Optional: Device Name, add for human read-ability 00010 //const static char DEVICE_NAME[] = "ChangeMe!!"; // Optional: device name 00011 00012 // You have up to 26 bytes of advertising data to use. 00013 00014 //const static uint8_t AdvData[] = {"ChangeThisData"}; // example of character data 00015 const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; // example of hex data 00016 00017 // Optional: Restart advertising when phone app disconnects 00018 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){ 00019 ble.startAdvertising(); 00020 } 00021 00022 // main program 00023 int main(void) 00024 { 00025 // Initialize BLE baselayer, always do this first! 00026 ble.init(); 00027 00028 // Optional: add callback for disconnection 00029 // ble.onDisconnection(disconnectionCallback); 00030 00031 // Sacrifice 3B of 31B to Advertising Flags 00032 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); 00033 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00034 00035 // Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define 00036 ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); 00037 00038 // Optional: Add name to device 00039 //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00040 00041 // Set advertising interval. Longer interval = longer battery life 00042 ble.setAdvertisingInterval(100); // 100ms, set as percentage of a second 00043 ble.startAdvertising(); 00044 00045 // Infinite loop waiting for BLE events 00046 for (;;) { 00047 ble.waitForEvent(); // this saves battery while waiting for callback events 00048 } 00049 }
Generated on Fri Jul 15 2022 13:55:38 by
1.7.2