asd
Dependencies: BLE_API mbed nRF51822
Fork of Central_1 by
main.cpp@0:51c847e94a29, 2015-08-17 (annotated)
- Committer:
- DDUdui
- Date:
- Mon Aug 17 09:17:25 2015 +0000
- Revision:
- 0:51c847e94a29
- Child:
- 1:df5573997c91
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DDUdui | 0:51c847e94a29 | 1 | /* mbed Microcontroller Library |
DDUdui | 0:51c847e94a29 | 2 | * Copyright (c) 2006-2015 ARM Limited |
DDUdui | 0:51c847e94a29 | 3 | * |
DDUdui | 0:51c847e94a29 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DDUdui | 0:51c847e94a29 | 5 | * you may not use this file except in compliance with the License. |
DDUdui | 0:51c847e94a29 | 6 | * You may obtain a copy of the License at |
DDUdui | 0:51c847e94a29 | 7 | * |
DDUdui | 0:51c847e94a29 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DDUdui | 0:51c847e94a29 | 9 | * |
DDUdui | 0:51c847e94a29 | 10 | * Unless required by applicable law or agreed to in writing, software |
DDUdui | 0:51c847e94a29 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DDUdui | 0:51c847e94a29 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DDUdui | 0:51c847e94a29 | 13 | * See the License for the specific language governing permissions and |
DDUdui | 0:51c847e94a29 | 14 | * limitations under the License. |
DDUdui | 0:51c847e94a29 | 15 | */ |
DDUdui | 0:51c847e94a29 | 16 | |
DDUdui | 0:51c847e94a29 | 17 | #include "mbed.h" |
DDUdui | 0:51c847e94a29 | 18 | #include "ble/BLE.h" |
DDUdui | 0:51c847e94a29 | 19 | #include "ble/DiscoveredCharacteristic.h" |
DDUdui | 0:51c847e94a29 | 20 | #include "ble/DiscoveredService.h" |
DDUdui | 0:51c847e94a29 | 21 | BLE ble; |
DDUdui | 0:51c847e94a29 | 22 | DigitalOut led1(LED1); |
DDUdui | 0:51c847e94a29 | 23 | Serial pc(USBTX, USBRX); |
DDUdui | 0:51c847e94a29 | 24 | |
DDUdui | 0:51c847e94a29 | 25 | |
DDUdui | 0:51c847e94a29 | 26 | void periodicCallback(void) |
DDUdui | 0:51c847e94a29 | 27 | { |
DDUdui | 0:51c847e94a29 | 28 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
DDUdui | 0:51c847e94a29 | 29 | } |
DDUdui | 0:51c847e94a29 | 30 | |
DDUdui | 0:51c847e94a29 | 31 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
DDUdui | 0:51c847e94a29 | 32 | |
DDUdui | 0:51c847e94a29 | 33 | pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", |
DDUdui | 0:51c847e94a29 | 34 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
DDUdui | 0:51c847e94a29 | 35 | params->rssi, params->isScanResponse, params->type); |
DDUdui | 0:51c847e94a29 | 36 | |
DDUdui | 0:51c847e94a29 | 37 | |
DDUdui | 0:51c847e94a29 | 38 | pc.printf("%s", params->advertisingData); |
DDUdui | 0:51c847e94a29 | 39 | |
DDUdui | 0:51c847e94a29 | 40 | pc.printf("\r\n"); |
DDUdui | 0:51c847e94a29 | 41 | |
DDUdui | 0:51c847e94a29 | 42 | } |
DDUdui | 0:51c847e94a29 | 43 | |
DDUdui | 0:51c847e94a29 | 44 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
DDUdui | 0:51c847e94a29 | 45 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
DDUdui | 0:51c847e94a29 | 46 | pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
DDUdui | 0:51c847e94a29 | 47 | } else { |
DDUdui | 0:51c847e94a29 | 48 | printf("S UUID-"); |
DDUdui | 0:51c847e94a29 | 49 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
DDUdui | 0:51c847e94a29 | 50 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
DDUdui | 0:51c847e94a29 | 51 | printf("%02x", longUUIDBytes[i]); |
DDUdui | 0:51c847e94a29 | 52 | } |
DDUdui | 0:51c847e94a29 | 53 | pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
DDUdui | 0:51c847e94a29 | 54 | } |
DDUdui | 0:51c847e94a29 | 55 | } |
DDUdui | 0:51c847e94a29 | 56 | |
DDUdui | 0:51c847e94a29 | 57 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
DDUdui | 0:51c847e94a29 | 58 | |
DDUdui | 0:51c847e94a29 | 59 | } |
DDUdui | 0:51c847e94a29 | 60 | |
DDUdui | 0:51c847e94a29 | 61 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
DDUdui | 0:51c847e94a29 | 62 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
DDUdui | 0:51c847e94a29 | 63 | } |
DDUdui | 0:51c847e94a29 | 64 | |
DDUdui | 0:51c847e94a29 | 65 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
DDUdui | 0:51c847e94a29 | 66 | pc.printf("Connected!!\r\n"); |
DDUdui | 0:51c847e94a29 | 67 | ble.stopAdvertising(); |
DDUdui | 0:51c847e94a29 | 68 | wait(1); |
DDUdui | 0:51c847e94a29 | 69 | if (params->role == Gap::CENTRAL) { |
DDUdui | 0:51c847e94a29 | 70 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
DDUdui | 0:51c847e94a29 | 71 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
DDUdui | 0:51c847e94a29 | 72 | } |
DDUdui | 0:51c847e94a29 | 73 | ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION); |
DDUdui | 0:51c847e94a29 | 74 | } |
DDUdui | 0:51c847e94a29 | 75 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
DDUdui | 0:51c847e94a29 | 76 | { |
DDUdui | 0:51c847e94a29 | 77 | pc.printf("Disconnected\r\n"); |
DDUdui | 0:51c847e94a29 | 78 | pc.printf("Rescan\r\n"); |
DDUdui | 0:51c847e94a29 | 79 | ble.startScan(advertisementCallback); |
DDUdui | 0:51c847e94a29 | 80 | ble.startAdvertising(); |
DDUdui | 0:51c847e94a29 | 81 | } |
DDUdui | 0:51c847e94a29 | 82 | |
DDUdui | 0:51c847e94a29 | 83 | |
DDUdui | 0:51c847e94a29 | 84 | int main(void) |
DDUdui | 0:51c847e94a29 | 85 | { |
DDUdui | 0:51c847e94a29 | 86 | led1 = 1; |
DDUdui | 0:51c847e94a29 | 87 | Ticker ticker; |
DDUdui | 0:51c847e94a29 | 88 | ticker.attach(periodicCallback, 1); |
DDUdui | 0:51c847e94a29 | 89 | ble.onConnection(connectionCallback); |
DDUdui | 0:51c847e94a29 | 90 | ble.onDisconnection(disconnectionCallback); |
DDUdui | 0:51c847e94a29 | 91 | |
DDUdui | 0:51c847e94a29 | 92 | ble.init(); |
DDUdui | 0:51c847e94a29 | 93 | |
DDUdui | 0:51c847e94a29 | 94 | pc.baud(9600); |
DDUdui | 0:51c847e94a29 | 95 | pc.printf("Observer Init \r\n"); |
DDUdui | 0:51c847e94a29 | 96 | |
DDUdui | 0:51c847e94a29 | 97 | |
DDUdui | 0:51c847e94a29 | 98 | /* */ |
DDUdui | 0:51c847e94a29 | 99 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
DDUdui | 0:51c847e94a29 | 100 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
DDUdui | 0:51c847e94a29 | 101 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
DDUdui | 0:51c847e94a29 | 102 | (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1); |
DDUdui | 0:51c847e94a29 | 103 | |
DDUdui | 0:51c847e94a29 | 104 | ble.gap().setAdvertisingInterval(100); /* 1second. */ |
DDUdui | 0:51c847e94a29 | 105 | ble.gap().startAdvertising(); |
DDUdui | 0:51c847e94a29 | 106 | |
DDUdui | 0:51c847e94a29 | 107 | ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); |
DDUdui | 0:51c847e94a29 | 108 | ble.gap().startScan(advertisementCallback); |
DDUdui | 0:51c847e94a29 | 109 | |
DDUdui | 0:51c847e94a29 | 110 | while (true) { |
DDUdui | 0:51c847e94a29 | 111 | ble.waitForEvent(); |
DDUdui | 0:51c847e94a29 | 112 | } |
DDUdui | 0:51c847e94a29 | 113 | } |