Seunghwan Kim / Mbed 2 deprecated BLE_LEDBlinker_mo

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LEDBlinker by Bluetooth Low Energy

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 "ble/BLE.h"
00019 #include "ble/DiscoveredCharacteristic.h"
00020 #include "ble/DiscoveredService.h"
00021 
00022 BLE ble;
00023 
00024 DigitalOut alivenessLED(LED1, 1);
00025 bool triggerLedCharacteristic = false;
00026 DiscoveredCharacteristic ledCharacteristic;
00027 
00028 void periodicCallback(void) {
00029     alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
00030 }
00031 
00032 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
00033     if (params->peerAddr[0] != 0x29) { /* !ALERT! Alter this filter to suit your device. */
00034         return;
00035     }
00036     printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
00037            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
00038            params->rssi, params->isScanResponse, params->type);
00039 
00040     ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
00041 }
00042 
00043 void serviceDiscoveryCallback(const DiscoveredService *service) {
00044     if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
00045         printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
00046     } else {
00047         printf("S UUID-");
00048         const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
00049         for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
00050             printf("%02x", longUUIDBytes[i]);
00051         }
00052         printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
00053     }
00054 }
00055 
00056 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
00057     printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
00058     if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
00059         ledCharacteristic        = *characteristicP;
00060         triggerLedCharacteristic = true;
00061     }
00062 }
00063 
00064 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
00065     printf("terminated SD for handle %u\r\n", connectionHandle);
00066 }
00067 
00068 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
00069     if (params->role == Gap::CENTRAL) {
00070         ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
00071         ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
00072     }
00073 }
00074 
00075 void triggerToggledWrite(const GattReadCallbackParams *response) {
00076     if (response->handle == ledCharacteristic.getValueHandle()) {
00077 #if DUMP_READ_DATA
00078         printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
00079         for (unsigned index = 0; index < response->len; index++) {
00080             printf("%c[%02x]", response->data[index], response->data[index]);
00081         }
00082         printf("\r\n");
00083 #endif
00084 
00085         uint8_t toggledValue = response->data[0] ^ 0x1;
00086         ledCharacteristic.write(1, &toggledValue);
00087     }
00088 }
00089 
00090 void triggerRead(const GattWriteCallbackParams *response) {
00091     if (response->handle == ledCharacteristic.getValueHandle()) {
00092         ledCharacteristic.read();
00093     }
00094 }
00095 
00096 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
00097     printf("disconnected\r\n");
00098 }
00099 
00100 int main(void) {
00101     Ticker ticker;
00102     ticker.attach(periodicCallback, 1);
00103 
00104     ble.init();
00105     ble.gap().onConnection(connectionCallback);
00106     ble.gap().onDisconnection(disconnectionCallback);
00107 
00108     ble.gattClient().onDataRead(triggerToggledWrite);
00109     ble.gattClient().onDataWrite(triggerRead);
00110 
00111     ble.gap().setScanParams(500, 400);
00112     ble.gap().startScan(advertisementCallback);
00113 
00114     while (true) {
00115         if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
00116             triggerLedCharacteristic = false;
00117             ledCharacteristic.read(); /* We could have issued this read just as easily from
00118                                        * characteristicDiscoveryCallback(); but
00119                                        * invoking it here demonstrates the use
00120                                        * of isServiceDiscoveryActive() and also
00121                                        * the fact that it is permitted to
00122                                        * operate on application-local copies of
00123                                        * DiscoveredCharacteristic. */
00124         }
00125         ble.waitForEvent();
00126     }
00127 }