Example of BLE scan/connect/service discovery

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 DigitalOut alivenessLED(LED1, 1);
00023 
00024 bool                     triggerLedCharacteristic = false;
00025 DiscoveredCharacteristic ledCharacteristic;
00026 
00027 Ticker ticker;
00028 
00029 void periodicCallback(void) {
00030     alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
00031 }
00032 
00033 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
00034     if (params->peerAddr[0] != 0xF9) { /* !ALERT! Alter this filter to suit your device. */
00035         return;
00036     }
00037     printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
00038            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
00039            params->rssi, params->isScanResponse, params->type);
00040 
00041     BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
00042 }
00043 
00044 void serviceDiscoveryCallback(const DiscoveredService *service) {
00045     printf("serviceDiscoveryCallback\n");
00046     if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
00047         printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
00048     } else {
00049         printf("S UUID-");
00050         const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
00051         for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
00052             printf("%02x", longUUIDBytes[i]);
00053         }
00054         printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
00055     }
00056 }
00057 
00058 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
00059     if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
00060         printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
00061         if (characteristicP->getUUID().getShortUUID() == 0x2a19) { /* !ALERT! Alter this filter to suit your device. */   
00062             ledCharacteristic        = *characteristicP;
00063             triggerLedCharacteristic = true;
00064         }
00065     }
00066     else {
00067         printf("  C UUID-");
00068         const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
00069         for (unsigned i = (UUID::LENGTH_OF_LONG_UUID) - 1; i < UUID::LENGTH_OF_LONG_UUID; i--) {
00070             printf("%02x ", longUUIDBytes[i]);
00071         }
00072         printf(" valueAttr[%u] props[%x]\r\n", characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
00073     }
00074 }
00075 
00076 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
00077     printf("terminated SD for handle %u\r\n", connectionHandle);
00078 }
00079 
00080 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
00081     printf("connectionCallback\n");
00082     if (params->role == Gap::CENTRAL) {
00083         BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
00084         BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback);
00085     }
00086 }
00087 
00088 void triggerToggledWrite(const GattReadCallbackParams *response) {
00089     if (response->handle == ledCharacteristic.getValueHandle()) {
00090 #if DUMP_READ_DATA
00091         printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
00092         for (unsigned index = 0; index < response->len; index++) {
00093             printf("%c[%02x]", response->data[index], response->data[index]);
00094         }
00095         printf("\r\n");
00096 #endif
00097 
00098         uint8_t toggledValue = response->data[0] ^ 0x1;
00099         ledCharacteristic.write(1, &toggledValue);
00100     }
00101 }
00102 
00103 void triggerRead(const GattWriteCallbackParams *response) {
00104     if (response->handle == ledCharacteristic.getValueHandle()) {
00105         ledCharacteristic.read();
00106     }
00107 }
00108 
00109 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
00110     printf("disconnected\r\n");
00111 }
00112 
00113 /**
00114  * This function is called when the ble initialization process has failed
00115  */
00116 void onBleInitError(BLE &ble, ble_error_t error)
00117 {
00118     /* Initialization error handling should go here */
00119 }
00120 
00121 /**
00122  * Callback triggered when the ble initialization process has finished
00123  */
00124 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00125 {
00126     BLE&        ble   = params->ble;
00127     ble_error_t error = params->error;
00128 
00129     if (error != BLE_ERROR_NONE) {
00130         /* In case of error, forward the error handling to onBleInitError */
00131         onBleInitError(ble, error);
00132         return;
00133     }
00134 
00135     /* Ensure that it is the default instance of BLE */
00136     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00137         return;
00138     }
00139  
00140     ble.gap().onConnection(connectionCallback);
00141     ble.gap().onDisconnection(disconnectionCallback);
00142 
00143     ble.gattClient().onDataRead(triggerToggledWrite);
00144     ble.gattClient().onDataWrite(triggerRead);
00145 
00146     ble.gap().setScanParams(500, 400);
00147     ble.gap().startScan(advertisementCallback);
00148 }
00149 
00150 int main(void) {
00151     printf("APPLICATION START\r\n");
00152     ticker.attach(periodicCallback, 1);
00153 
00154     BLE &ble = BLE::Instance();
00155     ble.init(bleInitComplete);
00156 
00157     /* SpinWait for initialization to complete. This is necessary because the
00158      * BLE object is used in the main loop below. */
00159     while (ble.hasInitialized()  == false) { /* spin loop */ }
00160     printf("init DONE\r\n");
00161     while (true) {
00162         if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
00163             triggerLedCharacteristic = false;
00164             ledCharacteristic.read(); /* We could have issued this read just as easily from
00165                                        * characteristicDiscoveryCallback(); but
00166                                        * invoking it here demonstrates the use
00167                                        * of isServiceDiscoveryActive() and also
00168                                        * the fact that it is permitted to
00169                                        * operate on application-local copies of
00170                                        * DiscoveredCharacteristic. */
00171         }
00172         ble.waitForEvent();
00173     }
00174 }