BLE GATT LED on Board Nucleo with IDB0XA1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_GAP_Example by Bluetooth Low Energy

Committer:
anoney180133
Date:
Fri Dec 11 13:24:16 2015 +0000
Revision:
14:414e9bbb2701
Parent:
13:827dd2b32bb8
asdasd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:5375be4301ed 1 #include "mbed.h"
anoney180133 14:414e9bbb2701 2 #include "ble/BLE.h"
anoney180133 14:414e9bbb2701 3 #include "ble/Gap.h"
anoney180133 14:414e9bbb2701 4 #include "ble/services/BatteryService.h"
anoney180133 14:414e9bbb2701 5 #include "ble/services/DeviceInformationService.h"
mbedAustin 0:5375be4301ed 6
anoney180133 14:414e9bbb2701 7 #define LED_SERVICE_UUID_0 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 8 #define LED_SERVICE_UUID_1 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 9 #define LED_SERVICE_UUID_2 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 10 #define LED_SERVICE_UUID_3 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 11 #define LED_SERVICE_UUID_4 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 12 #define LED_SERVICE_UUID_5 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 13 #define LED_SERVICE_UUID_6 0x0000 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 14 #define LED_SERVICE_UUID_7 0xFFE0 // Custom SERVICE_UUID
anoney180133 14:414e9bbb2701 15
anoney180133 14:414e9bbb2701 16 #define LED_STATE_CHARACTERISTIC_UUID_0 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 17 #define LED_STATE_CHARACTERISTIC_UUID_1 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 18 #define LED_STATE_CHARACTERISTIC_UUID_2 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 19 #define LED_STATE_CHARACTERISTIC_UUID_3 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 20 #define LED_STATE_CHARACTERISTIC_UUID_4 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 21 #define LED_STATE_CHARACTERISTIC_UUID_5 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 22 #define LED_STATE_CHARACTERISTIC_UUID_6 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:414e9bbb2701 23 #define LED_STATE_CHARACTERISTIC_UUID_7 0xFFE1 // Custom CHARACTERISTIC_UUID
mbedAustin 0:5375be4301ed 24
anoney180133 14:414e9bbb2701 25 BLE ble;
anoney180133 14:414e9bbb2701 26
anoney180133 14:414e9bbb2701 27 DigitalOut led1(LED1);
anoney180133 14:414e9bbb2701 28
anoney180133 14:414e9bbb2701 29 //------------------------------------
anoney180133 14:414e9bbb2701 30 // Hyperterminal configuration
anoney180133 14:414e9bbb2701 31 // 115200 bauds(Default 9600), 8-bit data, no parity
anoney180133 14:414e9bbb2701 32 //------------------------------------
anoney180133 14:414e9bbb2701 33 /* No need Serial to UART just usb debugger. */
anoney180133 14:414e9bbb2701 34
anoney180133 14:414e9bbb2701 35 Serial UART(SERIAL_TX, SERIAL_RX); // TX PA_2 , RX PA_3
mbedAustin 5:fff16d283dcf 36
anoney180133 14:414e9bbb2701 37 static const char DEVICE_NAME[] = "CESA BLE 4.0 LED";
anoney180133 14:414e9bbb2701 38 //static const uint16_t service_uuid16_list[] = {LED_SERVICE_UUID_7};
anoney180133 14:414e9bbb2701 39 static const uint16_t service_uuid128_list[] = {LED_SERVICE_UUID_7};
anoney180133 14:414e9bbb2701 40 static const uint16_t characteristic_uuid128_list[] = {LED_STATE_CHARACTERISTIC_UUID_7};
mbedAustin 1:0692bee84264 41
anoney180133 14:414e9bbb2701 42 bool LEDCharacteristic_Value = false;
anoney180133 14:414e9bbb2701 43
anoney180133 14:414e9bbb2701 44 //LEDState is accessible to LED callbacks(CHARACTERISTIC_UUID)
anoney180133 14:414e9bbb2701 45 ReadWriteGattCharacteristic<bool> ledState((uint8_t *)characteristic_uuid128_list,&LEDCharacteristic_Value,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
anoney180133 14:414e9bbb2701 46
anoney180133 14:414e9bbb2701 47
anoney180133 14:414e9bbb2701 48 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbedAustin 13:827dd2b32bb8 49 {
mbedAustin 13:827dd2b32bb8 50 ble.startAdvertising();
mbedAustin 8:5442739198ec 51 }
mbedAustin 8:5442739198ec 52
anoney180133 14:414e9bbb2701 53 void periodicCallback(void)
anoney180133 14:414e9bbb2701 54 {
anoney180133 14:414e9bbb2701 55 //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
anoney180133 14:414e9bbb2701 56 }
anoney180133 14:414e9bbb2701 57
anoney180133 14:414e9bbb2701 58
anoney180133 14:414e9bbb2701 59 void onDataWrittenCallback(const GattWriteCallbackParams *params)
anoney180133 14:414e9bbb2701 60 {
anoney180133 14:414e9bbb2701 61 if ((params->handle == ledState.getValueHandle()) && (params->len == 1))
anoney180133 14:414e9bbb2701 62 {
anoney180133 14:414e9bbb2701 63 led1 = params->data[0];
anoney180133 14:414e9bbb2701 64
anoney180133 14:414e9bbb2701 65 printf("data = %d\r\n",params->data[0]);
anoney180133 14:414e9bbb2701 66
anoney180133 14:414e9bbb2701 67 }
anoney180133 14:414e9bbb2701 68 }
anoney180133 14:414e9bbb2701 69
mbedAustin 1:0692bee84264 70 int main(void)
mbedAustin 0:5375be4301ed 71 {
anoney180133 14:414e9bbb2701 72 led1 = 0;
anoney180133 14:414e9bbb2701 73 Ticker ticker;
anoney180133 14:414e9bbb2701 74 ticker.attach(periodicCallback, 1);
anoney180133 14:414e9bbb2701 75
anoney180133 14:414e9bbb2701 76 UART.baud(115200); // Set BuadRate
anoney180133 14:414e9bbb2701 77
mbedAustin 1:0692bee84264 78 ble.init();
anoney180133 14:414e9bbb2701 79 ble.onDisconnection(disconnectionCallback);
anoney180133 14:414e9bbb2701 80 ble.onDataWritten(onDataWrittenCallback);
mbedAustin 13:827dd2b32bb8 81
anoney180133 14:414e9bbb2701 82 GattCharacteristic *charTable[] = {&ledState};
anoney180133 14:414e9bbb2701 83 GattService LEDService((uint8_t *)service_uuid128_list, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
anoney180133 14:414e9bbb2701 84 ble.addService(LEDService);
mbedAustin 1:0692bee84264 85
anoney180133 14:414e9bbb2701 86 /* setup advertising */
anoney180133 14:414e9bbb2701 87 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
anoney180133 14:414e9bbb2701 88 //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)service_uuid16_list, sizeof(service_uuid16_list));
anoney180133 14:414e9bbb2701 89 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(uint8_t *)service_uuid128_list, sizeof(service_uuid128_list));
anoney180133 14:414e9bbb2701 90 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,(uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbedAustin 4:d602b1c3aef4 91 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
anoney180133 14:414e9bbb2701 92
anoney180133 14:414e9bbb2701 93 ble.setAdvertisingInterval(100); /* 100 ms. */
mbedAustin 1:0692bee84264 94 ble.startAdvertising();
mbedAustin 1:0692bee84264 95
anoney180133 14:414e9bbb2701 96 while (true)
anoney180133 14:414e9bbb2701 97 {
anoney180133 14:414e9bbb2701 98 ble.waitForEvent();
mbedAustin 1:0692bee84264 99 }
anoney180133 14:414e9bbb2701 100 }