Esempio GAP Base

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
fdalforno
Date:
Wed Jan 10 20:17:16 2018 +0000
Revision:
22:4a9f377d3c16
Parent:
21:0e7c08f5386f
Esempio GAP base per nucleo

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 */
fdalforno 22:4a9f377d3c16 16
screamer 0:eb7f02ad28a7 17 #include "mbed.h"
screamer 0:eb7f02ad28a7 18 #include "ble/BLE.h"
fdalforno 22:4a9f377d3c16 19
fdalforno 22:4a9f377d3c16 20 /* Optional: Device Name, add for human read-ability */
fdalforno 22:4a9f377d3c16 21 const static char DEVICE_NAME[] = "ChangeMe!!";
fdalforno 22:4a9f377d3c16 22
fdalforno 22:4a9f377d3c16 23 /* You have up to 26 bytes of advertising data to use. */
fdalforno 22:4a9f377d3c16 24 const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */
fdalforno 22:4a9f377d3c16 25 //const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */
fdalforno 22:4a9f377d3c16 26
fdalforno 22:4a9f377d3c16 27 /* Optional: Restart advertising when peer disconnects */
apalmieri 2:bc0c0d442a24 28 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
screamer 0:eb7f02ad28a7 29 {
fdalforno 22:4a9f377d3c16 30 BLE::Instance().gap().startAdvertising();
screamer 0:eb7f02ad28a7 31 }
fdalforno 22:4a9f377d3c16 32 /**
fdalforno 22:4a9f377d3c16 33 * This function is called when the ble initialization process has failed
fdalforno 22:4a9f377d3c16 34 */
apalmieri 13:227a0149b677 35 void onBleInitError(BLE &ble, ble_error_t error)
apalmieri 13:227a0149b677 36 {
fdalforno 22:4a9f377d3c16 37 /* Avoid compiler warnings */
fdalforno 22:4a9f377d3c16 38 (void) ble;
fdalforno 22:4a9f377d3c16 39 (void) error;
fdalforno 22:4a9f377d3c16 40
fdalforno 22:4a9f377d3c16 41 /* Initialization error handling should go here */
fdalforno 22:4a9f377d3c16 42 }
fdalforno 22:4a9f377d3c16 43
fdalforno 22:4a9f377d3c16 44 /**
fdalforno 22:4a9f377d3c16 45 * Callback triggered when the ble initialization process has finished
fdalforno 22:4a9f377d3c16 46 */
apalmieri 13:227a0149b677 47 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
screamer 0:eb7f02ad28a7 48 {
apalmieri 13:227a0149b677 49 BLE& ble = params->ble;
apalmieri 13:227a0149b677 50 ble_error_t error = params->error;
fdalforno 22:4a9f377d3c16 51
apalmieri 13:227a0149b677 52 if (error != BLE_ERROR_NONE) {
fdalforno 22:4a9f377d3c16 53 /* In case of error, forward the error handling to onBleInitError */
apalmieri 13:227a0149b677 54 onBleInitError(ble, error);
apalmieri 13:227a0149b677 55 return;
apalmieri 13:227a0149b677 56 }
fdalforno 22:4a9f377d3c16 57
fdalforno 22:4a9f377d3c16 58 /* Ensure that it is the default instance of BLE */
fdalforno 22:4a9f377d3c16 59 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
apalmieri 13:227a0149b677 60 return;
apalmieri 13:227a0149b677 61 }
fdalforno 22:4a9f377d3c16 62
fdalforno 22:4a9f377d3c16 63 /* Set device name characteristic data */
fdalforno 22:4a9f377d3c16 64 ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);
fdalforno 22:4a9f377d3c16 65
fdalforno 22:4a9f377d3c16 66 /* Optional: add callback for disconnection */
screamer 0:eb7f02ad28a7 67 ble.gap().onDisconnection(disconnectionCallback);
fdalforno 22:4a9f377d3c16 68
fdalforno 22:4a9f377d3c16 69 /* Sacrifice 3B of 31B to Advertising Flags */
fdalforno 22:4a9f377d3c16 70 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
fdalforno 22:4a9f377d3c16 71 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
fdalforno 22:4a9f377d3c16 72
fdalforno 22:4a9f377d3c16 73 /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
fdalforno 22:4a9f377d3c16 74 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
fdalforno 22:4a9f377d3c16 75
fdalforno 22:4a9f377d3c16 76 /* Optional: Add name to device */
screamer 0:eb7f02ad28a7 77 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
fdalforno 22:4a9f377d3c16 78
fdalforno 22:4a9f377d3c16 79 /* Set advertising interval. Longer interval == longer battery life */
fdalforno 22:4a9f377d3c16 80 ble.gap().setAdvertisingInterval(100); /* 100ms */
fdalforno 22:4a9f377d3c16 81
fdalforno 22:4a9f377d3c16 82 /* Start advertising */
screamer 0:eb7f02ad28a7 83 ble.gap().startAdvertising();
fdalforno 22:4a9f377d3c16 84 }
fdalforno 22:4a9f377d3c16 85
fdalforno 22:4a9f377d3c16 86 int main(void)
fdalforno 22:4a9f377d3c16 87 {
fdalforno 22:4a9f377d3c16 88 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
fdalforno 22:4a9f377d3c16 89
fdalforno 22:4a9f377d3c16 90 /* Initialize BLE baselayer, always do this first! */
fdalforno 22:4a9f377d3c16 91 ble.init(bleInitComplete);
fdalforno 22:4a9f377d3c16 92
fdalforno 22:4a9f377d3c16 93 /* Infinite loop waiting for BLE events */
apalmieri 13:227a0149b677 94 while (true) {
fdalforno 22:4a9f377d3c16 95 /* Save power while waiting for callback events */
fdalforno 22:4a9f377d3c16 96 ble.waitForEvent();
screamer 0:eb7f02ad28a7 97 }
screamer 0:eb7f02ad28a7 98 }
fdalforno 22:4a9f377d3c16 99