Workings and tests to create custom GATT services, for use as part of peripheral communication in cars
Dependencies: BLE_API mbed nRF51822
BroadcasterService.h@14:7225b50aaaf7, 2015-04-09 (annotated)
- Committer:
- alexanderlea
- Date:
- Thu Apr 09 14:48:35 2015 +0000
- Revision:
- 14:7225b50aaaf7
- Parent:
- 12:f5b12e8b6043
updates
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 | 14:7225b50aaaf7 | 5 | using namespace std; |
alexanderlea | 0:af868ad47854 | 6 | |
alexanderlea | 0:af868ad47854 | 7 | /** |
alexanderlea | 7:5c983cf3c352 | 8 | * @class BroadcasterService |
alexanderlea | 12:f5b12e8b6043 | 9 | * @brief Based heavily on the BLE Battery Service, the aim is to send key, pair values <br>. |
alexanderlea | 12:f5b12e8b6043 | 10 | * UPDATE: Should now be sending 8-byte string - 4 bytes for Type, 4 for command |
alexanderlea | 0:af868ad47854 | 11 | */ |
alexanderlea | 7:5c983cf3c352 | 12 | class BroadcasterService |
alexanderlea | 7:5c983cf3c352 | 13 | { |
alexanderlea | 0:af868ad47854 | 14 | public: |
alexanderlea | 0:af868ad47854 | 15 | /** |
alexanderlea | 0:af868ad47854 | 16 | * @param[ref] _ble |
alexanderlea | 7:5c983cf3c352 | 17 | * |
alexanderlea | 7:5c983cf3c352 | 18 | * @param[in] |
alexanderlea | 14:7225b50aaaf7 | 19 | */ |
alexanderlea | 8:3376f79e7d50 | 20 | const static uint16_t BROADCAST_SERVICE_UUID = 0x2A67; |
alexanderlea | 8:3376f79e7d50 | 21 | const static uint16_t BROADCAST_CHARACTERISTIC_UUID = 0x1817; |
alexanderlea | 8:3376f79e7d50 | 22 | |
alexanderlea | 14:7225b50aaaf7 | 23 | BroadcasterService(BLEDevice &_ble) : |
alexanderlea | 0:af868ad47854 | 24 | ble(_ble), |
alexanderlea | 12:f5b12e8b6043 | 25 | //command(), //instead of command(_command); |
alexanderlea | 8:3376f79e7d50 | 26 | |
alexanderlea | 9:0ed64b14d46b | 27 | broadcasterCharacteristic(BROADCAST_CHARACTERISTIC_UUID, command, sizeof(command), sizeof(command), |
alexanderlea | 7:5c983cf3c352 | 28 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
alexanderlea | 7:5c983cf3c352 | 29 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
alexanderlea | 10:2c5c202c69a5 | 30 | |
alexanderlea | 0:af868ad47854 | 31 | static bool serviceAdded = false; /* We should only ever need to add the service once. */ |
alexanderlea | 0:af868ad47854 | 32 | if (serviceAdded) { |
alexanderlea | 0:af868ad47854 | 33 | return; |
alexanderlea | 0:af868ad47854 | 34 | } |
alexanderlea | 0:af868ad47854 | 35 | |
alexanderlea | 4:ac0ee88ea0ed | 36 | GattCharacteristic *charTable[] = {&broadcasterCharacteristic}; |
alexanderlea | 14:7225b50aaaf7 | 37 | GattService broadcasterService(BroadcasterService::BROADCAST_SERVICE_UUID, charTable, |
alexanderlea | 14:7225b50aaaf7 | 38 | 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 | 9:0ed64b14d46b | 49 | void sendCommand(uint8_t _newCommand[8]) { |
alexanderlea | 9:0ed64b14d46b | 50 | // command = _newCommand; |
alexanderlea | 14:7225b50aaaf7 | 51 | //std::copy_n(_newCommand, sizeof(_newCommand), command); |
alexanderlea | 14:7225b50aaaf7 | 52 | memcpy(&command, &_newCommand, sizeof(_newCommand)); |
alexanderlea | 10:2c5c202c69a5 | 53 | ble.updateCharacteristicValue(broadcasterCharacteristic.getValueAttribute().getHandle(), command, sizeof(command), false); |
alexanderlea | 0:af868ad47854 | 54 | } |
alexanderlea | 0:af868ad47854 | 55 | |
alexanderlea | 0:af868ad47854 | 56 | private: |
alexanderlea | 9:0ed64b14d46b | 57 | BLEDevice &ble; |
alexanderlea | 9:0ed64b14d46b | 58 | uint8_t command[8]; |
alexanderlea | 9:0ed64b14d46b | 59 | GattCharacteristic broadcasterCharacteristic; |
alexanderlea | 0:af868ad47854 | 60 | }; |
alexanderlea | 0:af868ad47854 | 61 | |
alexanderlea | 7:5c983cf3c352 | 62 | #endif |