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
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 Serial pc(USBTX, USBRX); 00024 00025 00026 void periodicCallback(void) 00027 { 00028 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00029 } 00030 00031 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { 00032 00033 pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", 00034 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00035 params->rssi, params->isScanResponse, params->type); 00036 00037 00038 pc.printf("%s", params->advertisingData); 00039 00040 pc.printf("\r\n"); 00041 00042 for(unsigned i = 0; i < 3; i++) { 00043 if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3] && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) { 00044 /* Only RSSI Value update */ 00045 list_rssi[i] = params->rssi; 00046 break; 00047 } 00048 if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) { 00049 /* Save the address */ 00050 list_addr[i][5] = params->peerAddr[5]; 00051 list_addr[i][4] = params->peerAddr[4]; 00052 list_addr[i][3] = params->peerAddr[3]; 00053 list_addr[i][2] = params->peerAddr[2]; 00054 list_addr[i][1] = params->peerAddr[1]; 00055 list_addr[i][0] = params->peerAddr[0]; 00056 /* Save RSSI value */ 00057 list_rssi[i] = params->rssi; 00058 if(i==2){ 00059 /* Find Maximum RSSI value */ 00060 for(unsigned j=0;j<NUM_LIST;j++) { 00061 for(unsigned k=j ; k<NUM_LIST ; k++) { 00062 if(list_rssi[j] > list_rssi[j+1]) { 00063 /* Swap RSSI Value */ 00064 temp = list_rssi[j]; 00065 list_rssi[j] = list_rssi[j+1]; 00066 list_rssi[j+1] = temp; 00067 00068 /* Swap the address */ 00069 for(unsigned l = 0; l < 6 ; l++) { 00070 temp = list_addr[j][l]; 00071 list_addr[j][l] = list_addr[j+1][l]; 00072 list_addr[j+1][l] = temp; 00073 } 00074 } 00075 } 00076 } 00077 } 00078 break; 00079 } 00080 } 00081 } 00082 00083 void serviceDiscoveryCallback(const DiscoveredService *service) { 00084 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 00085 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 00086 } else { 00087 printf("S UUID-"); 00088 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 00089 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 00090 printf("%02x", longUUIDBytes[i]); 00091 } 00092 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); 00093 } 00094 } 00095 00096 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { 00097 00098 } 00099 00100 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { 00101 pc.printf("terminated SD for handle %u\r\n", connectionHandle); 00102 } 00103 00104 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 00105 pc.printf("Connected!!\r\n"); 00106 ble.stopAdvertising(); 00107 wait(1); 00108 if (params->role == Gap::CENTRAL) { 00109 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 00110 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); 00111 } 00112 ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION); 00113 } 00114 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00115 { 00116 pc.printf("Disconnected\r\n"); 00117 pc.printf("Rescan\r\n"); 00118 ble.startScan(advertisementCallback); 00119 ble.startAdvertising(); 00120 } 00121 00122 00123 int main(void) 00124 { 00125 led1 = 1; 00126 Ticker ticker; 00127 ticker.attach(periodicCallback, 1); 00128 ble.onConnection(connectionCallback); 00129 ble.onDisconnection(disconnectionCallback); 00130 00131 ble.init(); 00132 00133 pc.baud(9600); 00134 pc.printf("Observer Init \r\n"); 00135 00136 00137 /* */ 00138 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00139 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00140 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00141 (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1); 00142 00143 ble.gap().setAdvertisingInterval(100); /* 1second. */ 00144 ble.gap().startAdvertising(); 00145 00146 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); 00147 ble.gap().startScan(advertisementCallback); 00148 00149 while (true) { 00150 ble.waitForEvent(); 00151 } 00152 }
Generated on Mon Aug 8 2022 09:55:36 by
1.7.2