Workings and tests to create custom GATT services, for use as part of peripheral communication in cars
Dependencies: BLE_API mbed nRF51822
BroadcasterService.h@4:ac0ee88ea0ed, 2015-02-17 (annotated)
- Committer:
- alexanderlea
- Date:
- Tue Feb 17 15:57:48 2015 +0000
- Revision:
- 4:ac0ee88ea0ed
- Parent:
- TestGattService.h@3:f3d20b36b7ea
- Child:
- 7:5c983cf3c352
Refactored to "BroadcasterService", and now sending 1 and 0 as on or off
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexanderlea | 0:af868ad47854 | 1 | #ifndef __BLE_TEST_SERVICE_H__ |
alexanderlea | 0:af868ad47854 | 2 | #define __BLE_TEST_SERVICE_H__ |
alexanderlea | 0:af868ad47854 | 3 | |
alexanderlea | 0:af868ad47854 | 4 | #include "BLEDevice.h" |
alexanderlea | 0:af868ad47854 | 5 | |
alexanderlea | 0:af868ad47854 | 6 | /** |
alexanderlea | 0:af868ad47854 | 7 | * @class BatteryService |
alexanderlea | 0:af868ad47854 | 8 | * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br> |
alexanderlea | 0:af868ad47854 | 9 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br> |
alexanderlea | 0:af868ad47854 | 10 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
alexanderlea | 0:af868ad47854 | 11 | */ |
alexanderlea | 4:ac0ee88ea0ed | 12 | class BroadcasterService { |
alexanderlea | 0:af868ad47854 | 13 | public: |
alexanderlea | 0:af868ad47854 | 14 | /** |
alexanderlea | 0:af868ad47854 | 15 | * @param[ref] _ble |
alexanderlea | 1:ebdf445c4bcc | 16 | * |
alexanderlea | 1:ebdf445c4bcc | 17 | * @param[in] |
alexanderlea | 0:af868ad47854 | 18 | */ |
alexanderlea | 4:ac0ee88ea0ed | 19 | BroadcasterService(BLEDevice &_ble, uint8_t _command) : |
alexanderlea | 0:af868ad47854 | 20 | ble(_ble), |
alexanderlea | 1:ebdf445c4bcc | 21 | command(_command), |
alexanderlea | 4:ac0ee88ea0ed | 22 | broadcasterCharacteristic(0x2A67, &command, sizeof(command), sizeof(command), |
alexanderlea | 1:ebdf445c4bcc | 23 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
alexanderlea | 3:f3d20b36b7ea | 24 | // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
alexanderlea | 1:ebdf445c4bcc | 25 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
alexanderlea | 0:af868ad47854 | 26 | |
alexanderlea | 0:af868ad47854 | 27 | static bool serviceAdded = false; /* We should only ever need to add the service once. */ |
alexanderlea | 0:af868ad47854 | 28 | if (serviceAdded) { |
alexanderlea | 0:af868ad47854 | 29 | return; |
alexanderlea | 0:af868ad47854 | 30 | } |
alexanderlea | 0:af868ad47854 | 31 | |
alexanderlea | 4:ac0ee88ea0ed | 32 | GattCharacteristic *charTable[] = {&broadcasterCharacteristic}; |
alexanderlea | 4:ac0ee88ea0ed | 33 | GattService broadcasterService(0x1817, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
alexanderlea | 0:af868ad47854 | 34 | |
alexanderlea | 4:ac0ee88ea0ed | 35 | ble.addService(broadcasterService); |
alexanderlea | 0:af868ad47854 | 36 | serviceAdded = true; |
alexanderlea | 0:af868ad47854 | 37 | } |
alexanderlea | 0:af868ad47854 | 38 | |
alexanderlea | 0:af868ad47854 | 39 | /** |
alexanderlea | 1:ebdf445c4bcc | 40 | * @brief |
alexanderlea | 0:af868ad47854 | 41 | * |
alexanderlea | 1:ebdf445c4bcc | 42 | * @param |
alexanderlea | 0:af868ad47854 | 43 | */ |
alexanderlea | 1:ebdf445c4bcc | 44 | void sendCommand(uint8_t _newCommand) { |
alexanderlea | 1:ebdf445c4bcc | 45 | command = _newCommand; |
alexanderlea | 4:ac0ee88ea0ed | 46 | ble.updateCharacteristicValue(broadcasterCharacteristic.getValueAttribute().getHandle(), &command, 1); |
alexanderlea | 0:af868ad47854 | 47 | } |
alexanderlea | 0:af868ad47854 | 48 | |
alexanderlea | 0:af868ad47854 | 49 | private: |
alexanderlea | 0:af868ad47854 | 50 | BLEDevice &ble; |
alexanderlea | 1:ebdf445c4bcc | 51 | uint8_t command; |
alexanderlea | 4:ac0ee88ea0ed | 52 | GattCharacteristic broadcasterCharacteristic; |
alexanderlea | 0:af868ad47854 | 53 | }; |
alexanderlea | 0:af868ad47854 | 54 | |
alexanderlea | 0:af868ad47854 | 55 | #endif /* #ifndef __BLE_TEST_SERVICE_H__*/ |