A client to go with BLE_Button. Demonstrates the enabling of notifications.
Dependencies: BLE_API mbed nRF51822
main.cpp@0:2dec89c76f68, 2015-07-06 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Jul 06 09:30:41 2015 +0000
- Revision:
- 0:2dec89c76f68
A client to go with BLE_Button. Demonstrates the enabling of notifications.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:2dec89c76f68 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:2dec89c76f68 | 2 | * Copyright (c) 2006-2015 ARM Limited |
rgrover1 | 0:2dec89c76f68 | 3 | * |
rgrover1 | 0:2dec89c76f68 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:2dec89c76f68 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:2dec89c76f68 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:2dec89c76f68 | 7 | * |
rgrover1 | 0:2dec89c76f68 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:2dec89c76f68 | 9 | * |
rgrover1 | 0:2dec89c76f68 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:2dec89c76f68 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:2dec89c76f68 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:2dec89c76f68 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:2dec89c76f68 | 14 | * limitations under the License. |
rgrover1 | 0:2dec89c76f68 | 15 | */ |
rgrover1 | 0:2dec89c76f68 | 16 | |
rgrover1 | 0:2dec89c76f68 | 17 | #include "mbed.h" |
rgrover1 | 0:2dec89c76f68 | 18 | #include "ble/BLE.h" |
rgrover1 | 0:2dec89c76f68 | 19 | #include "ble/DiscoveredCharacteristic.h" |
rgrover1 | 0:2dec89c76f68 | 20 | #include "ble/DiscoveredService.h" |
rgrover1 | 0:2dec89c76f68 | 21 | |
rgrover1 | 0:2dec89c76f68 | 22 | #include "ble_gatt.h" |
rgrover1 | 0:2dec89c76f68 | 23 | |
rgrover1 | 0:2dec89c76f68 | 24 | BLE ble; |
rgrover1 | 0:2dec89c76f68 | 25 | |
rgrover1 | 0:2dec89c76f68 | 26 | Gap::Handle_t connectionHandle = 0xFFFF; |
rgrover1 | 0:2dec89c76f68 | 27 | |
rgrover1 | 0:2dec89c76f68 | 28 | DigitalOut alivenessLED(LED1, 1); |
rgrover1 | 0:2dec89c76f68 | 29 | bool foundButtonCharacteristic = false; |
rgrover1 | 0:2dec89c76f68 | 30 | DiscoveredCharacteristic buttonCharacteristic; |
rgrover1 | 0:2dec89c76f68 | 31 | |
rgrover1 | 0:2dec89c76f68 | 32 | void periodicCallback(void) { |
rgrover1 | 0:2dec89c76f68 | 33 | alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ |
rgrover1 | 0:2dec89c76f68 | 34 | } |
rgrover1 | 0:2dec89c76f68 | 35 | |
rgrover1 | 0:2dec89c76f68 | 36 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
rgrover1 | 0:2dec89c76f68 | 37 | if (params->peerAddr[0] != 0x29) { /* !ALERT! Update this filter to suit your device. */ |
rgrover1 | 0:2dec89c76f68 | 38 | return; |
rgrover1 | 0:2dec89c76f68 | 39 | } |
rgrover1 | 0:2dec89c76f68 | 40 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
rgrover1 | 0:2dec89c76f68 | 41 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
rgrover1 | 0:2dec89c76f68 | 42 | params->rssi, params->isScanResponse, params->type); |
rgrover1 | 0:2dec89c76f68 | 43 | |
rgrover1 | 0:2dec89c76f68 | 44 | ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
rgrover1 | 0:2dec89c76f68 | 45 | } |
rgrover1 | 0:2dec89c76f68 | 46 | |
rgrover1 | 0:2dec89c76f68 | 47 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
rgrover1 | 0:2dec89c76f68 | 48 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
rgrover1 | 0:2dec89c76f68 | 49 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:2dec89c76f68 | 50 | } else { |
rgrover1 | 0:2dec89c76f68 | 51 | printf("S UUID-"); |
rgrover1 | 0:2dec89c76f68 | 52 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
rgrover1 | 0:2dec89c76f68 | 53 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
rgrover1 | 0:2dec89c76f68 | 54 | printf("%02x", longUUIDBytes[i]); |
rgrover1 | 0:2dec89c76f68 | 55 | } |
rgrover1 | 0:2dec89c76f68 | 56 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:2dec89c76f68 | 57 | } |
rgrover1 | 0:2dec89c76f68 | 58 | } |
rgrover1 | 0:2dec89c76f68 | 59 | |
rgrover1 | 0:2dec89c76f68 | 60 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
rgrover1 | 0:2dec89c76f68 | 61 | printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
rgrover1 | 0:2dec89c76f68 | 62 | if (characteristicP->getShortUUID() == 0xa001) { /* !ALERT! Update this filter to suit your device. */ |
rgrover1 | 0:2dec89c76f68 | 63 | buttonCharacteristic = *characteristicP; |
rgrover1 | 0:2dec89c76f68 | 64 | foundButtonCharacteristic = true; |
rgrover1 | 0:2dec89c76f68 | 65 | } |
rgrover1 | 0:2dec89c76f68 | 66 | } |
rgrover1 | 0:2dec89c76f68 | 67 | |
rgrover1 | 0:2dec89c76f68 | 68 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
rgrover1 | 0:2dec89c76f68 | 69 | printf("terminated SD for handle %u\r\n", connectionHandle); |
rgrover1 | 0:2dec89c76f68 | 70 | } |
rgrover1 | 0:2dec89c76f68 | 71 | |
rgrover1 | 0:2dec89c76f68 | 72 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
rgrover1 | 0:2dec89c76f68 | 73 | if (params->role == Gap::CENTRAL) { |
rgrover1 | 0:2dec89c76f68 | 74 | connectionHandle = params->handle; |
rgrover1 | 0:2dec89c76f68 | 75 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
rgrover1 | 0:2dec89c76f68 | 76 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/); |
rgrover1 | 0:2dec89c76f68 | 77 | } |
rgrover1 | 0:2dec89c76f68 | 78 | } |
rgrover1 | 0:2dec89c76f68 | 79 | |
rgrover1 | 0:2dec89c76f68 | 80 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { |
rgrover1 | 0:2dec89c76f68 | 81 | printf("disconnected\r\n"); |
rgrover1 | 0:2dec89c76f68 | 82 | } |
rgrover1 | 0:2dec89c76f68 | 83 | |
rgrover1 | 0:2dec89c76f68 | 84 | void hvxCallback(const GattHVXCallbackParams *params) { |
rgrover1 | 0:2dec89c76f68 | 85 | printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); |
rgrover1 | 0:2dec89c76f68 | 86 | for (unsigned index = 0; index < params->len; index++) { |
rgrover1 | 0:2dec89c76f68 | 87 | printf(" %02x", params->data[index]); |
rgrover1 | 0:2dec89c76f68 | 88 | } |
rgrover1 | 0:2dec89c76f68 | 89 | printf("\r\n"); |
rgrover1 | 0:2dec89c76f68 | 90 | } |
rgrover1 | 0:2dec89c76f68 | 91 | |
rgrover1 | 0:2dec89c76f68 | 92 | int main(void) { |
rgrover1 | 0:2dec89c76f68 | 93 | Ticker ticker; |
rgrover1 | 0:2dec89c76f68 | 94 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:2dec89c76f68 | 95 | |
rgrover1 | 0:2dec89c76f68 | 96 | ble.init(); |
rgrover1 | 0:2dec89c76f68 | 97 | ble.gap().onConnection(connectionCallback); |
rgrover1 | 0:2dec89c76f68 | 98 | ble.gap().onDisconnection(disconnectionCallback); |
rgrover1 | 0:2dec89c76f68 | 99 | |
rgrover1 | 0:2dec89c76f68 | 100 | ble.gap().setScanParams(500, 400); |
rgrover1 | 0:2dec89c76f68 | 101 | ble.gap().startScan(advertisementCallback); |
rgrover1 | 0:2dec89c76f68 | 102 | |
rgrover1 | 0:2dec89c76f68 | 103 | ble.gattClient().onHVX(hvxCallback); |
rgrover1 | 0:2dec89c76f68 | 104 | |
rgrover1 | 0:2dec89c76f68 | 105 | while (true) { |
rgrover1 | 0:2dec89c76f68 | 106 | if (foundButtonCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) { |
rgrover1 | 0:2dec89c76f68 | 107 | foundButtonCharacteristic = false; /* need to do the following only once */ |
rgrover1 | 0:2dec89c76f68 | 108 | |
rgrover1 | 0:2dec89c76f68 | 109 | /* Note: Yuckiness alert! The following needs to be encapsulated in a neat API. |
rgrover1 | 0:2dec89c76f68 | 110 | * It isn't clear whether we should provide a DiscoveredCharacteristic::enableNoticiation() or |
rgrover1 | 0:2dec89c76f68 | 111 | * DiscoveredCharacteristic::discoverDescriptors() followed by DiscoveredDescriptor::write(...). */ |
rgrover1 | 0:2dec89c76f68 | 112 | uint16_t value = BLE_HVX_NOTIFICATION; |
rgrover1 | 0:2dec89c76f68 | 113 | ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ, |
rgrover1 | 0:2dec89c76f68 | 114 | connectionHandle, |
rgrover1 | 0:2dec89c76f68 | 115 | buttonCharacteristic.getValueHandle() + 1, /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */ |
rgrover1 | 0:2dec89c76f68 | 116 | sizeof(uint16_t), /* HACK Alert! size should be made into a BLE_API constant. */ |
rgrover1 | 0:2dec89c76f68 | 117 | reinterpret_cast<const uint8_t *>(&value)); |
rgrover1 | 0:2dec89c76f68 | 118 | } |
rgrover1 | 0:2dec89c76f68 | 119 | ble.waitForEvent(); |
rgrover1 | 0:2dec89c76f68 | 120 | } |
rgrover1 | 0:2dec89c76f68 | 121 | } |