BLE GATT Button on Board Example Nucleo IDB0XA1

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_GAP_Example by Bluetooth Low Energy

Committer:
anoney180133
Date:
Fri Dec 11 13:15:46 2015 +0000
Revision:
14:4af58166825c
Parent:
13:827dd2b32bb8
BLE GATT Button on Board Example Nucleo IDB0XA1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:5375be4301ed 1 #include "mbed.h"
anoney180133 14:4af58166825c 2 #include "ble/BLE.h"
anoney180133 14:4af58166825c 3 #include "ble/Gap.h"
anoney180133 14:4af58166825c 4 #include "ble/services/BatteryService.h"
anoney180133 14:4af58166825c 5 #include "ble/services/DeviceInformationService.h"
mbedAustin 0:5375be4301ed 6
anoney180133 14:4af58166825c 7 #define BUTTON_SERVICE_UUID_0 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 8 #define BUTTON_SERVICE_UUID_1 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 9 #define BUTTON_SERVICE_UUID_2 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 10 #define BUTTON_SERVICE_UUID_3 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 11 #define BUTTON_SERVICE_UUID_4 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 12 #define BUTTON_SERVICE_UUID_5 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 13 #define BUTTON_SERVICE_UUID_6 0x0000 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 14 #define BUTTON_SERVICE_UUID_7 0xFFF0 // Custom SERVICE_UUID
anoney180133 14:4af58166825c 15
anoney180133 14:4af58166825c 16 #define BUTTON_STATE_CHARACTERISTIC_UUID_0 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 17 #define BUTTON_STATE_CHARACTERISTIC_UUID_1 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 18 #define BUTTON_STATE_CHARACTERISTIC_UUID_2 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 19 #define BUTTON_STATE_CHARACTERISTIC_UUID_3 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 20 #define BUTTON_STATE_CHARACTERISTIC_UUID_4 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 21 #define BUTTON_STATE_CHARACTERISTIC_UUID_5 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 22 #define BUTTON_STATE_CHARACTERISTIC_UUID_6 0x0000 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 23 #define BUTTON_STATE_CHARACTERISTIC_UUID_7 0xFFF1 // Custom CHARACTERISTIC_UUID
anoney180133 14:4af58166825c 24
anoney180133 14:4af58166825c 25 BLE ble;
mbedAustin 0:5375be4301ed 26
anoney180133 14:4af58166825c 27 DigitalOut led1(LED1);
anoney180133 14:4af58166825c 28 InterruptIn int_external(PC_13);
anoney180133 14:4af58166825c 29 DigitalIn Push_Button(PC_13,PullNone);
anoney180133 14:4af58166825c 30
anoney180133 14:4af58166825c 31 static const char DEVICE_NAME[] = "CESA BLE 4.0 Button";
anoney180133 14:4af58166825c 32 //static const uint16_t service_uuid16_list[] = {BUTTON_SERVICE_UUID_7};
anoney180133 14:4af58166825c 33 static const uint16_t service_uuid128_list[] = {BUTTON_SERVICE_UUID_7};
anoney180133 14:4af58166825c 34 static const uint16_t characteristic_uuid128_list[] = {BUTTON_STATE_CHARACTERISTIC_UUID_7};
anoney180133 14:4af58166825c 35
anoney180133 14:4af58166825c 36 bool buttonPressed = false;
anoney180133 14:4af58166825c 37
anoney180133 14:4af58166825c 38 //buttonState is accessible to button callbacks(CHARACTERISTIC_UUID)
anoney180133 14:4af58166825c 39 ReadOnlyGattCharacteristic<bool> buttonState((uint8_t *)characteristic_uuid128_list,&buttonPressed,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
mbedAustin 5:fff16d283dcf 40
anoney180133 14:4af58166825c 41
anoney180133 14:4af58166825c 42 void buttonPressedCallback(void)
anoney180133 14:4af58166825c 43 {
anoney180133 14:4af58166825c 44 buttonPressed = true;
anoney180133 14:4af58166825c 45 ble.updateCharacteristicValue(buttonState.getValueHandle(),(uint8_t *)&buttonPressed, sizeof(bool));
anoney180133 14:4af58166825c 46 }
mbedAustin 1:0692bee84264 47
anoney180133 14:4af58166825c 48 void buttonReleasedCallback(void)
anoney180133 14:4af58166825c 49 {
anoney180133 14:4af58166825c 50 buttonPressed = false;
anoney180133 14:4af58166825c 51 ble.updateCharacteristicValue(buttonState.getValueHandle(),(uint8_t *)&buttonPressed, sizeof(bool));
anoney180133 14:4af58166825c 52 }
anoney180133 14:4af58166825c 53
anoney180133 14:4af58166825c 54 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbedAustin 13:827dd2b32bb8 55 {
mbedAustin 13:827dd2b32bb8 56 ble.startAdvertising();
mbedAustin 8:5442739198ec 57 }
mbedAustin 8:5442739198ec 58
anoney180133 14:4af58166825c 59 void periodicCallback(void)
anoney180133 14:4af58166825c 60 {
anoney180133 14:4af58166825c 61 //led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
anoney180133 14:4af58166825c 62 }
anoney180133 14:4af58166825c 63
mbedAustin 1:0692bee84264 64 int main(void)
mbedAustin 0:5375be4301ed 65 {
anoney180133 14:4af58166825c 66 led1 = 1;
anoney180133 14:4af58166825c 67 Ticker ticker;
anoney180133 14:4af58166825c 68 ticker.attach(periodicCallback, 1);
anoney180133 14:4af58166825c 69 int_external.fall(&buttonPressedCallback);
anoney180133 14:4af58166825c 70 int_external.rise(&buttonReleasedCallback);
anoney180133 14:4af58166825c 71
mbedAustin 1:0692bee84264 72 ble.init();
anoney180133 14:4af58166825c 73 ble.onDisconnection(disconnectionCallback);
mbedAustin 13:827dd2b32bb8 74
anoney180133 14:4af58166825c 75 GattCharacteristic *charTable[] = {&buttonState};
anoney180133 14:4af58166825c 76 GattService buttonService((uint8_t *)service_uuid128_list, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
anoney180133 14:4af58166825c 77 ble.addService(buttonService);
mbedAustin 1:0692bee84264 78
anoney180133 14:4af58166825c 79 /* setup advertising */
anoney180133 14:4af58166825c 80 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
anoney180133 14:4af58166825c 81 //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)service_uuid16_list, sizeof(service_uuid16_list));
anoney180133 14:4af58166825c 82 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(uint8_t *)service_uuid128_list, sizeof(service_uuid128_list));
anoney180133 14:4af58166825c 83 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,(uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbedAustin 4:d602b1c3aef4 84 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
anoney180133 14:4af58166825c 85
anoney180133 14:4af58166825c 86 ble.setAdvertisingInterval(100); /* 100 ms. */
mbedAustin 1:0692bee84264 87 ble.startAdvertising();
mbedAustin 1:0692bee84264 88
anoney180133 14:4af58166825c 89 while (true)
anoney180133 14:4af58166825c 90 {
anoney180133 14:4af58166825c 91 ble.waitForEvent();
mbedAustin 1:0692bee84264 92 }
anoney180133 14:4af58166825c 93 }