wattx / Mbed 2 deprecated BLE_GAP_Example

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 Ticker timer;
00021 
00022 DigitalIn myInputPin(P0_10);
00023 
00024 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00025 
00026 /* Optional: Device Name, add for human read-ability */
00027 const static char     DEVICE_NAME[] = "ChangeMe!!";
00028 
00029 /* You have up to 26 bytes of advertising data to use. */
00030 uint8_t AdvData[] = {0x01,0x02,0x00,0x04,0x12};   /* Example of hex data */
00031 //const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */
00032 
00033 
00034 class AdvertisementPacket {
00035 public:
00036     AdvertisementPacket(BLE&);
00037     void updateAdvertisementPacket(){
00038         
00039         AdvData[1] = myInputPin;
00040         
00041         ble.gap().clearAdvertisingPayload();
00042         ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
00043         ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
00044         };
00045 private:
00046     BLE &ble;
00047 };
00048 
00049 AdvertisementPacket::AdvertisementPacket(BLE &bleIn): ble(bleIn){
00050         
00051 };
00052 /*
00053 
00054 /* Optional: Restart advertising when peer disconnects */
00055 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00056 {
00057     BLE::Instance().gap().startAdvertising();
00058 }
00059 /**
00060  * This function is called when the ble initialization process has failed
00061  */
00062 void onBleInitError(BLE &ble, ble_error_t error)
00063 {
00064     /* Avoid compiler warnings */
00065     (void) ble;
00066     (void) error;
00067     
00068     /* Initialization error handling should go here */
00069 }    
00070 
00071 /**
00072  * Callback triggered when the ble initialization process has finished
00073  */
00074 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00075 {
00076     BLE&        ble   = params->ble;
00077     ble_error_t error = params->error;
00078 
00079     if (error != BLE_ERROR_NONE) {
00080         /* In case of error, forward the error handling to onBleInitError */
00081         onBleInitError(ble, error);
00082         return;
00083     }
00084 
00085     /* Ensure that it is the default instance of BLE */
00086     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00087         return;
00088     }
00089     
00090     /* Set device name characteristic data */
00091     ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
00092 
00093     /* Optional: add callback for disconnection */
00094     ble.gap().onDisconnection(disconnectionCallback);
00095 
00096     /* Sacrifice 3B of 31B to Advertising Flags */
00097     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
00098     //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00099     
00100     
00101      /* Configure advertisements */
00102     //ble.gap().setTxPower(radioPowerLevels[txPowerMode]);
00103     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
00104     //ble.gap().setAdvertisingInterval(beaconPeriod);
00105     //ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
00106     //ble.gap().initRadioNotification();
00107     
00108 
00109     /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
00110     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
00111 
00112     /* Optional: Add name to device */
00113     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00114 
00115     /* Set advertising interval. Longer interval == longer battery life */
00116     ble.gap().setAdvertisingInterval(1000); /* 100ms */
00117     
00118     ble.gap().clearAdvertisingPayload();
00119     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
00120     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
00121     
00122     
00123 
00124     /* Start advertising */
00125     ble.gap().startAdvertising();
00126 }
00127 
00128 int main(void)
00129 {
00130     
00131     
00132     
00133  
00134     /* Initialize BLE baselayer, always do this first! */
00135     ble.init(bleInitComplete);
00136 
00137     /* SpinWait for initialization to complete. This is necessary because the
00138      * BLE object is used in the main loop below. */
00139     while (!ble.hasInitialized()) { /* spin loop */ }
00140     
00141     AdvertisementPacket f(ble);
00142 
00143     timer.attach( &f, &AdvertisementPacket::updateAdvertisementPacket, 1.0); 
00144 
00145     while (true) {
00146         ble.waitForEvent();
00147     }
00148 }