Workings and tests to create custom GATT services, for use as part of peripheral communication in cars
Dependencies: BLE_API mbed nRF51822
BroadcasterService.h@8:3376f79e7d50, 2015-02-24 (annotated)
- Committer:
- alexanderlea
- Date:
- Tue Feb 24 15:38:16 2015 +0000
- Revision:
- 8:3376f79e7d50
- Parent:
- 7:5c983cf3c352
- Child:
- 9:0ed64b14d46b
Tidied up UUIDs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexanderlea | 7:5c983cf3c352 | 1 | #ifndef __BLE_BROADCASTER_SERVICE_H__ |
alexanderlea | 7:5c983cf3c352 | 2 | #define __BLE_BROADCASTER_SERVICE_H__ |
alexanderlea | 0:af868ad47854 | 3 | |
alexanderlea | 0:af868ad47854 | 4 | #include "BLEDevice.h" |
alexanderlea | 0:af868ad47854 | 5 | |
alexanderlea | 0:af868ad47854 | 6 | /** |
alexanderlea | 7:5c983cf3c352 | 7 | * @class BroadcasterService |
alexanderlea | 7:5c983cf3c352 | 8 | * @brief Based heavily on the BLE Battery Service, the aim is to send key, pair values <br> |
alexanderlea | 0:af868ad47854 | 9 | */ |
alexanderlea | 7:5c983cf3c352 | 10 | class BroadcasterService |
alexanderlea | 7:5c983cf3c352 | 11 | { |
alexanderlea | 0:af868ad47854 | 12 | public: |
alexanderlea | 0:af868ad47854 | 13 | /** |
alexanderlea | 0:af868ad47854 | 14 | * @param[ref] _ble |
alexanderlea | 7:5c983cf3c352 | 15 | * |
alexanderlea | 7:5c983cf3c352 | 16 | * @param[in] |
alexanderlea | 0:af868ad47854 | 17 | */ |
alexanderlea | 8:3376f79e7d50 | 18 | |
alexanderlea | 8:3376f79e7d50 | 19 | const static uint16_t BROADCAST_SERVICE_UUID = 0x2A67; |
alexanderlea | 8:3376f79e7d50 | 20 | const static uint16_t BROADCAST_CHARACTERISTIC_UUID = 0x1817; |
alexanderlea | 8:3376f79e7d50 | 21 | |
alexanderlea | 4:ac0ee88ea0ed | 22 | BroadcasterService(BLEDevice &_ble, uint8_t _command) : |
alexanderlea | 0:af868ad47854 | 23 | ble(_ble), |
alexanderlea | 1:ebdf445c4bcc | 24 | command(_command), |
alexanderlea | 8:3376f79e7d50 | 25 | |
alexanderlea | 8:3376f79e7d50 | 26 | broadcasterCharacteristic(BROADCAST_CHARACTERISTIC_UUID, &command, sizeof(command), sizeof(command), |
alexanderlea | 7:5c983cf3c352 | 27 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
alexanderlea | 7:5c983cf3c352 | 28 | // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
alexanderlea | 7:5c983cf3c352 | 29 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
alexanderlea | 0:af868ad47854 | 30 | |
alexanderlea | 8:3376f79e7d50 | 31 | |
alexanderlea | 0:af868ad47854 | 32 | static bool serviceAdded = false; /* We should only ever need to add the service once. */ |
alexanderlea | 0:af868ad47854 | 33 | if (serviceAdded) { |
alexanderlea | 0:af868ad47854 | 34 | return; |
alexanderlea | 0:af868ad47854 | 35 | } |
alexanderlea | 0:af868ad47854 | 36 | |
alexanderlea | 4:ac0ee88ea0ed | 37 | GattCharacteristic *charTable[] = {&broadcasterCharacteristic}; |
alexanderlea | 8:3376f79e7d50 | 38 | GattService broadcasterService(BroadcasterService::BROADCAST_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
alexanderlea | 0:af868ad47854 | 39 | |
alexanderlea | 4:ac0ee88ea0ed | 40 | ble.addService(broadcasterService); |
alexanderlea | 0:af868ad47854 | 41 | serviceAdded = true; |
alexanderlea | 0:af868ad47854 | 42 | } |
alexanderlea | 0:af868ad47854 | 43 | |
alexanderlea | 0:af868ad47854 | 44 | /** |
alexanderlea | 7:5c983cf3c352 | 45 | * @brief |
alexanderlea | 0:af868ad47854 | 46 | * |
alexanderlea | 7:5c983cf3c352 | 47 | * @param |
alexanderlea | 0:af868ad47854 | 48 | */ |
alexanderlea | 1:ebdf445c4bcc | 49 | void sendCommand(uint8_t _newCommand) { |
alexanderlea | 1:ebdf445c4bcc | 50 | command = _newCommand; |
alexanderlea | 4:ac0ee88ea0ed | 51 | ble.updateCharacteristicValue(broadcasterCharacteristic.getValueAttribute().getHandle(), &command, 1); |
alexanderlea | 0:af868ad47854 | 52 | } |
alexanderlea | 0:af868ad47854 | 53 | |
alexanderlea | 0:af868ad47854 | 54 | private: |
alexanderlea | 0:af868ad47854 | 55 | BLEDevice &ble; |
alexanderlea | 1:ebdf445c4bcc | 56 | uint8_t command; |
alexanderlea | 4:ac0ee88ea0ed | 57 | GattCharacteristic broadcasterCharacteristic; |
alexanderlea | 0:af868ad47854 | 58 | }; |
alexanderlea | 0:af868ad47854 | 59 | |
alexanderlea | 7:5c983cf3c352 | 60 | #endif |