The simplest of Beacons with basic configuration.
Dependencies: BLE_API mbed nRF51822
main.cpp@0:ced730eabda2, 2016-09-12 (annotated)
- Committer:
- BLELEIM
- Date:
- Mon Sep 12 23:38:20 2016 +0000
- Revision:
- 0:ced730eabda2
Simple Beacon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BLELEIM | 0:ced730eabda2 | 1 | /* mbed Microcontroller Library |
BLELEIM | 0:ced730eabda2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
BLELEIM | 0:ced730eabda2 | 3 | * |
BLELEIM | 0:ced730eabda2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
BLELEIM | 0:ced730eabda2 | 5 | * you may not use this file except in compliance with the License. |
BLELEIM | 0:ced730eabda2 | 6 | * You may obtain a copy of the License at |
BLELEIM | 0:ced730eabda2 | 7 | * |
BLELEIM | 0:ced730eabda2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
BLELEIM | 0:ced730eabda2 | 9 | * |
BLELEIM | 0:ced730eabda2 | 10 | * Unless required by applicable law or agreed to in writing, software |
BLELEIM | 0:ced730eabda2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
BLELEIM | 0:ced730eabda2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
BLELEIM | 0:ced730eabda2 | 13 | * See the License for the specific language governing permissions and |
BLELEIM | 0:ced730eabda2 | 14 | * limitations under the License. |
BLELEIM | 0:ced730eabda2 | 15 | */ |
BLELEIM | 0:ced730eabda2 | 16 | |
BLELEIM | 0:ced730eabda2 | 17 | #include "mbed.h" |
BLELEIM | 0:ced730eabda2 | 18 | #include "ble/BLE.h" |
BLELEIM | 0:ced730eabda2 | 19 | const static char DEVICE_NAME[] = "LEIM Beacon"; |
BLELEIM | 0:ced730eabda2 | 20 | uint8_t SERVICE_DEVICE_DESC[7] ={0x10, 0x10, 0x01, 0x12, 0x34, 0x56, 0x78}; |
BLELEIM | 0:ced730eabda2 | 21 | |
BLELEIM | 0:ced730eabda2 | 22 | |
BLELEIM | 0:ced730eabda2 | 23 | |
BLELEIM | 0:ced730eabda2 | 24 | /** |
BLELEIM | 0:ced730eabda2 | 25 | * This function is called when the ble initialization process has failed |
BLELEIM | 0:ced730eabda2 | 26 | */ |
BLELEIM | 0:ced730eabda2 | 27 | void onBleInitError(BLE &ble, ble_error_t error) |
BLELEIM | 0:ced730eabda2 | 28 | { |
BLELEIM | 0:ced730eabda2 | 29 | /* Initialization error handling should go here */ |
BLELEIM | 0:ced730eabda2 | 30 | } |
BLELEIM | 0:ced730eabda2 | 31 | |
BLELEIM | 0:ced730eabda2 | 32 | /** |
BLELEIM | 0:ced730eabda2 | 33 | * Callback triggered when the ble initialization process has finished |
BLELEIM | 0:ced730eabda2 | 34 | */ |
BLELEIM | 0:ced730eabda2 | 35 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
BLELEIM | 0:ced730eabda2 | 36 | { |
BLELEIM | 0:ced730eabda2 | 37 | BLE& ble = params->ble; |
BLELEIM | 0:ced730eabda2 | 38 | ble_error_t error = params->error; |
BLELEIM | 0:ced730eabda2 | 39 | |
BLELEIM | 0:ced730eabda2 | 40 | if (error != BLE_ERROR_NONE) { |
BLELEIM | 0:ced730eabda2 | 41 | /* In case of error, forward the error handling to onBleInitError */ |
BLELEIM | 0:ced730eabda2 | 42 | onBleInitError(ble, error); |
BLELEIM | 0:ced730eabda2 | 43 | return; |
BLELEIM | 0:ced730eabda2 | 44 | } |
BLELEIM | 0:ced730eabda2 | 45 | |
BLELEIM | 0:ced730eabda2 | 46 | /* Ensure that it is the default instance of BLE */ |
BLELEIM | 0:ced730eabda2 | 47 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
BLELEIM | 0:ced730eabda2 | 48 | return; |
BLELEIM | 0:ced730eabda2 | 49 | } |
BLELEIM | 0:ced730eabda2 | 50 | |
BLELEIM | 0:ced730eabda2 | 51 | |
BLELEIM | 0:ced730eabda2 | 52 | |
BLELEIM | 0:ced730eabda2 | 53 | /* setup advertising */ |
BLELEIM | 0:ced730eabda2 | 54 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); // advertising type |
BLELEIM | 0:ced730eabda2 | 55 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
BLELEIM | 0:ced730eabda2 | 56 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
BLELEIM | 0:ced730eabda2 | 57 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)SERVICE_DEVICE_DESC, sizeof(SERVICE_DEVICE_DESC)); |
BLELEIM | 0:ced730eabda2 | 58 | ble.gap().setAdvertisingInterval(4000); |
BLELEIM | 0:ced730eabda2 | 59 | ble.gap().startAdvertising(); |
BLELEIM | 0:ced730eabda2 | 60 | } |
BLELEIM | 0:ced730eabda2 | 61 | |
BLELEIM | 0:ced730eabda2 | 62 | int main(void) |
BLELEIM | 0:ced730eabda2 | 63 | { |
BLELEIM | 0:ced730eabda2 | 64 | |
BLELEIM | 0:ced730eabda2 | 65 | BLE &ble = BLE::Instance(); |
BLELEIM | 0:ced730eabda2 | 66 | ble.init(bleInitComplete); |
BLELEIM | 0:ced730eabda2 | 67 | |
BLELEIM | 0:ced730eabda2 | 68 | /* SpinWait for initialization to complete. This is necessary because the |
BLELEIM | 0:ced730eabda2 | 69 | * BLE object is used in the main loop below. */ |
BLELEIM | 0:ced730eabda2 | 70 | while (ble.hasInitialized() == false) { /* spin loop */ } |
BLELEIM | 0:ced730eabda2 | 71 | |
BLELEIM | 0:ced730eabda2 | 72 | while (true) { |
BLELEIM | 0:ced730eabda2 | 73 | ble.waitForEvent(); |
BLELEIM | 0:ced730eabda2 | 74 | } |
BLELEIM | 0:ced730eabda2 | 75 | } |