Hugo Pristauz / Mbed 2 deprecated S05_Advertising

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sat Dec 10 23:32:08 2016 +0000
Revision:
25:f3b44a34cf5d
Parent:
24:7bd093ad7d63
Child:
26:ab05e85743a9
blob.start() implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:eb7f02ad28a7 1 /* mbed Microcontroller Library
screamer 0:eb7f02ad28a7 2 * Copyright (c) 2006-2015 ARM Limited
screamer 0:eb7f02ad28a7 3 *
screamer 0:eb7f02ad28a7 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:eb7f02ad28a7 5 * you may not use this file except in compliance with the License.
screamer 0:eb7f02ad28a7 6 * You may obtain a copy of the License at
screamer 0:eb7f02ad28a7 7 *
screamer 0:eb7f02ad28a7 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:eb7f02ad28a7 9 *
screamer 0:eb7f02ad28a7 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:eb7f02ad28a7 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:eb7f02ad28a7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:eb7f02ad28a7 13 * See the License for the specific language governing permissions and
screamer 0:eb7f02ad28a7 14 * limitations under the License.
screamer 0:eb7f02ad28a7 15 */
hux 22:e82c7b8a6072 16
screamer 0:eb7f02ad28a7 17 #include "mbed.h"
screamer 0:eb7f02ad28a7 18 #include "ble/BLE.h"
hux 22:e82c7b8a6072 19 #include "bricks/blob.h"
hux 22:e82c7b8a6072 20 #include "bricks/blink.h"
screamer 0:eb7f02ad28a7 21
hux 22:e82c7b8a6072 22 const char *idle = "x "; // 'idle' blinking sequence
hux 24:7bd093ad7d63 23 const char *advertise = "x xxx "; // 'advertise' blinking sequence
hux 22:e82c7b8a6072 24 const char *action = "x x x x x "; // 'action' blinking sequence
hux 24:7bd093ad7d63 25 const char *faulty = "x x xxx "; // 'fault' blinking sequence
hux 24:7bd093ad7d63 26
hux 24:7bd093ad7d63 27 // Add a device name for human readability
screamer 0:eb7f02ad28a7 28
hux 24:7bd093ad7d63 29 const static char DEVICE_NAME[] = "T05N_Advertising";
hux 24:7bd093ad7d63 30
hux 24:7bd093ad7d63 31 // You have up to 26 bytes of advertising data to use.
hux 24:7bd093ad7d63 32 // Hex data example: {0x01,0x02,0x03,0x04,0x05}
hux 24:7bd093ad7d63 33 // Char data example: {"ChangeThisData"}
hux 24:7bd093ad7d63 34
hux 24:7bd093ad7d63 35 const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05};
apalmieri 13:227a0149b677 36
hux 24:7bd093ad7d63 37 //==============================================================================
hux 24:7bd093ad7d63 38 // Some Callbacks
hux 24:7bd093ad7d63 39 //==============================================================================
screamer 0:eb7f02ad28a7 40
hux 24:7bd093ad7d63 41 void onDisconnect(Blob &blob) // Disconnection Callback
hux 24:7bd093ad7d63 42 {
hux 25:f3b44a34cf5d 43 blob.start(); // start advertising
hux 24:7bd093ad7d63 44 blinking("advertise");
hux 24:7bd093ad7d63 45 }
hux 23:f06c9851c72c 46
hux 25:f3b44a34cf5d 47
hux 24:7bd093ad7d63 48 void onError(Blob &blob) // Error Reporting Callback
hux 24:7bd093ad7d63 49 {
hux 24:7bd093ad7d63 50 (void) blob; // Avoid compiler warnings
hux 24:7bd093ad7d63 51 /* Initialization error handling should go here */
hux 24:7bd093ad7d63 52 }
hux 23:f06c9851c72c 53
hux 23:f06c9851c72c 54
hux 24:7bd093ad7d63 55 void onSetup(Blob &blob) // Immediately After Initializing BLE
hux 23:f06c9851c72c 56 {
hux 23:f06c9851c72c 57 if (blob.error != BLE_ERROR_NONE)
hux 23:f06c9851c72c 58 {
hux 24:7bd093ad7d63 59 onError(blob);
hux 23:f06c9851c72c 60 return;
hux 23:f06c9851c72c 61 }
apalmieri 13:227a0149b677 62
hux 24:7bd093ad7d63 63 blob.device(DEVICE_NAME); // setup device name
hux 24:7bd093ad7d63 64 blob.disconnect(onDisconnect); // setup disconnect callback
hux 24:7bd093ad7d63 65
hux 23:f06c9851c72c 66 /* Sacrifice 3B of 31B to Advertising Flags */
hux 23:f06c9851c72c 67 blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
hux 23:f06c9851c72c 68 blob.pble->gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
screamer 0:eb7f02ad28a7 69
hux 23:f06c9851c72c 70 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
hux 23:f06c9851c72c 71 blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
screamer 0:eb7f02ad28a7 72
hux 23:f06c9851c72c 73 /* Optional: Add name to device */
hux 23:f06c9851c72c 74 blob.pble->gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
screamer 0:eb7f02ad28a7 75
hux 23:f06c9851c72c 76 /* Set advertising interval. Longer interval == longer battery life */
hux 25:f3b44a34cf5d 77 //blob.pble->gap().setAdvertisingInterval(100); /* 100ms */
screamer 0:eb7f02ad28a7 78
hux 23:f06c9851c72c 79 /* Start advertising */
hux 25:f3b44a34cf5d 80 //blob.pble->gap().startAdvertising();
hux 24:7bd093ad7d63 81
hux 25:f3b44a34cf5d 82 blob.start(100); // start advertising with 100 msec interval
hux 24:7bd093ad7d63 83 blinking("advertise");
hux 23:f06c9851c72c 84 }
hux 23:f06c9851c72c 85
apalmieri 13:227a0149b677 86
hux 22:e82c7b8a6072 87 int main(void)
hux 22:e82c7b8a6072 88 {
hux 22:e82c7b8a6072 89 Blob blob;
hux 24:7bd093ad7d63 90 blinking(idle); // idle blinking - everything is OK!
hux 24:7bd093ad7d63 91 wait(5.0);
hux 23:f06c9851c72c 92
hux 24:7bd093ad7d63 93 blob.init(onSetup); // init BLE base layer, always do first
hux 24:7bd093ad7d63 94 blinking(advertise); // idle blinking - everything is OK!
hux 24:7bd093ad7d63 95
hux 24:7bd093ad7d63 96 while (true) // Infinite loop waiting for BLE events */
hux 22:e82c7b8a6072 97 {
hux 24:7bd093ad7d63 98 blob.sleep(); // low power waiting for BLE events
hux 22:e82c7b8a6072 99 }
hux 22:e82c7b8a6072 100 }