Bluetooth Low Energy / Mbed 2 deprecated BLE_TemperatureObserver

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_TemperatureObserver by xiao sun

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "toolchain.h"
00019 #include "ble/BLE.h"
00020 #include "TMP_nrf51/TMP_nrf51.h"
00021 
00022 DigitalOut alivenessLED(LED1, 1);
00023 Ticker     ticker;
00024 
00025 void periodicCallback(void)
00026 {
00027     alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
00028 }
00029 
00030 /*
00031  * This function is called every time we scan an advertisement.
00032  */
00033 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
00034 {
00035     struct AdvertisingData_t {
00036         uint8_t                        length; /* doesn't include itself */
00037         GapAdvertisingData::DataType_t dataType;
00038         uint8_t                        data[0];
00039     } PACKED;
00040 
00041     struct ApplicationData_t {
00042         uint16_t applicationSpecificId;             /* An ID used to identify temperature value
00043                                                        in the manufacture specific AD data field */
00044         TMP_nrf51::TempSensorValue_t tmpSensorValue; /* User defined application data */
00045     } PACKED;
00046 
00047     static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
00048 
00049     /* Search for the manufacturer specific data with matching application-ID */
00050     AdvertisingData_t *pAdvData;
00051     size_t index = 0;
00052     while (index < params->advertisingDataLen) {
00053         pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
00054         if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
00055             ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
00056             if (pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
00057                 /* dump information on the console. */
00058                 printf("From [%02x %02x %02x], ", params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
00059                 printf("Temp is %f\r\n", (TMP_nrf51::TempSensorValue_t)pAppData->tmpSensorValue);
00060                 break;
00061             }
00062         }
00063         index += (pAdvData->length + 1);
00064     }
00065 }
00066 
00067 /**
00068  * This function is called when the ble initialization process has failed
00069  */
00070 void onBleInitError(BLE &ble, ble_error_t error)
00071 {
00072     /* Initialization error handling should go here */
00073 }
00074 
00075 /**
00076  * Callback triggered when the ble initialization process has finished
00077  */
00078 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00079 {
00080     BLE&        ble   = params->ble;
00081     ble_error_t error = params->error;
00082 
00083     if (error != BLE_ERROR_NONE) {
00084         /* In case of error, forward the error handling to onBleInitError */
00085         onBleInitError(ble, error);
00086         return;
00087     }
00088 
00089     /* Ensure that it is the default instance of BLE */
00090     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00091         return;
00092     }
00093 
00094     /* Setup and start scanning */
00095     ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
00096     ble.gap().startScan(advertisementCallback);
00097 }
00098 
00099 int main(void)
00100 {
00101     ticker.attach(periodicCallback, 1);  /* trigger sensor polling every 2 seconds */
00102 
00103     BLE &ble = BLE::Instance();
00104     ble.init(bleInitComplete);
00105 
00106     while (true) {
00107         ble.waitForEvent();
00108     }
00109 }