Central/ Peripheral example: Central Device connects to a Peripheral called "LED" and additionally a led can be toggled with a button -> BLE_LED_PERIPHERAL needed
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LED_CENTRAL 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 00022 DigitalOut alivenessLED(p16, 0); 00023 InterruptIn button(p9); 00024 Serial pc(p5, p4); 00025 Ticker ticker, ticker2; 00026 00027 static DiscoveredCharacteristic ledCharacteristic; 00028 static const char PEER_NAME[] = "LED"; 00029 00030 void periodicCallback(void) 00031 { 00032 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ 00033 } 00034 00035 void buttonPressedCallback(void) 00036 { 00037 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access 00038 * BLE device API from the main thread. */ 00039 uint8_t toggledValue = 0x1; 00040 ledCharacteristic.write(1, &toggledValue); 00041 pc.printf("LED on... \n"); 00042 } 00043 00044 void buttonReleasedCallback(void) 00045 { 00046 /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access 00047 * BLE device API from the main thread. */ 00048 uint8_t toggledValue = 0x0; 00049 ledCharacteristic.write(1, &toggledValue); 00050 pc.printf("LED off... \n"); 00051 } 00052 00053 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) 00054 { 00055 // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME 00056 // The advertising payload is a collection of key/value records where 00057 // byte 0: length of the record excluding this byte 00058 // byte 1: The key, it is the type of the data 00059 // byte [2..N] The value. N is equal to byte0 - 1 00060 for (uint8_t i = 0; i < params->advertisingDataLen; ++i) { 00061 00062 const uint8_t record_length = params->advertisingData[i]; 00063 if (record_length == 0) { 00064 continue; 00065 } 00066 const uint8_t type = params->advertisingData[i + 1]; 00067 const uint8_t* value = params->advertisingData + i + 2; 00068 const uint8_t value_length = record_length - 1; 00069 00070 if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) { 00071 if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) { 00072 pc.printf( 00073 "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", 00074 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], 00075 params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type 00076 ); 00077 BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); 00078 break; 00079 } 00080 } 00081 i += record_length; 00082 } 00083 } 00084 00085 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) 00086 { 00087 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); 00088 if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ 00089 ledCharacteristic = *characteristicP; 00090 } 00091 } 00092 00093 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00094 { 00095 if (params->role == Gap::CENTRAL) { 00096 BLE &ble = BLE::Instance(); 00097 //ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); 00098 ble.gattClient().launchServiceDiscovery(params->handle, NULL, characteristicDiscoveryCallback, 0xa000, 0xa001); 00099 } 00100 } 00101 00102 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) 00103 { 00104 pc.printf("disconnected\r\n"); 00105 /* Start scanning and try to connect again */ 00106 BLE::Instance().gap().startScan(advertisementCallback); 00107 } 00108 00109 void onBleInitError(BLE &ble, ble_error_t error) 00110 { 00111 /* Initialization error handling should go here */ 00112 } 00113 00114 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00115 { 00116 BLE& ble = params->ble; 00117 ble_error_t error = params->error; 00118 00119 if (error != BLE_ERROR_NONE) { 00120 /* In case of error, forward the error handling to onBleInitError */ 00121 onBleInitError(ble, error); 00122 return; 00123 } 00124 00125 /* Ensure that it is the default instance of BLE */ 00126 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00127 return; 00128 } 00129 00130 ble.gap().onDisconnection(disconnectionCallback); 00131 ble.gap().onConnection(connectionCallback); 00132 00133 // scan interval: 400ms and scan window: 400ms. 00134 // Every 400ms the device will scan for 400ms 00135 // This means that the device will scan continuously. 00136 ble.gap().setScanParams(400, 400); 00137 ble.gap().startScan(advertisementCallback); 00138 } 00139 00140 00141 int main(void) { 00142 pc.baud(115200); 00143 pc.printf("Initialization starts... \n"); 00144 00145 ticker.attach(periodicCallback, 1); /* Blink LED every second */ 00146 button.fall(buttonPressedCallback); 00147 button.rise(buttonReleasedCallback); 00148 00149 BLE &ble = BLE::Instance(); 00150 ble.init(bleInitComplete); 00151 00152 while (true) { 00153 ble.waitForEvent(); 00154 } 00155 }
Generated on Tue Sep 13 2022 17:54:23 by
1.7.2
