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.
Fork of mbed-os-example-ble-EddystoneObserver by
main.cpp
00001 #include <events/mbed_events.h> 00002 #include "mbed.h" 00003 #include "ble/BLE.h" 00004 #include "ble/DiscoveredCharacteristic.h" 00005 #include "ble/DiscoveredService.h" 00006 #include "ble/services/HeartRateService.h" 00007 00008 DigitalOut led1(LED1, 1); 00009 //I2C i2c(p27, p26); // sda, scl 00010 Serial uart(p13, p14); // tx, rx 00011 static DiscoveredCharacteristic ledCharacteristic; 00012 static bool triggerLedCharacteristic; 00013 static const char PEER_NAME[] = "HRM"; 00014 //const int address = 0x3C<<1; 00015 00016 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00017 char HeartRate_Data = 0; 00018 00019 void periodicCallback(void) { 00020 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00021 } 00022 00023 00024 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { 00025 // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME 00026 // The advertising payload is a collection of key/value records where 00027 // byte 0: length of the record excluding this byte 00028 // byte 1: The key, it is the type of the data 00029 // byte [2..N] The value. N is equal to byte0 - 1 00030 printf("Hello:)\r\n"); 00031 printf( 00032 "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", 00033 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], 00034 params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type 00035 ); 00036 for (uint8_t i = 0; i < params->advertisingDataLen; ++i) { 00037 00038 const uint8_t record_length = params->advertisingData[i]; 00039 if (record_length == 0) { 00040 continue; 00041 } 00042 const uint8_t type = params->advertisingData[i + 1]; 00043 const uint8_t* value = params->advertisingData + i + 2; 00044 const uint8_t value_length = record_length - 1; 00045 00046 00047 if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) { 00048 if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) { 00049 printf( 00050 "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", 00051 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], 00052 params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type 00053 ); 00054 BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); 00055 break; 00056 } 00057 } 00058 i += record_length; 00059 } 00060 } 00061 00062 00063 void serviceDiscoveryCallback(const DiscoveredService *service) { 00064 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { 00065 printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); 00066 } else { 00067 printf("S UUID-"); 00068 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); 00069 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { 00070 printf("%02x", longUUIDBytes[i]); 00071 } 00072 printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); 00073 } 00074 } 00075 00076 00077 void updateLedCharacteristic(void) { 00078 if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) { 00079 ledCharacteristic.read(); 00080 } 00081 } 00082 00083 void triggerToggledWrite(const GattReadCallbackParams *response) { 00084 printf("triggerToggledWrite\r\n"); 00085 printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); 00086 HeartRate_Data = response->data[1]; 00087 printf("HeartRate_Data = %d\r\n", HeartRate_Data); 00088 uart.putc(HeartRate_Data); 00089 ledCharacteristic.read(); 00090 } 00091 00092 00093 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { 00094 printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); 00095 if (characteristicP->getUUID().getShortUUID() == 0x2a37) { /* !ALERT! Alter this filter to suit your device. */ 00096 characteristicP->read(0); 00097 ledCharacteristic = *characteristicP; 00098 triggerLedCharacteristic = true; 00099 } 00100 } 00101 00102 00103 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { 00104 printf("terminated SD for handle %u\r\n", connectionHandle); 00105 if (triggerLedCharacteristic) { 00106 triggerLedCharacteristic = false; 00107 eventQueue.call(updateLedCharacteristic); 00108 } 00109 } 00110 00111 00112 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { 00113 printf("connectionCallback\r\n"); 00114 if (params->role == Gap::CENTRAL) { 00115 printf("Gap::CENTRAL\r\n"); 00116 BLE &ble = BLE::Instance(); 00117 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 00118 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/); 00119 } 00120 } 00121 00122 void triggerRead(const GattWriteCallbackParams *response) { 00123 printf("triggerRead\r\n"); 00124 if (response->handle == ledCharacteristic.getValueHandle()) { 00125 ledCharacteristic.read(); 00126 } 00127 } 00128 00129 00130 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { 00131 printf("disconnected\r\n"); 00132 /* Start scanning and try to connect again */ 00133 BLE::Instance().gap().startScan(advertisementCallback); 00134 } 00135 00136 00137 void onBleInitError(BLE &ble, ble_error_t error) 00138 { 00139 /* Initialization error handling should go here */ 00140 } 00141 00142 00143 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00144 { 00145 BLE& ble = params->ble; 00146 ble_error_t error = params->error; 00147 00148 printf("Init_1\r\n"); 00149 00150 if (error != BLE_ERROR_NONE) { 00151 /* In case of error, forward the error handling to onBleInitError */ 00152 onBleInitError(ble, error); 00153 return; 00154 } 00155 00156 printf("Init_2\r\n"); 00157 00158 /* Ensure that it is the default instance of BLE */ 00159 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00160 return; 00161 } 00162 00163 00164 ble.gap().onDisconnection(disconnectionCallback); 00165 ble.gap().onConnection(connectionCallback); 00166 00167 00168 ble.gattClient().onDataRead(triggerToggledWrite); 00169 ble.gattClient().onDataWrite(triggerRead); 00170 00171 00172 // scan interval: 400ms and scan window: 400ms. 00173 // Every 400ms the device will scan for 400ms 00174 // This means that the device will scan continuously. 00175 ble.gap().setScanParams(400, 400); 00176 ble.gap().startScan(advertisementCallback); 00177 } 00178 00179 00180 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 00181 BLE &ble = BLE::Instance(); 00182 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00183 } 00184 00185 00186 int main() 00187 { 00188 uart.baud(115200); 00189 printf("main_1\r\n"); 00190 triggerLedCharacteristic = false; 00191 eventQueue.call_every(1000, periodicCallback); 00192 00193 printf("main_2\r\n"); 00194 BLE &ble = BLE::Instance(); 00195 ble.onEventsToProcess(scheduleBleEventsProcessing); 00196 ble.init(bleInitComplete); 00197 printf("main_3\r\n"); 00198 00199 eventQueue.dispatch_forever(); 00200 00201 printf("main_4\r\n"); 00202 00203 return 0; 00204 }
Generated on Sat Jul 23 2022 11:05:38 by
1.7.2
