an observer for temperature demo

Dependencies:   BLE_API mbed nRF51822 TMP_nrf51

Fork of BLE_Observer by Bluetooth Low Energy

Committer:
sunsmile2015
Date:
Fri Jul 31 09:13:50 2015 +0000
Revision:
8:649bd171929e
Parent:
7:91324daa3bfa
add advertiser address information in the output message

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:332983584a9c 1 /* mbed Microcontroller Library
rgrover1 0:332983584a9c 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:332983584a9c 3 *
rgrover1 0:332983584a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:332983584a9c 5 * you may not use this file except in compliance with the License.
rgrover1 0:332983584a9c 6 * You may obtain a copy of the License at
rgrover1 0:332983584a9c 7 *
rgrover1 0:332983584a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:332983584a9c 9 *
rgrover1 0:332983584a9c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:332983584a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:332983584a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:332983584a9c 13 * See the License for the specific language governing permissions and
rgrover1 0:332983584a9c 14 * limitations under the License.
rgrover1 0:332983584a9c 15 */
rgrover1 0:332983584a9c 16
rgrover1 0:332983584a9c 17 #include "mbed.h"
rgrover1 4:dd8231564124 18 #include "BLE.h"
sunsmile2015 7:91324daa3bfa 19 #include "TMP_nrf51/TMP_nrf51.h"
sunsmile2015 7:91324daa3bfa 20
sunsmile2015 8:649bd171929e 21 #define APP_SPECIFIC_ID_TEST 0xFEFE
sunsmile2015 7:91324daa3bfa 22
sunsmile2015 7:91324daa3bfa 23 #pragma pack(1)
sunsmile2015 7:91324daa3bfa 24 /* Advertising data */
sunsmile2015 8:649bd171929e 25 struct AdvertisingData_t {
sunsmile2015 8:649bd171929e 26 uint8_t length; /* doesn't include itself */
sunsmile2015 7:91324daa3bfa 27 GapAdvertisingData::DataType dataType;
sunsmile2015 7:91324daa3bfa 28 uint8_t data[1];
sunsmile2015 8:649bd171929e 29 };
sunsmile2015 7:91324daa3bfa 30
sunsmile2015 8:649bd171929e 31 struct ApplicationData_t {
sunsmile2015 8:649bd171929e 32 uint16_t applicationSpecificId; /* An ID used to identify temperature value
sunsmile2015 8:649bd171929e 33 in the manufacture specific AD data field */
sunsmile2015 8:649bd171929e 34 TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
sunsmile2015 8:649bd171929e 35 };
sunsmile2015 7:91324daa3bfa 36 #pragma pack()
rgrover1 0:332983584a9c 37
rgrover1 5:103717ce54e5 38 BLE ble;
rgrover1 0:332983584a9c 39 DigitalOut led1(LED1);
rgrover1 0:332983584a9c 40
rgrover1 0:332983584a9c 41 void periodicCallback(void)
rgrover1 0:332983584a9c 42 {
rgrover1 0:332983584a9c 43 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 0:332983584a9c 44 }
rgrover1 0:332983584a9c 45
sunsmile2015 6:850f44146c9f 46 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
sunsmile2015 6:850f44146c9f 47 {
sunsmile2015 8:649bd171929e 48 AdvertisingData_t *pAdvData = NULL;
sunsmile2015 7:91324daa3bfa 49 uint8_t len = 0;
sunsmile2015 6:850f44146c9f 50
sunsmile2015 7:91324daa3bfa 51 /* Search for the manufacturer data */
sunsmile2015 7:91324daa3bfa 52 while(len < params->advertisingDataLen) {
sunsmile2015 8:649bd171929e 53 pAdvData = (AdvertisingData_t *)&params->advertisingData[len];
sunsmile2015 7:91324daa3bfa 54 if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
sunsmile2015 8:649bd171929e 55 ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
sunsmile2015 8:649bd171929e 56 if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
sunsmile2015 8:649bd171929e 57 printf("From [%02x %02x %02x], ", params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
sunsmile2015 8:649bd171929e 58 printf("Temp is %f\r\n", (float)pAppData->tmpSensorValue);
sunsmile2015 7:91324daa3bfa 59 break;
sunsmile2015 7:91324daa3bfa 60 }
sunsmile2015 6:850f44146c9f 61 }
sunsmile2015 7:91324daa3bfa 62 len += (pAdvData->length + 1);
rgrover1 0:332983584a9c 63 }
rgrover1 0:332983584a9c 64 }
rgrover1 0:332983584a9c 65
rgrover1 0:332983584a9c 66 int main(void)
rgrover1 0:332983584a9c 67 {
rgrover1 0:332983584a9c 68 led1 = 1;
rgrover1 0:332983584a9c 69 Ticker ticker;
rgrover1 0:332983584a9c 70 ticker.attach(periodicCallback, 1);
rgrover1 0:332983584a9c 71
rgrover1 0:332983584a9c 72 ble.init();
sunsmile2015 6:850f44146c9f 73 ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
rgrover1 5:103717ce54e5 74 ble.gap().startScan(advertisementCallback);
rgrover1 0:332983584a9c 75
rgrover1 0:332983584a9c 76 while (true) {
rgrover1 0:332983584a9c 77 ble.waitForEvent();
rgrover1 0:332983584a9c 78 }
rgrover1 0:332983584a9c 79 }