skaner
Dependencies: BLE_API TextLCD mbed nRF51822
Fork of BLE_LEDBlinker by
main.cpp@11:023d96b0e427, 2016-01-12 (annotated)
- Committer:
- andresag
- Date:
- Tue Jan 12 10:49:03 2016 +0000
- Revision:
- 11:023d96b0e427
- Parent:
- 10:507318f2afda
- Child:
- 12:61812ca1f049
Update example to latest BLE API.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:415d7f24cb91 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:415d7f24cb91 | 2 | * Copyright (c) 2006-2015 ARM Limited |
rgrover1 | 0:415d7f24cb91 | 3 | * |
rgrover1 | 0:415d7f24cb91 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:415d7f24cb91 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:415d7f24cb91 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:415d7f24cb91 | 7 | * |
rgrover1 | 0:415d7f24cb91 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:415d7f24cb91 | 9 | * |
rgrover1 | 0:415d7f24cb91 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:415d7f24cb91 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:415d7f24cb91 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:415d7f24cb91 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:415d7f24cb91 | 14 | * limitations under the License. |
rgrover1 | 0:415d7f24cb91 | 15 | */ |
rgrover1 | 0:415d7f24cb91 | 16 | |
rgrover1 | 0:415d7f24cb91 | 17 | #include "mbed.h" |
rgrover1 | 6:1730f66fb14d | 18 | #include "ble/BLE.h" |
rgrover1 | 6:1730f66fb14d | 19 | #include "ble/DiscoveredCharacteristic.h" |
rgrover1 | 6:1730f66fb14d | 20 | #include "ble/DiscoveredService.h" |
rgrover1 | 0:415d7f24cb91 | 21 | |
andresag | 11:023d96b0e427 | 22 | DigitalOut alivenessLED(LED1, 1); |
rgrover1 | 6:1730f66fb14d | 23 | |
andresag | 11:023d96b0e427 | 24 | bool triggerLedCharacteristic = false; |
rgrover1 | 0:415d7f24cb91 | 25 | DiscoveredCharacteristic ledCharacteristic; |
rgrover1 | 0:415d7f24cb91 | 26 | |
andresag | 11:023d96b0e427 | 27 | Ticker ticker; |
andresag | 11:023d96b0e427 | 28 | |
rgrover1 | 0:415d7f24cb91 | 29 | void periodicCallback(void) { |
rgrover1 | 3:146eba831693 | 30 | alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ |
rgrover1 | 0:415d7f24cb91 | 31 | } |
rgrover1 | 0:415d7f24cb91 | 32 | |
rgrover1 | 0:415d7f24cb91 | 33 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
andresag | 11:023d96b0e427 | 34 | if (params->peerAddr[0] != 0x37) { /* !ALERT! Alter this filter to suit your device. */ |
rgrover1 | 0:415d7f24cb91 | 35 | return; |
rgrover1 | 0:415d7f24cb91 | 36 | } |
rgrover1 | 0:415d7f24cb91 | 37 | printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
rgrover1 | 0:415d7f24cb91 | 38 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
rgrover1 | 0:415d7f24cb91 | 39 | params->rssi, params->isScanResponse, params->type); |
rgrover1 | 0:415d7f24cb91 | 40 | |
andresag | 11:023d96b0e427 | 41 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
rgrover1 | 0:415d7f24cb91 | 42 | } |
rgrover1 | 0:415d7f24cb91 | 43 | |
rgrover1 | 0:415d7f24cb91 | 44 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
rgrover1 | 0:415d7f24cb91 | 45 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
rgrover1 | 0:415d7f24cb91 | 46 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:415d7f24cb91 | 47 | } else { |
rgrover1 | 0:415d7f24cb91 | 48 | printf("S UUID-"); |
rgrover1 | 0:415d7f24cb91 | 49 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
rgrover1 | 0:415d7f24cb91 | 50 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
rgrover1 | 0:415d7f24cb91 | 51 | printf("%02x", longUUIDBytes[i]); |
rgrover1 | 0:415d7f24cb91 | 52 | } |
rgrover1 | 0:415d7f24cb91 | 53 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
rgrover1 | 0:415d7f24cb91 | 54 | } |
rgrover1 | 0:415d7f24cb91 | 55 | } |
rgrover1 | 0:415d7f24cb91 | 56 | |
rgrover1 | 0:415d7f24cb91 | 57 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
rgrover1 | 10:507318f2afda | 58 | printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
rgrover1 | 10:507318f2afda | 59 | if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ |
rgrover1 | 1:1db45b17552e | 60 | ledCharacteristic = *characteristicP; |
rgrover1 | 1:1db45b17552e | 61 | triggerLedCharacteristic = true; |
rgrover1 | 0:415d7f24cb91 | 62 | } |
rgrover1 | 0:415d7f24cb91 | 63 | } |
rgrover1 | 0:415d7f24cb91 | 64 | |
rgrover1 | 0:415d7f24cb91 | 65 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
rgrover1 | 0:415d7f24cb91 | 66 | printf("terminated SD for handle %u\r\n", connectionHandle); |
rgrover1 | 0:415d7f24cb91 | 67 | } |
rgrover1 | 0:415d7f24cb91 | 68 | |
rgrover1 | 0:415d7f24cb91 | 69 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
rgrover1 | 0:415d7f24cb91 | 70 | if (params->role == Gap::CENTRAL) { |
andresag | 11:023d96b0e427 | 71 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
andresag | 11:023d96b0e427 | 72 | BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
rgrover1 | 0:415d7f24cb91 | 73 | } |
rgrover1 | 0:415d7f24cb91 | 74 | } |
rgrover1 | 0:415d7f24cb91 | 75 | |
rgrover1 | 0:415d7f24cb91 | 76 | void triggerToggledWrite(const GattReadCallbackParams *response) { |
rgrover1 | 0:415d7f24cb91 | 77 | if (response->handle == ledCharacteristic.getValueHandle()) { |
rgrover1 | 0:415d7f24cb91 | 78 | #if DUMP_READ_DATA |
rgrover1 | 0:415d7f24cb91 | 79 | printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
rgrover1 | 0:415d7f24cb91 | 80 | for (unsigned index = 0; index < response->len; index++) { |
rgrover1 | 0:415d7f24cb91 | 81 | printf("%c[%02x]", response->data[index], response->data[index]); |
rgrover1 | 0:415d7f24cb91 | 82 | } |
rgrover1 | 0:415d7f24cb91 | 83 | printf("\r\n"); |
rgrover1 | 0:415d7f24cb91 | 84 | #endif |
rgrover1 | 0:415d7f24cb91 | 85 | |
rgrover1 | 0:415d7f24cb91 | 86 | uint8_t toggledValue = response->data[0] ^ 0x1; |
rgrover1 | 0:415d7f24cb91 | 87 | ledCharacteristic.write(1, &toggledValue); |
rgrover1 | 0:415d7f24cb91 | 88 | } |
rgrover1 | 0:415d7f24cb91 | 89 | } |
rgrover1 | 0:415d7f24cb91 | 90 | |
rgrover1 | 0:415d7f24cb91 | 91 | void triggerRead(const GattWriteCallbackParams *response) { |
rgrover1 | 0:415d7f24cb91 | 92 | if (response->handle == ledCharacteristic.getValueHandle()) { |
rgrover1 | 0:415d7f24cb91 | 93 | ledCharacteristic.read(); |
rgrover1 | 0:415d7f24cb91 | 94 | } |
rgrover1 | 0:415d7f24cb91 | 95 | } |
rgrover1 | 0:415d7f24cb91 | 96 | |
rgrover1 | 10:507318f2afda | 97 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { |
rgrover1 | 0:415d7f24cb91 | 98 | printf("disconnected\r\n"); |
rgrover1 | 0:415d7f24cb91 | 99 | } |
rgrover1 | 0:415d7f24cb91 | 100 | |
andresag | 11:023d96b0e427 | 101 | /** |
andresag | 11:023d96b0e427 | 102 | * This function is called when the ble initialization process has failed |
andresag | 11:023d96b0e427 | 103 | */ |
andresag | 11:023d96b0e427 | 104 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 11:023d96b0e427 | 105 | { |
andresag | 11:023d96b0e427 | 106 | /* Initialization error handling should go here */ |
andresag | 11:023d96b0e427 | 107 | } |
rgrover1 | 0:415d7f24cb91 | 108 | |
andresag | 11:023d96b0e427 | 109 | /** |
andresag | 11:023d96b0e427 | 110 | * Callback triggered when the ble initialization process has finished |
andresag | 11:023d96b0e427 | 111 | */ |
andresag | 11:023d96b0e427 | 112 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 11:023d96b0e427 | 113 | { |
andresag | 11:023d96b0e427 | 114 | BLE& ble = params->ble; |
andresag | 11:023d96b0e427 | 115 | ble_error_t error = params->error; |
andresag | 11:023d96b0e427 | 116 | |
andresag | 11:023d96b0e427 | 117 | if (error != BLE_ERROR_NONE) { |
andresag | 11:023d96b0e427 | 118 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 11:023d96b0e427 | 119 | onBleInitError(ble, error); |
andresag | 11:023d96b0e427 | 120 | return; |
andresag | 11:023d96b0e427 | 121 | } |
andresag | 11:023d96b0e427 | 122 | |
andresag | 11:023d96b0e427 | 123 | /* Ensure that it is the default instance of BLE */ |
andresag | 11:023d96b0e427 | 124 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 11:023d96b0e427 | 125 | return; |
andresag | 11:023d96b0e427 | 126 | } |
andresag | 11:023d96b0e427 | 127 | |
rgrover1 | 5:3bbad34d1a85 | 128 | ble.gap().onConnection(connectionCallback); |
rgrover1 | 5:3bbad34d1a85 | 129 | ble.gap().onDisconnection(disconnectionCallback); |
rgrover1 | 0:415d7f24cb91 | 130 | |
rgrover1 | 7:61e2224ec9a0 | 131 | ble.gattClient().onDataRead(triggerToggledWrite); |
rgrover1 | 7:61e2224ec9a0 | 132 | ble.gattClient().onDataWrite(triggerRead); |
rgrover1 | 0:415d7f24cb91 | 133 | |
rgrover1 | 5:3bbad34d1a85 | 134 | ble.gap().setScanParams(500, 400); |
rgrover1 | 5:3bbad34d1a85 | 135 | ble.gap().startScan(advertisementCallback); |
andresag | 11:023d96b0e427 | 136 | } |
andresag | 11:023d96b0e427 | 137 | |
andresag | 11:023d96b0e427 | 138 | int main(void) { |
andresag | 11:023d96b0e427 | 139 | ticker.attach(periodicCallback, 1); |
andresag | 11:023d96b0e427 | 140 | |
andresag | 11:023d96b0e427 | 141 | BLE &ble = BLE::Instance(); |
andresag | 11:023d96b0e427 | 142 | ble.init(bleInitComplete); |
andresag | 11:023d96b0e427 | 143 | |
andresag | 11:023d96b0e427 | 144 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 11:023d96b0e427 | 145 | * BLE object is used in the main loop below. */ |
andresag | 11:023d96b0e427 | 146 | while (ble.hasInitialized() == false) { /* spin loop */ } |
rgrover1 | 0:415d7f24cb91 | 147 | |
rgrover1 | 0:415d7f24cb91 | 148 | while (true) { |
rgrover1 | 4:460ce53dc854 | 149 | if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) { |
rgrover1 | 1:1db45b17552e | 150 | triggerLedCharacteristic = false; |
rgrover1 | 0:415d7f24cb91 | 151 | ledCharacteristic.read(); /* We could have issued this read just as easily from |
rgrover1 | 0:415d7f24cb91 | 152 | * characteristicDiscoveryCallback(); but |
rgrover1 | 2:3e1e967035cb | 153 | * invoking it here demonstrates the use |
rgrover1 | 2:3e1e967035cb | 154 | * of isServiceDiscoveryActive() and also |
rgrover1 | 2:3e1e967035cb | 155 | * the fact that it is permitted to |
rgrover1 | 2:3e1e967035cb | 156 | * operate on application-local copies of |
rgrover1 | 2:3e1e967035cb | 157 | * DiscoveredCharacteristic. */ |
rgrover1 | 0:415d7f24cb91 | 158 | } |
rgrover1 | 0:415d7f24cb91 | 159 | ble.waitForEvent(); |
rgrover1 | 0:415d7f24cb91 | 160 | } |
rgrover1 | 0:415d7f24cb91 | 161 | } |