![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
main.cpp
- Committer:
- erikamorv
- Date:
- 2016-11-14
- Revision:
- 4:2aefa62bdce5
- Parent:
- 3:37a2f13b746d
- Child:
- 5:346bcd62f251
File content as of revision 4:2aefa62bdce5:
/* 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.h" BLE ble; BLE ble_Scan; DigitalOut led1(LED1); // Change your device name below const char DEVICE_NAME[] = "Nucleo_test"; // Control Beacon read static int beacon = 0; void periodicCallback(void) { led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ } void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { if (params->peerAddr[0] == 0xB2) { if (abs(params->rssi) <= 60) { printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi); beacon = 1; } } if (params->peerAddr[0] == 0xA8) { if (abs(params->rssi) <= 60) { // Beacon 2 necessita di una soglia di vicinanza più alta printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi); beacon = 2; } } if (params->peerAddr[0] == 0x46) { if (abs(params->rssi) <= 60) { printf("Beacon_3: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi); beacon = 3; } } } /* Optional: Restart advertising when peer disconnects */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { BLE::Instance().gap().startAdvertising(); } void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) { // Set up the advertising flags. Note: not all combination of flags are valid // BREDR_NOT_SUPPORTED: Device does not support Basic Rate or Enchanced Data Rate, It is Low Energy only. // LE_GENERAL_DISCOVERABLE: Peripheral device is discoverable at any moment ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // Put the device name in the advertising payload ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); /* Optional: add callback for disconnection */ ble.gap().onDisconnection(disconnectionCallback); // The Service Data data type consists of a service UUID with the data associated with that service. // We will encode the number of beacon received //uint8_t service_data[3]; //service_data[0] = MajorID & 0xff; //service_data[1] = MinorID & 0xff; //service_data[2] = TXPOWER & 0xff; //err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); //if (err != BLE_ERROR_NONE) { // print_error(err, "Setting service data failed"); // return; //} // It is not connectable as we are just boardcasting ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); //Send out the advertising payload every 1000ms ble.gap().setAdvertisingInterval(1000); //ble.gap().startAdvertising(); } void bleScanInitComplete(BLE::InitializationCompleteCallbackContext *context) { ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); } //extern "C" void mbed_reset(); int main(void) { led1 = 1; Ticker ticker; ticker.attach(periodicCallback, 1); BLE &ble = BLE::Instance(); BLE &ble_Scan = BLE::Instance(); //ble.init(bleInitComplete); //ble_Scan.init(); //ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); //ble_Scan.gap().startScan(advertisementCallback); //ble.gap().startAdvertising(); int stato = 1; /* Infinite loop waiting for BLE events */ while (true) { switch (stato) { case(0): //BLE &ble = BLE::Instance(); printf("Advertising stato 0\r\n"); //ble.gap().stopScan(); ble.init(bleInitComplete); ble.gap().startAdvertising(); wait(10); ble.gap().stopAdvertising(); wait(1); stato = 1; NVIC_SystemReset(); break; case(1): //BLE &ble_Scan = BLE::Instance(); ble_Scan.init(bleScanInitComplete); printf("Scan stato 1\r\n"); //ble.gap().stopAdvertising(); //ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); ble_Scan.gap().startScan(advertisementCallback); wait(5); //ble_Scan.gap().stopScan(); stato = 0; break; } /* Save power while waiting for callback events */ ble.waitForEvent(); ble_Scan.waitForEvent(); } }