Test for peripheral device B, iBeacon
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.h" 00019 #define DUMP_ADV_DATA 0 00020 00021 #define BLE_CHECK(X) (X == BLE_ERROR_NONE) ? (printf("{{success}}\r\n")) : printf("{{failure}} %s at line %u\r\n", #X, __LINE__); 00022 #define BLE_EQUAL(X,Y) ((X)==(Y)) ? (printf("{{sucess}}\n")) : printf("{{failure}}\n"); 00023 00024 BLE ble; 00025 uint8_t address[6]; 00026 DigitalOut myled(p21); 00027 Ticker tick; 00028 00029 00030 00031 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { 00032 for (int i = 0; i < 5; i++){ 00033 if(address[i] != params->peerAddr[i]){ 00034 return; 00035 } 00036 } 00037 printf("ScanResp: %u, Data: %d\r\n", params->isScanResponse, *(params->advertisingData + params->advertisingDataLen -1)); 00038 // printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u, Data: %d\r\n", 00039 // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00040 // params->rssi, params->isScanResponse, params->type, *(params->advertisingData + params->advertisingDataLen -1)); 00041 00042 // printf("Adv peerAddr: [%d %d %d %d %d %d] rssi %d, ScanResp: %u, AdvType: %u\r\n", 00043 // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], 00044 // params->rssi, params->isScanResponse, params->type); 00045 00046 #if DUMP_ADV_DATA 00047 for (unsigned index = 0; index < params->advertisingDataLen; index++) { 00048 printf("%02x ", params->advertisingData[index]); 00049 } 00050 printf("\r\n"); 00051 #endif /* DUMP_ADV_DATA */ 00052 } 00053 00054 void blink(void){ 00055 myled = !myled; 00056 } 00057 00058 int main(void) 00059 { 00060 myled = 1; 00061 printf("{{success}}" "\n" "{{end}}" "\n"); 00062 tick.attach(blink,1); 00063 for (int i = 0; i < 6; i++){ 00064 scanf("%hhu",&address[i]); 00065 } 00066 00067 BLE_CHECK(ble.init()); 00068 00069 BLE_CHECK(ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */, 0, true)); 00070 BLE_CHECK(ble.gap().startScan(advertisementCallback)); 00071 00072 00073 }
Generated on Thu Jul 28 2022 19:24:23 by 1.7.2