Beacon demo for the BLE API using the nRF51822 native mode drivers
Dependencies: BLE_API SDFileSystem mbed-rtos mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_iBeacon by
Revision 80:a1cd660e00d1, committed 2016-10-21
- Comitter:
- tangcan0823
- Date:
- Fri Oct 21 04:18:17 2016 +0000
- Parent:
- 79:4e16bde5c10a
- Commit message:
- memory
Changed in this revision
diff -r 4e16bde5c10a -r a1cd660e00d1 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri Oct 21 04:18:17 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/neilt6/code/SDFileSystem/#e4d2567200db
diff -r 4e16bde5c10a -r a1cd660e00d1 main.cpp --- a/main.cpp Tue Sep 20 12:41:28 2016 +0100 +++ b/main.cpp Fri Oct 21 04:18:17 2016 +0000 @@ -1,61 +1,160 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2015 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +#include "mbed.h" +#include "ble/BLE.h" +#include "ble/services/HeartRateService.h" +#include "ble/services/BatteryService.h" +#include "ble/services/DeviceInformationService.h" +#include "rtos.h" +#include <string.h> +#include "SDFileSystem.h" + +Semaphore one_slot(1); + +SDFileSystem sd(p25, p28, p29, p21, "sd"); +BLE ble; +int address[10][12]={}; +const GapScanningParams scanningParams; + + +int counter= 0; +void onScanCallback(const Gap::AdvertisementCallbackParams_t *params) +{ +time_t seconds = time(NULL); // JST +struct tm *t = localtime(&seconds); +/*printf("%04d/%02d/%02d %02d:%02d:%02d\r\n", +t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); +*/ + +int i=0,a=0,b=0,flag=0; +for(i=0;i<10;i++){ +if(address[i][0]==params->peerAddr[5]){ +flag=1; +break; +} +} +if(flag==0){ +for(i=0; i<6; i++){ +address[counter][i]=params->peerAddr[5-i]; +} +time_t seconds = time(NULL); +struct tm *t = localtime(&seconds); +address[counter][6]=t->tm_year; +address[counter][7]=t->tm_mon; +address[counter][8]= t->tm_mday; +address[counter][9]= t->tm_hour; +address[counter][10]= t->tm_min; +address[counter][11]= t->tm_sec; + +counter++; +for(a=0; a<counter; a++){ +for(b=0; b<12 ; b++){ + FILE *fp = fopen("/sd/mydata/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + } +if(b==0){printf("DEV:"); +fprintf(fp,"DEV:");} +if(b<6){printf("%02x ", address[a][b]); +fprintf(fp,"%02x ", address[a][b]);} +else if(b>5){printf("%02d:",address[a][b]); +fprintf(fp,"%02d:",address[a][b]);} +if(b==11){ +printf("\r\n"); +fprintf(fp,"\r\n"); + fclose(fp); + +} +} + +if(a==counter-1)printf("----------\n\r"); +} +} + -#include "mbed.h" -#include "ble/services/iBeacon.h" +/* for(int i=0; i<6; i++){ +address[0][i]=params->peerAddr[5-i]; +} +for(int b=0; b<6 ; b++){ +if(b==0)printf("DEV:"); +printf("%02x ", address[0][b]); +if(b==5)printf("\n\r"); +}*/ +} + + +void test_thread(void const *name) { + + +while (true) { +Timer timer; +one_slot.wait(); +printf("%s\n\r", (const char*)name); +//1 +if(!strcmp((const char*)name, "1")){ +printf("**this is 1**\n\r"); +ble.gap().stopAdvertising(); +ble.startScan(&onScanCallback); + +} -BLE ble; - -void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) -{ - BLE &ble = params->ble; - ble_error_t error = params->error; +//2 +if(!strcmp((const char*)name, "2")){ +printf("**this is 2**\n\r"); +timer.start(); +while(1){ +ble.waitForEvent(); +if(timer.read() > 9){ +timer.stop(); +timer.reset(); +ble.stopScan(); +break; +} +} +} +//3 +if(!strcmp((const char*)name, "3")){ +printf("**this is 3**\n\r"); +ble.gap().startAdvertising(); + +} + - if (error != BLE_ERROR_NONE) { - return; - } +Thread::wait(10000); +one_slot.release(); +} +} + +// const static char DEVICE_NAME[] = "BLE1"; + +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ +BLE &ble = params->ble; +ble_error_t error = params->error; + +if (error != BLE_ERROR_NONE) { +return; +} +const uint8_t address1[] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA}; +ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, address1); +// ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); +// ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); +// ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ +} + +int main (void) { + +mkdir("/sd/mydata", 0777); - /** - * The Beacon payload has the following composition: - * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 - * Major/Minor = 0x1122 / 0x3344 - * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB) - * - * Note: please remember to calibrate your beacons TX Power for more accurate results. - */ - const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, - 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61}; - uint16_t majorNumber = 1122; - uint16_t minorNumber = 3344; - uint16_t txPower = 0xC8; - iBeacon *ibeacon = new iBeacon(ble, uuid, majorNumber, minorNumber, txPower); +//ble.init(); +ble.init(bleInitComplete); +ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MAX, +GapScanningParams::SCAN_WINDOW_MAX, 0); + + - ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ - ble.gap().startAdvertising(); -} + +Thread t2(test_thread, (void *)"2"); +Thread t3(test_thread, (void *)"3"); -int main(void) -{ - ble.init(bleInitComplete); - - /* SpinWait for initialization to complete. This is necessary because the - * BLE object is used in the main loop below. */ - while (!ble.hasInitialized()) { /* spin loop */ } +test_thread((void *)"1"); - while (true) { - ble.waitForEvent(); // allows or low power operation - } -} +} \ No newline at end of file
diff -r 4e16bde5c10a -r a1cd660e00d1 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Fri Oct 21 04:18:17 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#3da5f554d8bf
diff -r 4e16bde5c10a -r a1cd660e00d1 mbed_app.json --- a/mbed_app.json Tue Sep 20 12:41:28 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -{ - "target_overrides": { - "K64F": { - "target.features_add": ["BLE"], - "target.extra_labels_add": ["ST_BLUENRG"], - "target.macros_add": ["IDB0XA1_D13_PATCH"] - }, - "NUCLEO_F401RE": { - "target.features_add": ["BLE"], - "target.extra_labels_add": ["ST_BLUENRG"] - } - } -} \ No newline at end of file