![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Using nRF51 as Line Beacon
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GAP_Example by
main.cpp@16:4271375739ea, 2017-04-12 (annotated)
- Committer:
- HTD
- Date:
- Wed Apr 12 18:52:04 2017 +0000
- Revision:
- 16:4271375739ea
- Parent:
- 15:7e06fce6e4f8
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andresag | 15:7e06fce6e4f8 | 1 | /* mbed Microcontroller Library |
andresag | 15:7e06fce6e4f8 | 2 | * Copyright (c) 2006-2015 ARM Limited |
andresag | 15:7e06fce6e4f8 | 3 | * |
andresag | 15:7e06fce6e4f8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
andresag | 15:7e06fce6e4f8 | 5 | * you may not use this file except in compliance with the License. |
andresag | 15:7e06fce6e4f8 | 6 | * You may obtain a copy of the License at |
andresag | 15:7e06fce6e4f8 | 7 | * |
andresag | 15:7e06fce6e4f8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
andresag | 15:7e06fce6e4f8 | 9 | * |
andresag | 15:7e06fce6e4f8 | 10 | * Unless required by applicable law or agreed to in writing, software |
andresag | 15:7e06fce6e4f8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
andresag | 15:7e06fce6e4f8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
andresag | 15:7e06fce6e4f8 | 13 | * See the License for the specific language governing permissions and |
andresag | 15:7e06fce6e4f8 | 14 | * limitations under the License. |
andresag | 15:7e06fce6e4f8 | 15 | */ |
andresag | 15:7e06fce6e4f8 | 16 | |
mbedAustin | 0:5375be4301ed | 17 | #include "mbed.h" |
andresag | 14:1c15d473b42f | 18 | #include "ble/BLE.h" |
andresag | 14:1c15d473b42f | 19 | |
andresag | 14:1c15d473b42f | 20 | /* Optional: Device Name, add for human read-ability */ |
HTD | 16:4271375739ea | 21 | const static char DEVICE_NAME[] = "Line Beacon 001"; /* Name it */ |
HTD | 16:4271375739ea | 22 | |
andresag | 14:1c15d473b42f | 23 | |
HTD | 16:4271375739ea | 24 | const static uint8_t AdvServiceUUIDs[] = { 0x6F, 0xFE }; |
HTD | 16:4271375739ea | 25 | const static uint8_t AdvData[] = { 0x6F, 0xFE, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00 }; /* Replace 0xFF, 0xFF, 0xFF, 0xFF, 0xFF to your bot's HWID */ |
HTD | 16:4271375739ea | 26 | //const static uint8_t AdvDataFrameType[] = { 0x02 }; |
HTD | 16:4271375739ea | 27 | //const static uint8_t AdvDataHWID[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
HTD | 16:4271375739ea | 28 | //const static uint8_t AdvDataMeasuredTxPower[] = { 0x7F }; |
HTD | 16:4271375739ea | 29 | //const static uint8_t AdvDataDeviceMessage[] = { 0x00 }; |
mbedAustin | 0:5375be4301ed | 30 | |
andresag | 14:1c15d473b42f | 31 | /* Optional: Restart advertising when peer disconnects */ |
andresag | 14:1c15d473b42f | 32 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
andresag | 14:1c15d473b42f | 33 | { |
andresag | 14:1c15d473b42f | 34 | BLE::Instance().gap().startAdvertising(); |
andresag | 14:1c15d473b42f | 35 | } |
andresag | 14:1c15d473b42f | 36 | /** |
andresag | 14:1c15d473b42f | 37 | * This function is called when the ble initialization process has failed |
andresag | 14:1c15d473b42f | 38 | */ |
andresag | 14:1c15d473b42f | 39 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 14:1c15d473b42f | 40 | { |
andresag | 14:1c15d473b42f | 41 | /* Avoid compiler warnings */ |
andresag | 14:1c15d473b42f | 42 | (void) ble; |
andresag | 14:1c15d473b42f | 43 | (void) error; |
andresag | 14:1c15d473b42f | 44 | |
andresag | 14:1c15d473b42f | 45 | /* Initialization error handling should go here */ |
andresag | 14:1c15d473b42f | 46 | } |
mbedAustin | 0:5375be4301ed | 47 | |
andresag | 14:1c15d473b42f | 48 | /** |
andresag | 14:1c15d473b42f | 49 | * Callback triggered when the ble initialization process has finished |
andresag | 14:1c15d473b42f | 50 | */ |
andresag | 14:1c15d473b42f | 51 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 14:1c15d473b42f | 52 | { |
andresag | 14:1c15d473b42f | 53 | BLE& ble = params->ble; |
andresag | 14:1c15d473b42f | 54 | ble_error_t error = params->error; |
andresag | 14:1c15d473b42f | 55 | |
andresag | 14:1c15d473b42f | 56 | if (error != BLE_ERROR_NONE) { |
andresag | 14:1c15d473b42f | 57 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 14:1c15d473b42f | 58 | onBleInitError(ble, error); |
andresag | 14:1c15d473b42f | 59 | return; |
andresag | 14:1c15d473b42f | 60 | } |
mbedAustin | 5:fff16d283dcf | 61 | |
andresag | 14:1c15d473b42f | 62 | /* Ensure that it is the default instance of BLE */ |
andresag | 14:1c15d473b42f | 63 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 14:1c15d473b42f | 64 | return; |
andresag | 14:1c15d473b42f | 65 | } |
andresag | 14:1c15d473b42f | 66 | |
andresag | 14:1c15d473b42f | 67 | /* Set device name characteristic data */ |
andresag | 14:1c15d473b42f | 68 | ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME); |
andresag | 14:1c15d473b42f | 69 | |
andresag | 14:1c15d473b42f | 70 | /* Optional: add callback for disconnection */ |
andresag | 14:1c15d473b42f | 71 | ble.gap().onDisconnection(disconnectionCallback); |
mbedAustin | 1:0692bee84264 | 72 | |
andresag | 14:1c15d473b42f | 73 | /* Sacrifice 3B of 31B to Advertising Flags */ |
andresag | 14:1c15d473b42f | 74 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); |
andresag | 14:1c15d473b42f | 75 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
andresag | 14:1c15d473b42f | 76 | |
andresag | 14:1c15d473b42f | 77 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
HTD | 16:4271375739ea | 78 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, AdvServiceUUIDs, sizeof(AdvServiceUUIDs)); |
HTD | 16:4271375739ea | 79 | |
HTD | 16:4271375739ea | 80 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvServiceUUIDs, sizeof(AdvServiceUUIDs)); |
andresag | 14:1c15d473b42f | 81 | |
andresag | 14:1c15d473b42f | 82 | /* Optional: Add name to device */ |
HTD | 16:4271375739ea | 83 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, AdvData, sizeof(AdvData)); |
HTD | 16:4271375739ea | 84 | //ble.gap().setAdvertisingData((const uint8_t *)AdvServiceUUIDs, (const uint8_t *)AdvData); |
andresag | 14:1c15d473b42f | 85 | |
andresag | 14:1c15d473b42f | 86 | /* Set advertising interval. Longer interval == longer battery life */ |
andresag | 14:1c15d473b42f | 87 | ble.gap().setAdvertisingInterval(100); /* 100ms */ |
andresag | 14:1c15d473b42f | 88 | |
andresag | 14:1c15d473b42f | 89 | /* Start advertising */ |
andresag | 14:1c15d473b42f | 90 | ble.gap().startAdvertising(); |
mbedAustin | 8:5442739198ec | 91 | } |
mbedAustin | 8:5442739198ec | 92 | |
mbedAustin | 1:0692bee84264 | 93 | int main(void) |
mbedAustin | 0:5375be4301ed | 94 | { |
andresag | 14:1c15d473b42f | 95 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
andresag | 14:1c15d473b42f | 96 | |
andresag | 14:1c15d473b42f | 97 | /* Initialize BLE baselayer, always do this first! */ |
andresag | 14:1c15d473b42f | 98 | ble.init(bleInitComplete); |
mbedAustin | 1:0692bee84264 | 99 | |
andresag | 14:1c15d473b42f | 100 | /* Infinite loop waiting for BLE events */ |
andresag | 14:1c15d473b42f | 101 | while (true) { |
andresag | 14:1c15d473b42f | 102 | /* Save power while waiting for callback events */ |
andresag | 14:1c15d473b42f | 103 | ble.waitForEvent(); |
mbedAustin | 1:0692bee84264 | 104 | } |
mbedAustin | 1:0692bee84264 | 105 | } |