ZO Hong / Mbed 2 deprecated BLE_GAP_LineBeacon_nRF51

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GAP_Example by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 /* Optional: Device Name, add for human read-ability */
00021 const static char     DEVICE_NAME[] = "Line Beacon 001";                                           /* Name it */
00022 
00023 
00024 const static uint8_t AdvServiceUUIDs[] = { 0x6F, 0xFE };                           
00025 const static uint8_t AdvData[] = { 0x6F, 0xFE, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00 };  /* Replace 0xFF, 0xFF, 0xFF, 0xFF, 0xFF to your bot's HWID */
00026 //const static uint8_t AdvDataFrameType[] = { 0x02 };
00027 //const static uint8_t AdvDataHWID[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
00028 //const static uint8_t AdvDataMeasuredTxPower[] = { 0x7F };
00029 //const static uint8_t AdvDataDeviceMessage[] = { 0x00 };
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((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::COMPLETE_LIST_16BIT_SERVICE_IDS, AdvServiceUUIDs, sizeof(AdvServiceUUIDs));
00079     
00080     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvServiceUUIDs, sizeof(AdvServiceUUIDs));
00081 
00082     /* Optional: Add name to device */
00083     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData));
00084     //ble.gap().setAdvertisingData((const uint8_t *)AdvServiceUUIDs, (const uint8_t *)AdvData);
00085 
00086     /* Set advertising interval. Longer interval == longer battery life */
00087     ble.gap().setAdvertisingInterval(100); /* 100ms */
00088 
00089     /* Start advertising */
00090     ble.gap().startAdvertising();
00091 }
00092 
00093 int main(void)
00094 {
00095     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00096  
00097     /* Initialize BLE baselayer, always do this first! */
00098     ble.init(bleInitComplete);
00099 
00100     /* Infinite loop waiting for BLE events */
00101     while (true) {
00102         /* Save power while waiting for callback events */
00103         ble.waitForEvent();
00104     }
00105 }