Demonstration code showing how to control an LED over Bluetooth Low Energy using the UARTService
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
This programme demonstrates the use of the UARTService to send command strings over Bluetooth Low Energy (BLE) to control devices attached to the nRF51822 board.
In this example you can use a BLE connection from smart phone to turn on and off LED2 on the board.
Sending the character string "led2 on" will turn the LED on and "led2 off" will turn it off.
The command that is received is also echoed back via BLE to the sending device.
You can use the nRF UART App on and Android or iOS smart phone with BLE to test this out https://www.nordicsemi.com/eng/Products/nRFready-Demo-APPS
I have also ported the source code for the Android app into Android studio which should be a good starting point for developing your own custom App https://github.com/bennthomsen/nRF_UART. If you want to start using this within Android Studio create for own fork in GitHub and then from within Android Studio VCS>Checkout from Version Control>GitHub
main.cpp@5:4bc41267a03a, 2014-09-02 (annotated)
- Committer:
- rgrover1
- Date:
- Tue Sep 02 16:32:58 2014 +0000
- Revision:
- 5:4bc41267a03a
- Parent:
- 2:e060367b9024
- Child:
- 6:e0fc9072e853
updating underlying libraries.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:e910d9bb040f | 1 | /* mbed Microcontroller Library |
yihui | 0:e910d9bb040f | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 0:e910d9bb040f | 3 | * |
yihui | 0:e910d9bb040f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 0:e910d9bb040f | 5 | * you may not use this file except in compliance with the License. |
yihui | 0:e910d9bb040f | 6 | * You may obtain a copy of the License at |
yihui | 0:e910d9bb040f | 7 | * |
yihui | 0:e910d9bb040f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 0:e910d9bb040f | 9 | * |
yihui | 0:e910d9bb040f | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 0:e910d9bb040f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 0:e910d9bb040f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 0:e910d9bb040f | 13 | * See the License for the specific language governing permissions and |
yihui | 0:e910d9bb040f | 14 | * limitations under the License. |
yihui | 0:e910d9bb040f | 15 | */ |
yihui | 0:e910d9bb040f | 16 | |
yihui | 0:e910d9bb040f | 17 | #include "mbed.h" |
Rohit Grover |
2:e060367b9024 | 18 | #include "BLEDevice.h" |
yihui | 0:e910d9bb040f | 19 | |
Rohit Grover |
2:e060367b9024 | 20 | #define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */ |
Rohit Grover |
2:e060367b9024 | 21 | #define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
Rohit Grover |
2:e060367b9024 | 22 | #define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
yihui | 0:e910d9bb040f | 23 | |
rgrover1 | 5:4bc41267a03a | 24 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
Rohit Grover |
2:e060367b9024 | 25 | * it will have an impact on code-size and power consumption. */ |
yihui | 0:e910d9bb040f | 26 | |
Rohit Grover |
2:e060367b9024 | 27 | #if NEED_CONSOLE_OUTPUT |
Rohit Grover |
2:e060367b9024 | 28 | Serial pc(USBTX, USBRX); |
Rohit Grover |
2:e060367b9024 | 29 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
yihui | 0:e910d9bb040f | 30 | #else |
Rohit Grover |
2:e060367b9024 | 31 | #define DEBUG(...) /* nothing */ |
Rohit Grover |
2:e060367b9024 | 32 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
yihui | 0:e910d9bb040f | 33 | |
Rohit Grover |
2:e060367b9024 | 34 | BLEDevice ble; |
Rohit Grover |
2:e060367b9024 | 35 | DigitalOut led1(LED1); |
yihui | 0:e910d9bb040f | 36 | |
yihui | 0:e910d9bb040f | 37 | // The Nordic UART Service |
Rohit Grover |
2:e060367b9024 | 38 | static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 39 | static const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 40 | static const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 41 | static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}; |
rgrover1 | 5:4bc41267a03a | 42 | |
rgrover1 | 5:4bc41267a03a | 43 | static const uint8_t SIZEOF_TX_RX_BUFFER = 128; |
rgrover1 | 5:4bc41267a03a | 44 | uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
rgrover1 | 5:4bc41267a03a | 45 | uint8_t txPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
rgrover1 | 5:4bc41267a03a | 46 | GattCharacteristic rxCharacteristic (uart_tx_uuid, rxPayload, 1, SIZEOF_TX_RX_BUFFER, |
Rohit Grover |
2:e060367b9024 | 47 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
rgrover1 | 5:4bc41267a03a | 48 | GattCharacteristic txCharacteristic (uart_rx_uuid, txPayload, 1, SIZEOF_TX_RX_BUFFER, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
rgrover1 | 5:4bc41267a03a | 49 | GattCharacteristic *uartChars[] = {&rxCharacteristic, &txCharacteristic}; |
Rohit Grover |
2:e060367b9024 | 50 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
yihui | 0:e910d9bb040f | 51 | |
rgrover1 | 5:4bc41267a03a | 52 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 53 | { |
Rohit Grover |
2:e060367b9024 | 54 | DEBUG("Disconnected!\n\r"); |
Rohit Grover |
2:e060367b9024 | 55 | DEBUG("Restarting the advertising process\n\r"); |
Rohit Grover |
2:e060367b9024 | 56 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 57 | } |
yihui | 0:e910d9bb040f | 58 | |
rgrover1 | 5:4bc41267a03a | 59 | void onDataWritten(uint16_t charHandle, const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 60 | { |
rgrover1 | 5:4bc41267a03a | 61 | if (charHandle == rxCharacteristic.getValueAttribute().getHandle()) { |
rgrover1 | 5:4bc41267a03a | 62 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 63 | DEBUG("received %u bytes\n\r", bytesRead); |
rgrover1 | 5:4bc41267a03a | 64 | if (bytesRead < sizeof(rxPayload)) { |
rgrover1 | 5:4bc41267a03a | 65 | memcpy(rxPayload, params->data, bytesRead); |
rgrover1 | 5:4bc41267a03a | 66 | rxPayload[bytesRead] = 0; |
rgrover1 | 5:4bc41267a03a | 67 | } |
rgrover1 | 5:4bc41267a03a | 68 | DEBUG("ECHO: %s\n\r", (char *)rxPayload); |
rgrover1 | 5:4bc41267a03a | 69 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), rxPayload, bytesRead); |
yihui | 0:e910d9bb040f | 70 | } |
Rohit Grover |
2:e060367b9024 | 71 | } |
yihui | 0:e910d9bb040f | 72 | |
Rohit Grover |
2:e060367b9024 | 73 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 74 | { |
Rohit Grover |
2:e060367b9024 | 75 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Rohit Grover |
2:e060367b9024 | 76 | } |
yihui | 0:e910d9bb040f | 77 | |
yihui | 0:e910d9bb040f | 78 | int main(void) |
yihui | 0:e910d9bb040f | 79 | { |
Rohit Grover |
2:e060367b9024 | 80 | led1 = 1; |
Rohit Grover |
2:e060367b9024 | 81 | Ticker ticker; |
Rohit Grover |
2:e060367b9024 | 82 | ticker.attach(periodicCallback, 1); |
yihui | 0:e910d9bb040f | 83 | |
Rohit Grover |
2:e060367b9024 | 84 | DEBUG("Initialising the nRF51822\n\r"); |
Rohit Grover |
2:e060367b9024 | 85 | ble.init(); |
Rohit Grover |
2:e060367b9024 | 86 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 87 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 88 | |
Rohit Grover |
2:e060367b9024 | 89 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 90 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 91 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 92 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Rohit Grover |
2:e060367b9024 | 93 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 94 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Rohit Grover |
2:e060367b9024 | 95 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
yihui | 0:e910d9bb040f | 96 | |
Rohit Grover |
2:e060367b9024 | 97 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
Rohit Grover |
2:e060367b9024 | 98 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 99 | |
Rohit Grover |
2:e060367b9024 | 100 | ble.addService(uartService); |
yihui | 0:e910d9bb040f | 101 | |
Rohit Grover |
2:e060367b9024 | 102 | while (true) { |
Rohit Grover |
2:e060367b9024 | 103 | ble.waitForEvent(); |
yihui | 0:e910d9bb040f | 104 | } |
yihui | 0:e910d9bb040f | 105 | } |