Hugo Pristauz / Mbed 2 deprecated S05_Advertising

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

main.cpp

Committer:
hux
Date:
2016-12-10
Revision:
25:f3b44a34cf5d
Parent:
24:7bd093ad7d63
Child:
26:ab05e85743a9

File content as of revision 25:f3b44a34cf5d:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "mbed.h"
#include "ble/BLE.h"
#include "bricks/blob.h"
#include "bricks/blink.h"

   const char *idle = "x       ";               // 'idle' blinking sequence
   const char *advertise = "x xxx       ";      // 'advertise' blinking sequence
   const char *action = "x x x x x       ";     // 'action' blinking sequence
   const char *faulty = "x x xxx ";             // 'fault' blinking sequence

      // Add a device name for human readability

   const static char     DEVICE_NAME[] = "T05N_Advertising";

      // You have up to 26 bytes of advertising data to use.
      // Hex data example: {0x01,0x02,0x03,0x04,0x05}
      // Char data example: {"ChangeThisData"}

   const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; 

//==============================================================================
// Some Callbacks
//==============================================================================

   void onDisconnect(Blob &blob)   // Disconnection Callback
   {
      blob.start();                // start advertising
      blinking("advertise");
   }


   void onError(Blob &blob)        // Error Reporting Callback
   {
      (void) blob;                 // Avoid compiler warnings    
      /* Initialization error handling should go here */
   }    


   void onSetup(Blob &blob)        // Immediately After Initializing BLE 
   {
      if (blob.error != BLE_ERROR_NONE)
      {
         onError(blob);
         return;
      }

      blob.device(DEVICE_NAME);                    // setup device name
      blob.disconnect(onDisconnect);               // setup disconnect callback
      
      /* Sacrifice 3B of 31B to Advertising Flags */
      blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
      blob.pble->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);

      /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
      blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));

      /* Optional: Add name to device */
      blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));

      /* Set advertising interval. Longer interval == longer battery life */
      //blob.pble->gap().setAdvertisingInterval(100); /* 100ms */

      /* Start advertising */
      //blob.pble->gap().startAdvertising();

      blob.start(100);              // start advertising with 100 msec interval
      blinking("advertise");
   }


   int main(void)
   {
      Blob blob;
      blinking(idle);               // idle blinking - everything is OK!
      wait(5.0);
      
      blob.init(onSetup);           // init BLE base layer, always do first
      blinking(advertise);          // idle blinking - everything is OK!
      
      while (true)                  // Infinite loop waiting for BLE events */
      {
         blob.sleep();              // low power waiting for BLE events 
      }
   }