BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
main.cpp@2:b34944e028f9, 2016-11-11 (annotated)
- Committer:
- erikamorv
- Date:
- Fri Nov 11 13:31:05 2016 +0000
- Revision:
- 2:b34944e028f9
- Parent:
- 1:6ec60260b682
- Child:
- 3:37a2f13b746d
2016.11.11 Resta in Scan e non in Advertising
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 0:39b311448c9e | 1 | /* mbed Microcontroller Library |
vcoubard | 0:39b311448c9e | 2 | * Copyright (c) 2006-2015 ARM Limited |
vcoubard | 0:39b311448c9e | 3 | * |
vcoubard | 0:39b311448c9e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 0:39b311448c9e | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 0:39b311448c9e | 6 | * You may obtain a copy of the License at |
vcoubard | 0:39b311448c9e | 7 | * |
vcoubard | 0:39b311448c9e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 0:39b311448c9e | 9 | * |
vcoubard | 0:39b311448c9e | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 0:39b311448c9e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 0:39b311448c9e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 0:39b311448c9e | 13 | * See the License for the specific language governing permissions and |
vcoubard | 0:39b311448c9e | 14 | * limitations under the License. |
vcoubard | 0:39b311448c9e | 15 | */ |
erikamorv | 1:6ec60260b682 | 16 | |
vcoubard | 0:39b311448c9e | 17 | #include "mbed.h" |
vcoubard | 0:39b311448c9e | 18 | #include "BLE.h" |
erikamorv | 1:6ec60260b682 | 19 | |
vcoubard | 0:39b311448c9e | 20 | BLE ble; |
erikamorv | 2:b34944e028f9 | 21 | BLE ble_Scan; |
vcoubard | 0:39b311448c9e | 22 | DigitalOut led1(LED1); |
erikamorv | 2:b34944e028f9 | 23 | |
erikamorv | 2:b34944e028f9 | 24 | // Change your device name below |
erikamorv | 2:b34944e028f9 | 25 | const char DEVICE_NAME[] = "Nucleo_test"; |
erikamorv | 1:6ec60260b682 | 26 | |
vcoubard | 0:39b311448c9e | 27 | void periodicCallback(void) |
vcoubard | 0:39b311448c9e | 28 | { |
vcoubard | 0:39b311448c9e | 29 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
vcoubard | 0:39b311448c9e | 30 | } |
erikamorv | 1:6ec60260b682 | 31 | |
erikamorv | 2:b34944e028f9 | 32 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
erikamorv | 2:b34944e028f9 | 33 | |
erikamorv | 2:b34944e028f9 | 34 | if (params->peerAddr[0] == 0xB2) { |
erikamorv | 2:b34944e028f9 | 35 | |
erikamorv | 2:b34944e028f9 | 36 | if (abs(params->rssi) <= 60) { |
erikamorv | 2:b34944e028f9 | 37 | |
erikamorv | 2:b34944e028f9 | 38 | printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
vcoubard | 0:39b311448c9e | 39 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 2:b34944e028f9 | 40 | params->rssi); |
erikamorv | 2:b34944e028f9 | 41 | } |
erikamorv | 2:b34944e028f9 | 42 | } |
erikamorv | 2:b34944e028f9 | 43 | |
erikamorv | 2:b34944e028f9 | 44 | if (params->peerAddr[0] == 0xA8) { |
erikamorv | 2:b34944e028f9 | 45 | |
erikamorv | 2:b34944e028f9 | 46 | if (abs(params->rssi) <= 60) { // Beacon 2 necessita di una soglia di vicinanza più alta |
erikamorv | 2:b34944e028f9 | 47 | printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
erikamorv | 2:b34944e028f9 | 48 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 2:b34944e028f9 | 49 | params->rssi); |
erikamorv | 2:b34944e028f9 | 50 | } |
erikamorv | 2:b34944e028f9 | 51 | } |
erikamorv | 1:6ec60260b682 | 52 | |
erikamorv | 2:b34944e028f9 | 53 | if (params->peerAddr[0] == 0x46) { |
erikamorv | 2:b34944e028f9 | 54 | |
erikamorv | 2:b34944e028f9 | 55 | if (abs(params->rssi) <= 60) { |
erikamorv | 2:b34944e028f9 | 56 | printf("Beacon_3: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
erikamorv | 2:b34944e028f9 | 57 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 2:b34944e028f9 | 58 | params->rssi); |
vcoubard | 0:39b311448c9e | 59 | } |
erikamorv | 2:b34944e028f9 | 60 | } |
erikamorv | 1:6ec60260b682 | 61 | |
erikamorv | 2:b34944e028f9 | 62 | } |
erikamorv | 2:b34944e028f9 | 63 | |
erikamorv | 2:b34944e028f9 | 64 | /* Optional: Restart advertising when peer disconnects */ |
erikamorv | 2:b34944e028f9 | 65 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
erikamorv | 2:b34944e028f9 | 66 | { |
erikamorv | 2:b34944e028f9 | 67 | BLE::Instance().gap().startAdvertising(); |
erikamorv | 2:b34944e028f9 | 68 | } |
erikamorv | 1:6ec60260b682 | 69 | |
erikamorv | 2:b34944e028f9 | 70 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) |
erikamorv | 2:b34944e028f9 | 71 | { |
erikamorv | 2:b34944e028f9 | 72 | // Set up the advertising flags. Note: not all combination of flags are valid |
erikamorv | 2:b34944e028f9 | 73 | // BREDR_NOT_SUPPORTED: Device does not support Basic Rate or Enchanced Data Rate, It is Low Energy only. |
erikamorv | 2:b34944e028f9 | 74 | // LE_GENERAL_DISCOVERABLE: Peripheral device is discoverable at any moment |
erikamorv | 2:b34944e028f9 | 75 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
erikamorv | 2:b34944e028f9 | 76 | |
erikamorv | 2:b34944e028f9 | 77 | // Put the device name in the advertising payload |
erikamorv | 2:b34944e028f9 | 78 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
erikamorv | 2:b34944e028f9 | 79 | |
erikamorv | 2:b34944e028f9 | 80 | /* Optional: add callback for disconnection */ |
erikamorv | 2:b34944e028f9 | 81 | ble.gap().onDisconnection(disconnectionCallback); |
erikamorv | 1:6ec60260b682 | 82 | |
erikamorv | 2:b34944e028f9 | 83 | // The Service Data data type consists of a service UUID with the data associated with that service. |
erikamorv | 2:b34944e028f9 | 84 | // We will encode the number of button clicks in the Service Data field |
erikamorv | 2:b34944e028f9 | 85 | // First two bytes of SERVICE_DATA field should contain the UUID of the service |
erikamorv | 2:b34944e028f9 | 86 | //uint8_t service_data[3]; |
erikamorv | 2:b34944e028f9 | 87 | //service_data[0] = MajorID & 0xff; |
erikamorv | 2:b34944e028f9 | 88 | //service_data[1] = MinorID & 0xff; |
erikamorv | 2:b34944e028f9 | 89 | //service_data[2] = TXPOWER & 0xff; |
erikamorv | 2:b34944e028f9 | 90 | //err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
erikamorv | 2:b34944e028f9 | 91 | //if (err != BLE_ERROR_NONE) { |
erikamorv | 2:b34944e028f9 | 92 | // print_error(err, "Setting service data failed"); |
erikamorv | 2:b34944e028f9 | 93 | // return; |
erikamorv | 2:b34944e028f9 | 94 | //} |
erikamorv | 2:b34944e028f9 | 95 | |
erikamorv | 2:b34944e028f9 | 96 | // It is not connectable as we are just boardcasting |
erikamorv | 2:b34944e028f9 | 97 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
erikamorv | 2:b34944e028f9 | 98 | |
erikamorv | 2:b34944e028f9 | 99 | //Send out the advertising payload every 1000ms |
erikamorv | 2:b34944e028f9 | 100 | ble.gap().setAdvertisingInterval(1000); |
erikamorv | 2:b34944e028f9 | 101 | |
erikamorv | 2:b34944e028f9 | 102 | //ble.gap().startAdvertising(); |
erikamorv | 2:b34944e028f9 | 103 | |
vcoubard | 0:39b311448c9e | 104 | } |
erikamorv | 1:6ec60260b682 | 105 | |
vcoubard | 0:39b311448c9e | 106 | int main(void) |
vcoubard | 0:39b311448c9e | 107 | { |
vcoubard | 0:39b311448c9e | 108 | led1 = 1; |
vcoubard | 0:39b311448c9e | 109 | Ticker ticker; |
vcoubard | 0:39b311448c9e | 110 | ticker.attach(periodicCallback, 1); |
erikamorv | 2:b34944e028f9 | 111 | |
erikamorv | 2:b34944e028f9 | 112 | BLE &ble = BLE::Instance(); |
erikamorv | 2:b34944e028f9 | 113 | BLE &ble_Scan = BLE::Instance(); |
erikamorv | 1:6ec60260b682 | 114 | |
erikamorv | 2:b34944e028f9 | 115 | ble.init(bleInitComplete); |
erikamorv | 2:b34944e028f9 | 116 | //ble_Scan.init(); |
erikamorv | 2:b34944e028f9 | 117 | |
erikamorv | 2:b34944e028f9 | 118 | //ble.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); |
erikamorv | 2:b34944e028f9 | 119 | //ble.gap().startScan(advertisementCallback); |
erikamorv | 2:b34944e028f9 | 120 | |
erikamorv | 2:b34944e028f9 | 121 | int stato = 0; |
erikamorv | 2:b34944e028f9 | 122 | |
erikamorv | 2:b34944e028f9 | 123 | /* Infinite loop waiting for BLE events */ |
erikamorv | 2:b34944e028f9 | 124 | while (true) { |
erikamorv | 2:b34944e028f9 | 125 | switch (stato) { |
erikamorv | 2:b34944e028f9 | 126 | case(0): |
erikamorv | 2:b34944e028f9 | 127 | //BLE &ble = BLE::Instance(); |
erikamorv | 2:b34944e028f9 | 128 | printf("Advertising stato 0\r\n"); |
erikamorv | 2:b34944e028f9 | 129 | ble.gap().stopScan(); |
erikamorv | 2:b34944e028f9 | 130 | //ble.init(bleInitComplete); |
erikamorv | 2:b34944e028f9 | 131 | ble.gap().startAdvertising(); |
erikamorv | 2:b34944e028f9 | 132 | wait(10); |
erikamorv | 2:b34944e028f9 | 133 | //ble.gap().stopAdvertising(); |
erikamorv | 2:b34944e028f9 | 134 | //wait(1); |
erikamorv | 2:b34944e028f9 | 135 | stato = 1; |
erikamorv | 2:b34944e028f9 | 136 | break; |
erikamorv | 2:b34944e028f9 | 137 | case(1): |
erikamorv | 2:b34944e028f9 | 138 | //BLE &ble_Scan = BLE::Instance(); |
erikamorv | 2:b34944e028f9 | 139 | //ble_Scan.init(); |
erikamorv | 2:b34944e028f9 | 140 | printf("Scan stato 1\r\n"); |
erikamorv | 2:b34944e028f9 | 141 | ble.gap().stopAdvertising(); |
erikamorv | 2:b34944e028f9 | 142 | ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); |
erikamorv | 2:b34944e028f9 | 143 | ble_Scan.gap().startScan(advertisementCallback); |
erikamorv | 2:b34944e028f9 | 144 | wait(1); |
erikamorv | 2:b34944e028f9 | 145 | ble_Scan.gap().stopScan(); |
erikamorv | 2:b34944e028f9 | 146 | //wait(1); |
erikamorv | 2:b34944e028f9 | 147 | stato = 0; |
erikamorv | 2:b34944e028f9 | 148 | break; |
erikamorv | 2:b34944e028f9 | 149 | } |
erikamorv | 2:b34944e028f9 | 150 | |
erikamorv | 2:b34944e028f9 | 151 | /* Save power while waiting for callback events */ |
erikamorv | 2:b34944e028f9 | 152 | ble.waitForEvent(); |
erikamorv | 2:b34944e028f9 | 153 | |
erikamorv | 2:b34944e028f9 | 154 | } |
erikamorv | 2:b34944e028f9 | 155 | } |