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
Fork of BLE_GAP_Example by
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 "mbed.h" 00018 #include "ble/BLE.h" 00019 00020 #ifndef NAME 00021 #define NAME "spiffy" 00022 #endif 00023 00024 /* Optional: Device Name, add for human read-ability */ 00025 const static char DEVICE_NAME[] = NAME; 00026 00027 /* You have up to 26 bytes of advertising data to use. */ 00028 //const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */ 00029 const static uint8_t AdvData[] = {"dac"}; /* Example of character data */ 00030 00031 /* Optional: Restart advertising when peer disconnects */ 00032 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00033 { 00034 BLE::Instance().gap().startAdvertising(); 00035 } 00036 /** 00037 * This function is called when the ble initialization process has failed 00038 */ 00039 void onBleInitError(BLE &ble, ble_error_t error) 00040 { 00041 /* Avoid compiler warnings */ 00042 (void) ble; 00043 (void) error; 00044 00045 /* Initialization error handling should go here */ 00046 } 00047 00048 /** 00049 * Callback triggered when the ble initialization process has finished 00050 */ 00051 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00052 { 00053 BLE& ble = params->ble; 00054 ble_error_t error = params->error; 00055 00056 if (error != BLE_ERROR_NONE) { 00057 /* In case of error, forward the error handling to onBleInitError */ 00058 onBleInitError(ble, error); 00059 return; 00060 } 00061 00062 /* Ensure that it is the default instance of BLE */ 00063 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00064 return; 00065 } 00066 00067 /* Set device name characteristic data */ 00068 ble.gap().setDeviceName(reinterpret_cast<const uint8_t *>(&DEVICE_NAME)); 00069 00070 /* Optional: add callback for disconnection */ 00071 ble.gap().onDisconnection(disconnectionCallback); 00072 00073 /* Sacrifice 3B of 31B to Advertising Flags */ 00074 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); 00075 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00076 00077 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ 00078 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); 00079 00080 /* Optional: Add name to device */ 00081 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00082 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, reinterpret_cast<const uint8_t *>(&DEVICE_NAME), sizeof(DEVICE_NAME)); 00083 /* Set advertising interval. Longer interval == longer battery life */ 00084 ble.gap().setAdvertisingInterval(100); /* 100ms */ 00085 00086 /* Start advertising */ 00087 ble.gap().setTxPower(0); 00088 ble.gap().startAdvertising(); 00089 } 00090 00091 int main(void) 00092 { 00093 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 00094 00095 /* Initialize BLE baselayer, always do this first! */ 00096 ble.init(bleInitComplete); 00097 00098 /* Infinite loop waiting for BLE events */ 00099 while (true) { 00100 /* Save power while waiting for callback events */ 00101 ble.waitForEvent(); 00102 } 00103 }
Generated on Fri Jul 15 2022 16:14:34 by
1.7.2
