First commit

Dependencies:   mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FindmeService.h Source File

FindmeService.h

00001 
00002 
00003 #ifndef __BLE_FIND_ME_SERVICE_H__
00004 #define __BLE_FIND_ME_SERVICE_H__
00005 
00006 #include "BLEDevice.h"
00007 
00008 
00009 class FindMeService {
00010 public:
00011     uint8_t                 AlertValue;
00012 
00013     /**
00014      * @brief Constructor with 8bit Alert value.
00015      * @param[ref] _ble
00016      * @param[in] AlertValue (8-bit)
00017      */
00018     FindMeService(BLEDevice &_ble) :
00019         ble(_ble),
00020         
00021         Alert_Level(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, (uint8_t *) &AlertValue,
00022                 sizeof(AlertValue), sizeof(AlertValue), 
00023                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE)
00024     {
00025         AlertValue = 2;
00026         setupService();
00027     }
00028 
00029     virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
00030         uint8_t buf[2];
00031         uint16_t bytesRead;
00032         uint16_t *AlertTemp = 0;
00033         if (params->charHandle == Alert_Level.getValueAttribute().getHandle()) {
00034                 
00035                 ble.readCharacteristicValue(Alert_Level.getValueAttribute().getHandle(), buf, &bytesRead);
00036                 memset(AlertTemp, 0, sizeof(buf));
00037                 memcpy(AlertTemp, buf, sizeof(buf));
00038                 AlertValue = (uint8_t)((*AlertTemp)&0xff);
00039         }
00040     }
00041 
00042 private:
00043     void setupService(void) {
00044         static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00045         if (serviceAdded) {
00046             return;
00047         }
00048 
00049         GattCharacteristic *charTable[] = {&Alert_Level};
00050         GattService         fmService(GattService::UUID_IMMEDIATE_ALERT_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00051         ble.addService(fmService);
00052         serviceAdded = true;
00053         ble.onDataWritten(this, &FindMeService::onDataWritten);
00054     }
00055 
00056 private:
00057     BLEDevice               &ble;
00058     GattCharacteristic      Alert_Level;
00059 
00060 };
00061 
00062 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/