Esempio Gatt Base
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@22:f9fd5024f7ea, 2018-01-10 (annotated)
- Committer:
- fdalforno
- Date:
- Wed Jan 10 20:19:29 2018 +0000
- Revision:
- 22:f9fd5024f7ea
- Parent:
- 21:0e7c08f5386f
Esempio GATT Base
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:eb7f02ad28a7 | 1 | #include "mbed.h" |
screamer | 0:eb7f02ad28a7 | 2 | #include "ble/BLE.h" |
fdalforno | 22:f9fd5024f7ea | 3 | |
fdalforno | 22:f9fd5024f7ea | 4 | DigitalOut led(LED1, 1); |
fdalforno | 22:f9fd5024f7ea | 5 | uint16_t customServiceUUID = 0xA000; |
fdalforno | 22:f9fd5024f7ea | 6 | uint16_t readCharUUID = 0xA001; |
fdalforno | 22:f9fd5024f7ea | 7 | uint16_t writeCharUUID = 0xA002; |
fdalforno | 22:f9fd5024f7ea | 8 | |
fdalforno | 22:f9fd5024f7ea | 9 | const static char DEVICE_NAME[] = "Pippo"; // change this |
fdalforno | 22:f9fd5024f7ea | 10 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
fdalforno | 22:f9fd5024f7ea | 11 | |
fdalforno | 22:f9fd5024f7ea | 12 | /* Set Up custom Characteristics */ |
fdalforno | 22:f9fd5024f7ea | 13 | static uint8_t readValue[10] = {0}; |
fdalforno | 22:f9fd5024f7ea | 14 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
fdalforno | 22:f9fd5024f7ea | 15 | |
fdalforno | 22:f9fd5024f7ea | 16 | static uint8_t writeValue[10] = {0}; |
fdalforno | 22:f9fd5024f7ea | 17 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
fdalforno | 22:f9fd5024f7ea | 18 | |
fdalforno | 22:f9fd5024f7ea | 19 | /* Set up custom service */ |
fdalforno | 22:f9fd5024f7ea | 20 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
fdalforno | 22:f9fd5024f7ea | 21 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
fdalforno | 22:f9fd5024f7ea | 22 | |
fdalforno | 22:f9fd5024f7ea | 23 | |
fdalforno | 22:f9fd5024f7ea | 24 | /* |
fdalforno | 22:f9fd5024f7ea | 25 | * Restart advertising when phone app disconnects |
fdalforno | 22:f9fd5024f7ea | 26 | */ |
fdalforno | 22:f9fd5024f7ea | 27 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
screamer | 0:eb7f02ad28a7 | 28 | { |
fdalforno | 22:f9fd5024f7ea | 29 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 30 | } |
fdalforno | 22:f9fd5024f7ea | 31 | |
fdalforno | 22:f9fd5024f7ea | 32 | /* |
fdalforno | 22:f9fd5024f7ea | 33 | * Handle writes to writeCharacteristic |
fdalforno | 22:f9fd5024f7ea | 34 | */ |
fdalforno | 22:f9fd5024f7ea | 35 | void writeCharCallback(const GattWriteCallbackParams *params) |
screamer | 0:eb7f02ad28a7 | 36 | { |
fdalforno | 22:f9fd5024f7ea | 37 | /* Check to see what characteristic was written, by handle */ |
fdalforno | 22:f9fd5024f7ea | 38 | if(params->handle == writeChar.getValueHandle()) { |
fdalforno | 22:f9fd5024f7ea | 39 | /* toggle LED if only 1 byte is written */ |
fdalforno | 22:f9fd5024f7ea | 40 | if(params->len == 1) { |
fdalforno | 22:f9fd5024f7ea | 41 | led = params->data[0]; |
fdalforno | 22:f9fd5024f7ea | 42 | (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle |
fdalforno | 22:f9fd5024f7ea | 43 | } |
fdalforno | 22:f9fd5024f7ea | 44 | /* Print the data if more than 1 byte is written */ |
fdalforno | 22:f9fd5024f7ea | 45 | else { |
fdalforno | 22:f9fd5024f7ea | 46 | printf("Data received: length = %d, data = 0x",params->len); |
fdalforno | 22:f9fd5024f7ea | 47 | for(int x=0; x < params->len; x++) { |
fdalforno | 22:f9fd5024f7ea | 48 | printf("%x", params->data[x]); |
fdalforno | 22:f9fd5024f7ea | 49 | } |
fdalforno | 22:f9fd5024f7ea | 50 | printf("\n\r"); |
fdalforno | 22:f9fd5024f7ea | 51 | } |
fdalforno | 22:f9fd5024f7ea | 52 | /* Update the readChar with the value of writeChar */ |
fdalforno | 22:f9fd5024f7ea | 53 | BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); |
fdalforno | 22:f9fd5024f7ea | 54 | } |
screamer | 0:eb7f02ad28a7 | 55 | } |
fdalforno | 22:f9fd5024f7ea | 56 | /* |
fdalforno | 22:f9fd5024f7ea | 57 | * Initialization callback |
fdalforno | 22:f9fd5024f7ea | 58 | */ |
apalmieri | 13:227a0149b677 | 59 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
screamer | 0:eb7f02ad28a7 | 60 | { |
fdalforno | 22:f9fd5024f7ea | 61 | BLE &ble = params->ble; |
apalmieri | 13:227a0149b677 | 62 | ble_error_t error = params->error; |
fdalforno | 22:f9fd5024f7ea | 63 | |
apalmieri | 13:227a0149b677 | 64 | if (error != BLE_ERROR_NONE) { |
apalmieri | 13:227a0149b677 | 65 | return; |
apalmieri | 13:227a0149b677 | 66 | } |
fdalforno | 22:f9fd5024f7ea | 67 | |
screamer | 0:eb7f02ad28a7 | 68 | ble.gap().onDisconnection(disconnectionCallback); |
fdalforno | 22:f9fd5024f7ea | 69 | ble.gattServer().onDataWritten(writeCharCallback); |
fdalforno | 22:f9fd5024f7ea | 70 | |
fdalforno | 22:f9fd5024f7ea | 71 | /* Setup advertising */ |
fdalforno | 22:f9fd5024f7ea | 72 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
fdalforno | 22:f9fd5024f7ea | 73 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
fdalforno | 22:f9fd5024f7ea | 74 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
fdalforno | 22:f9fd5024f7ea | 75 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
fdalforno | 22:f9fd5024f7ea | 76 | ble.gap().setAdvertisingInterval(100); // 100ms. |
fdalforno | 22:f9fd5024f7ea | 77 | |
fdalforno | 22:f9fd5024f7ea | 78 | /* Add our custom service */ |
fdalforno | 22:f9fd5024f7ea | 79 | ble.addService(customService); |
fdalforno | 22:f9fd5024f7ea | 80 | |
fdalforno | 22:f9fd5024f7ea | 81 | /* Start advertising */ |
screamer | 0:eb7f02ad28a7 | 82 | ble.gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 83 | } |
fdalforno | 22:f9fd5024f7ea | 84 | |
fdalforno | 22:f9fd5024f7ea | 85 | /* |
fdalforno | 22:f9fd5024f7ea | 86 | * Main loop |
fdalforno | 22:f9fd5024f7ea | 87 | */ |
apalmieri | 13:227a0149b677 | 88 | int main(void) |
apalmieri | 13:227a0149b677 | 89 | { |
fdalforno | 22:f9fd5024f7ea | 90 | /* initialize stuff */ |
fdalforno | 22:f9fd5024f7ea | 91 | printf("\n\r********* Starting Main Loop *********\n\r"); |
fdalforno | 22:f9fd5024f7ea | 92 | |
fdalforno | 22:f9fd5024f7ea | 93 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
fdalforno | 22:f9fd5024f7ea | 94 | ble.init(bleInitComplete); |
fdalforno | 22:f9fd5024f7ea | 95 | |
fdalforno | 22:f9fd5024f7ea | 96 | /* SpinWait for initialization to complete. This is necessary because the |
fdalforno | 22:f9fd5024f7ea | 97 | * BLE object is used in the main loop below. */ |
fdalforno | 22:f9fd5024f7ea | 98 | while (ble.hasInitialized() == false) { /* spin loop */ } |
fdalforno | 22:f9fd5024f7ea | 99 | |
fdalforno | 22:f9fd5024f7ea | 100 | /* Infinite loop waiting for BLE interrupt events */ |
fdalforno | 22:f9fd5024f7ea | 101 | while (true) { |
fdalforno | 22:f9fd5024f7ea | 102 | ble.waitForEvent(); /* Save power */ |
fdalforno | 22:f9fd5024f7ea | 103 | } |
fdalforno | 22:f9fd5024f7ea | 104 | } |