Minimal usage example of BLE API
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 #include "mbed.h" 00002 #include "ble/BLE.h" 00003 00004 // DFUService is already included & automatically advertised by the mbed lib dependancies 00005 00006 const static char DEVICE_NAME[] = "HelloBlue"; 00007 00008 BLEDevice ble; 00009 00010 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00011 { 00012 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising 00013 } 00014 00015 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00016 { 00017 BLE &ble = params->ble; 00018 ble_error_t error = params->error; 00019 00020 if (error != BLE_ERROR_NONE) { 00021 return; 00022 } 00023 00024 ble.gap().onDisconnection(disconnectionCallback); 00025 00026 /* Setup advertising. */ 00027 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00028 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00029 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00030 ble.gap().setAdvertisingInterval(1000); /* ms */ 00031 ble.gap().startAdvertising(); 00032 } 00033 00034 00035 int main(void) 00036 { 00037 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00038 ble.init(bleInitComplete); 00039 00040 /* SpinWait for initialization to complete. This is necessary because the 00041 * BLE object is used in the main loop below. */ 00042 while (ble.hasInitialized() == false) { /* spin loop */ } 00043 00044 while (1) 00045 { 00046 // to see if a Central device is currently connected you can call: ble.getGapState().connected 00047 ble.waitForEvent(); // low power wait for event 00048 } 00049 } 00050
Generated on Wed Aug 3 2022 00:25:08 by 1.7.2