This is a simple program to work with RedBearLab BLE Controller App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal.

Dependencies:   BLE_API mbed nRF51822

Committer:
RedBearLab
Date:
Fri Oct 31 09:37:18 2014 +0000
Revision:
0:cffe8ac1bdf0
Child:
1:1c058e553423
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:cffe8ac1bdf0 1
RedBearLab 0:cffe8ac1bdf0 2 #include "mbed.h"
RedBearLab 0:cffe8ac1bdf0 3 #include "BLEDevice.h"
RedBearLab 0:cffe8ac1bdf0 4
RedBearLab 0:cffe8ac1bdf0 5
RedBearLab 0:cffe8ac1bdf0 6 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 7 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 8 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 9
RedBearLab 0:cffe8ac1bdf0 10 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 11
RedBearLab 0:cffe8ac1bdf0 12 BLEDevice ble;
RedBearLab 0:cffe8ac1bdf0 13
RedBearLab 0:cffe8ac1bdf0 14 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 15
RedBearLab 0:cffe8ac1bdf0 16
RedBearLab 0:cffe8ac1bdf0 17 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 18 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 19 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 20 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 21 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:cffe8ac1bdf0 22
RedBearLab 0:cffe8ac1bdf0 23
RedBearLab 0:cffe8ac1bdf0 24 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 25 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 26
RedBearLab 0:cffe8ac1bdf0 27 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 28 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
RedBearLab 0:cffe8ac1bdf0 32
RedBearLab 0:cffe8ac1bdf0 33 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 36
RedBearLab 0:cffe8ac1bdf0 37 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 38
RedBearLab 0:cffe8ac1bdf0 39
RedBearLab 0:cffe8ac1bdf0 40
RedBearLab 0:cffe8ac1bdf0 41 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 42 {
RedBearLab 0:cffe8ac1bdf0 43 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 44 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 45 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 46 }
RedBearLab 0:cffe8ac1bdf0 47
RedBearLab 0:cffe8ac1bdf0 48 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler)
RedBearLab 0:cffe8ac1bdf0 49 {
RedBearLab 0:cffe8ac1bdf0 50 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 51 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 0:cffe8ac1bdf0 53 if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 54 {
RedBearLab 0:cffe8ac1bdf0 55 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
RedBearLab 0:cffe8ac1bdf0 56 memset(txPayload, 0, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 57 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 58 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 59 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 60 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 61 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 62 pc.printf("Data: ");
RedBearLab 0:cffe8ac1bdf0 63 for(index=0; index<bytesRead; index++)
RedBearLab 0:cffe8ac1bdf0 64 {
RedBearLab 0:cffe8ac1bdf0 65 pc.putc(txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 66 }
RedBearLab 0:cffe8ac1bdf0 67 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 68 }
RedBearLab 0:cffe8ac1bdf0 69 }
RedBearLab 0:cffe8ac1bdf0 70
RedBearLab 0:cffe8ac1bdf0 71 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 72 {
RedBearLab 0:cffe8ac1bdf0 73 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 74 {
RedBearLab 0:cffe8ac1bdf0 75 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 76 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 77 {
RedBearLab 0:cffe8ac1bdf0 78 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 79 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 80 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 81 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 82 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 83 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 84 break;
RedBearLab 0:cffe8ac1bdf0 85 }
RedBearLab 0:cffe8ac1bdf0 86 }
RedBearLab 0:cffe8ac1bdf0 87 }
RedBearLab 0:cffe8ac1bdf0 88
RedBearLab 0:cffe8ac1bdf0 89 int main(void)
RedBearLab 0:cffe8ac1bdf0 90 {
RedBearLab 0:cffe8ac1bdf0 91 ble.init();
RedBearLab 0:cffe8ac1bdf0 92 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 93 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 94
RedBearLab 0:cffe8ac1bdf0 95 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 96 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 97
RedBearLab 0:cffe8ac1bdf0 98 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 99 // setup advertising
RedBearLab 0:cffe8ac1bdf0 100 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 101 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 102 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
RedBearLab 0:cffe8ac1bdf0 103 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
RedBearLab 0:cffe8ac1bdf0 104 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 105 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 106 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 107 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 108
RedBearLab 0:cffe8ac1bdf0 109 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 110
RedBearLab 0:cffe8ac1bdf0 111 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 112 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 113
RedBearLab 0:cffe8ac1bdf0 114 while(1)
RedBearLab 0:cffe8ac1bdf0 115 {
RedBearLab 0:cffe8ac1bdf0 116 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 117 }
RedBearLab 0:cffe8ac1bdf0 118 }
RedBearLab 0:cffe8ac1bdf0 119
RedBearLab 0:cffe8ac1bdf0 120
RedBearLab 0:cffe8ac1bdf0 121
RedBearLab 0:cffe8ac1bdf0 122
RedBearLab 0:cffe8ac1bdf0 123
RedBearLab 0:cffe8ac1bdf0 124
RedBearLab 0:cffe8ac1bdf0 125
RedBearLab 0:cffe8ac1bdf0 126
RedBearLab 0:cffe8ac1bdf0 127
RedBearLab 0:cffe8ac1bdf0 128
RedBearLab 0:cffe8ac1bdf0 129
RedBearLab 0:cffe8ac1bdf0 130
RedBearLab 0:cffe8ac1bdf0 131
RedBearLab 0:cffe8ac1bdf0 132
RedBearLab 0:cffe8ac1bdf0 133
RedBearLab 0:cffe8ac1bdf0 134