Workings and tests to create custom GATT services, for use as part of peripheral communication in cars
Dependencies: BLE_API mbed nRF51822
Revision 14:7225b50aaaf7, committed 2015-04-09
- Comitter:
- alexanderlea
- Date:
- Thu Apr 09 14:48:35 2015 +0000
- Parent:
- 13:a578c382cb7d
- Commit message:
- updates
Changed in this revision
BroadcasterService.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/BroadcasterService.h Fri Mar 27 12:36:36 2015 +0000 +++ b/BroadcasterService.h Thu Apr 09 14:48:35 2015 +0000 @@ -2,6 +2,7 @@ #define __BLE_BROADCASTER_SERVICE_H__ #include "BLEDevice.h" +using namespace std; /** * @class BroadcasterService @@ -15,18 +16,16 @@ * @param[ref] _ble * * @param[in] - */ - + */ const static uint16_t BROADCAST_SERVICE_UUID = 0x2A67; const static uint16_t BROADCAST_CHARACTERISTIC_UUID = 0x1817; - BroadcasterService(BLEDevice &_ble) : //, uint8_t _command[8] + BroadcasterService(BLEDevice &_ble) : ble(_ble), //command(), //instead of command(_command); broadcasterCharacteristic(BROADCAST_CHARACTERISTIC_UUID, command, sizeof(command), sizeof(command), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | - // GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { static bool serviceAdded = false; /* We should only ever need to add the service once. */ @@ -35,7 +34,8 @@ } GattCharacteristic *charTable[] = {&broadcasterCharacteristic}; - GattService broadcasterService(BroadcasterService::BROADCAST_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + GattService broadcasterService(BroadcasterService::BROADCAST_SERVICE_UUID, charTable, + sizeof(charTable) / sizeof(GattCharacteristic *)); ble.addService(broadcasterService); serviceAdded = true; @@ -48,8 +48,8 @@ */ void sendCommand(uint8_t _newCommand[8]) { // command = _newCommand; - - memcpy(command, _newCommand, sizeof(_newCommand)); + //std::copy_n(_newCommand, sizeof(_newCommand), command); + memcpy(&command, &_newCommand, sizeof(_newCommand)); ble.updateCharacteristicValue(broadcasterCharacteristic.getValueAttribute().getHandle(), command, sizeof(command), false); }
--- a/main.cpp Fri Mar 27 12:36:36 2015 +0000 +++ b/main.cpp Thu Apr 09 14:48:35 2015 +0000 @@ -15,8 +15,9 @@ const static char DEVICE_NAME[] = "BLE_Broadcaster"; static volatile bool indicatorOn = false; static volatile bool indicatorSent = false; -uint8_t cmdIndicatorOn[8] = { 0x69,0x6e,0x64,0x69,0x7f,0x7f,0x7f,0x7f }; // = I N D I 127 127 127 127 +uint8_t cmdIndicatorOn[8] = { 0x69,0x6e,0x64,0x69,0xff,0xff,0xff,0xff }; // = I N D I 255 255 255 255 uint8_t cmdIndicatorOff[8] = { 0x69,0x6e,0x64,0x69,0x00,0x00,0x00,0x00 }; // = I N D I 0 0 0 0 +static const uint16_t uuid16_list[] = {BroadcasterService::BROADCAST_SERVICE_UUID}; /** Callback function for ticker */ void blink(void) @@ -48,7 +49,7 @@ /** Callback function for BLE connection */ void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms) { - pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr); + pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Addr:%u.\r\n", handle, peerAddrType, peerAddr); ble.stopAdvertising(); } @@ -62,6 +63,8 @@ indicatorIn.fall(&indicatorSwitchOn); indicatorIn.rise(&indicatorSwitchOff); + //TODO: Initial light input - if switch is ON at program start + /*Setup Blinky*/ led1 = 1; Ticker t; @@ -82,11 +85,11 @@ * ADV_CONNECTABLE_UNDIRECTED = Any central device can connect */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); -// ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600 + ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ ble.startAdvertising(); pc.printf("Advertising node %s\n\r", DEVICE_NAME); @@ -97,13 +100,13 @@ if(indicatorOn) { //if button is pressed if(!indicatorSent) { //should only fire the first time! - pc.printf("Command on = %u\r\n", cmdIndicatorOn); + pc.printf("Command on\r\n"); broadcasterService.sendCommand(cmdIndicatorOn); indicatorSent = true; //set true to stop multiple firing } } else { if(!indicatorSent) { - pc.printf("Command off = %u\r\n", cmdIndicatorOff); + pc.printf("Command off\r\n"); broadcasterService.sendCommand(cmdIndicatorOff); indicatorSent = true; //set true to stop multiple firing }