BLE UART example for Nucloe board and Bluetooth LE Nucleo shield.

Dependencies:   Nucleo_BLE_API Nucleo_BLE_BlueNRG mbed

Warning: Deprecated!

Supported drivers and applications can be found at this link.

Committer:
sjallouli
Date:
Fri Dec 19 19:53:05 2014 +0000
Revision:
0:3e2ce2bb50b9
Child:
1:69e44344edaa
change the USARTService->write method access permission to public

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjallouli 0:3e2ce2bb50b9 1 /* mbed Microcontroller Library
sjallouli 0:3e2ce2bb50b9 2 * Copyright (c) 2006-2013 ARM Limited
sjallouli 0:3e2ce2bb50b9 3 *
sjallouli 0:3e2ce2bb50b9 4 * Licensed under the Apache License, Version 2.0 (the "License");
sjallouli 0:3e2ce2bb50b9 5 * you may not use this file except in compliance with the License.
sjallouli 0:3e2ce2bb50b9 6 * You may obtain a copy of the License at
sjallouli 0:3e2ce2bb50b9 7 *
sjallouli 0:3e2ce2bb50b9 8 * http://www.apache.org/licenses/LICENSE-2.0
sjallouli 0:3e2ce2bb50b9 9 *
sjallouli 0:3e2ce2bb50b9 10 * Unless required by applicable law or agreed to in writing, software
sjallouli 0:3e2ce2bb50b9 11 * distributed under the License is distributed on an "AS IS" BASIS,
sjallouli 0:3e2ce2bb50b9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sjallouli 0:3e2ce2bb50b9 13 * See the License for the specific language governing permissions and
sjallouli 0:3e2ce2bb50b9 14 * limitations under the License.
sjallouli 0:3e2ce2bb50b9 15 */
sjallouli 0:3e2ce2bb50b9 16
sjallouli 0:3e2ce2bb50b9 17 #include "mbed.h"
sjallouli 0:3e2ce2bb50b9 18 #include "BLEDevice.h"
sjallouli 0:3e2ce2bb50b9 19 #include "UARTService.h"
sjallouli 0:3e2ce2bb50b9 20 #include "Utils.h"
sjallouli 0:3e2ce2bb50b9 21
sjallouli 0:3e2ce2bb50b9 22
sjallouli 0:3e2ce2bb50b9 23 #define BLE_UART_SEND(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); }
sjallouli 0:3e2ce2bb50b9 24
sjallouli 0:3e2ce2bb50b9 25
sjallouli 0:3e2ce2bb50b9 26 BLEDevice ble;
sjallouli 0:3e2ce2bb50b9 27 DigitalOut led1(LED1);
sjallouli 0:3e2ce2bb50b9 28 DigitalIn mybutton(USER_BUTTON);
sjallouli 0:3e2ce2bb50b9 29
sjallouli 0:3e2ce2bb50b9 30 bool connected = false;
sjallouli 0:3e2ce2bb50b9 31
sjallouli 0:3e2ce2bb50b9 32 UARTService *uartServicePtr;
sjallouli 0:3e2ce2bb50b9 33
sjallouli 0:3e2ce2bb50b9 34 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
sjallouli 0:3e2ce2bb50b9 35 {
sjallouli 0:3e2ce2bb50b9 36 DEBUG("Disconnected!\n\r");
sjallouli 0:3e2ce2bb50b9 37 DEBUG("Restarting the advertising process\n\r");
sjallouli 0:3e2ce2bb50b9 38 ble.startAdvertising();
sjallouli 0:3e2ce2bb50b9 39 connected = false;
sjallouli 0:3e2ce2bb50b9 40 }
sjallouli 0:3e2ce2bb50b9 41
sjallouli 0:3e2ce2bb50b9 42 void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
sjallouli 0:3e2ce2bb50b9 43 {
sjallouli 0:3e2ce2bb50b9 44 DEBUG("Connected\r\n");
sjallouli 0:3e2ce2bb50b9 45 connected = true;
sjallouli 0:3e2ce2bb50b9 46 }
sjallouli 0:3e2ce2bb50b9 47
sjallouli 0:3e2ce2bb50b9 48 void onDataWritten(const GattCharacteristicWriteCBParams *params)
sjallouli 0:3e2ce2bb50b9 49 {
sjallouli 0:3e2ce2bb50b9 50 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
sjallouli 0:3e2ce2bb50b9 51 uint16_t bytesRead = params->len;
sjallouli 0:3e2ce2bb50b9 52 DEBUG("received %u bytes\n\r", bytesRead);
sjallouli 0:3e2ce2bb50b9 53 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
sjallouli 0:3e2ce2bb50b9 54 }
sjallouli 0:3e2ce2bb50b9 55 }
sjallouli 0:3e2ce2bb50b9 56
sjallouli 0:3e2ce2bb50b9 57 void periodicCallback(void)
sjallouli 0:3e2ce2bb50b9 58 {
sjallouli 0:3e2ce2bb50b9 59 led1 = !led1;
sjallouli 0:3e2ce2bb50b9 60 }
sjallouli 0:3e2ce2bb50b9 61
sjallouli 0:3e2ce2bb50b9 62 int main(void)
sjallouli 0:3e2ce2bb50b9 63 {
sjallouli 0:3e2ce2bb50b9 64 led1 = 1;
sjallouli 0:3e2ce2bb50b9 65 Ticker ticker;
sjallouli 0:3e2ce2bb50b9 66 ticker.attach(periodicCallback, 1);
sjallouli 0:3e2ce2bb50b9 67
sjallouli 0:3e2ce2bb50b9 68 DEBUG("Initialising \n\r");
sjallouli 0:3e2ce2bb50b9 69 ble.init();
sjallouli 0:3e2ce2bb50b9 70 ble.onDisconnection(disconnectionCallback);
sjallouli 0:3e2ce2bb50b9 71 ble.onConnection(connectionCallback);
sjallouli 0:3e2ce2bb50b9 72 ble.onDataWritten(onDataWritten);
sjallouli 0:3e2ce2bb50b9 73
sjallouli 0:3e2ce2bb50b9 74 /* setup advertising */
sjallouli 0:3e2ce2bb50b9 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
sjallouli 0:3e2ce2bb50b9 76 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
sjallouli 0:3e2ce2bb50b9 77 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
sjallouli 0:3e2ce2bb50b9 78 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
sjallouli 0:3e2ce2bb50b9 79
sjallouli 0:3e2ce2bb50b9 80 ble.setAdvertisingInterval(160);
sjallouli 0:3e2ce2bb50b9 81 ble.startAdvertising();
sjallouli 0:3e2ce2bb50b9 82
sjallouli 0:3e2ce2bb50b9 83 UARTService uartService(ble);
sjallouli 0:3e2ce2bb50b9 84 uartServicePtr = &uartService;
sjallouli 0:3e2ce2bb50b9 85
sjallouli 0:3e2ce2bb50b9 86
sjallouli 0:3e2ce2bb50b9 87 while (true)
sjallouli 0:3e2ce2bb50b9 88 {
sjallouli 0:3e2ce2bb50b9 89 ble.waitForEvent();
sjallouli 0:3e2ce2bb50b9 90
sjallouli 0:3e2ce2bb50b9 91 if(connected == true)
sjallouli 0:3e2ce2bb50b9 92 {
sjallouli 0:3e2ce2bb50b9 93 if (mybutton == 0)
sjallouli 0:3e2ce2bb50b9 94 {
sjallouli 0:3e2ce2bb50b9 95 DEBUG("ABCDEFGHIJKLMNOPQRST");
sjallouli 0:3e2ce2bb50b9 96 BLE_UART_SEND("ABCDEFGHIJKLMNOPQRST");
sjallouli 0:3e2ce2bb50b9 97
sjallouli 0:3e2ce2bb50b9 98 while(mybutton == 0);
sjallouli 0:3e2ce2bb50b9 99 }
sjallouli 0:3e2ce2bb50b9 100 }
sjallouli 0:3e2ce2bb50b9 101 }
sjallouli 0:3e2ce2bb50b9 102 }