1154

Dependencies:   BLE_API Peripheral_1 mbed nRF51822

Fork of Peripheral_1_serial by -deleted-

Committer:
DDUdui
Date:
Tue Aug 18 07:06:07 2015 +0000
Revision:
0:482d3fcc94f4
Child:
1:c227f3064399
Peripheral_1

Who changed what in which revision?

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