Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Observer by
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 BLE ble; 00022 DigitalOut led1(LED1); 00023 DigitalOut led2(P0_28); 00024 DigitalOut led3(P0_29); 00025 Serial pc(USBTX, USBRX); 00026 00027 00028 void periodicCallback(void) 00029 { 00030 led1 = !led1; /* 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] == 0xcb) { /* !ALERT! Alter this filter to suit your device. */ 00035 return; 00036 } 00037 pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %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 for (unsigned index = 0; index < params->advertisingDataLen; index++) { 00042 pc.printf("%02x ", params->advertisingData[index]); 00043 } 00044 pc.printf("\r\n"); 00045 /* DUMP_ADV_DATA */ 00046 00047 ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); 00048 00049 } 00050 00051 void serviceDiscoveryCallback(const DiscoveredService *service) { 00052 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 00053 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 00054 } else { 00055 printf("S UUID-"); 00056 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 00057 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 00058 printf("%02x", longUUIDBytes[i]); 00059 } 00060 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); 00061 } 00062 } 00063 00064 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { 00065 00066 } 00067 00068 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { 00069 pc.printf("terminated SD for handle %u\r\n", connectionHandle); 00070 } 00071 00072 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 00073 pc.printf("Connected!!\r\n"); 00074 led2=0; 00075 led3=1; 00076 ble.stopAdvertising(); 00077 wait(3); 00078 if (params->role == Gap::CENTRAL) { 00079 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 00080 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); 00081 }/* 00082 ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION); 00083 */ 00084 } 00085 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00086 { 00087 pc.printf("Disconnected\r\n"); 00088 pc.printf("Rescan\r\n"); 00089 led2=1; 00090 led3=0; 00091 ble.startScan(advertisementCallback); 00092 ble.startAdvertising(); 00093 } 00094 00095 00096 int main(void) 00097 { 00098 led1 = 1; 00099 led2 = 1; 00100 led3 = 0; 00101 Ticker ticker; 00102 ticker.attach(periodicCallback, 1); 00103 ble.onConnection(connectionCallback); 00104 ble.onDisconnection(disconnectionCallback); 00105 00106 ble.init(); 00107 00108 pc.baud(9600); 00109 pc.printf("Observer Init \r\n"); 00110 00111 00112 /* */ 00113 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00114 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00115 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00116 (const uint8_t *)"RAMO2", sizeof("RAMO2") - 1); 00117 00118 ble.gap().setAdvertisingInterval(100); /* 1second. */ 00119 ble.gap().startAdvertising(); 00120 00121 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); 00122 ble.gap().startScan(advertisementCallback); 00123 00124 while (true) { 00125 ble.waitForEvent(); 00126 } 00127 }
Generated on Tue Jul 12 2022 21:32:07 by
1.7.2
