Created
October 3, 2017 01:13
-
-
Save kuc-arc-f/36a919dace241e9ac46f87103cfebed8 to your computer and use it in GitHub Desktop.
BLE_GAP_Example2/main.cpp ,micro:bit BLE driver , advertising
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BLE_GAP_Example2/main.cpp | |
/* 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 <stdio.h> | |
#include <string> | |
Serial mPc(USBTX, USBRX); | |
// Timer mTimer; | |
/* Optional: Device Name, add for human read-ability */ | |
const static char DEVICE_NAME[] = "ChangeMe!!"; | |
/* You have up to 26 bytes of advertising data to use. */ | |
//const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */ | |
//static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05,0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */ | |
static uint8_t AdvData[25 ]; /* Example of hex data */ | |
// const int mTmMax=5; | |
int mValue=0; | |
static Ticker ticker; | |
static bool triggerTempValueUpdate = false; | |
// | |
void print_debugData(){ | |
mPc.printf( "sz=%d\n" , sizeof(AdvData) ); | |
for(int i=0; i< sizeof(AdvData); i++){ | |
mPc.printf("0x%02x", AdvData[i]); | |
} | |
mPc.printf("\n"); | |
} | |
// | |
void periodicCallback(void) | |
{ | |
// alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. */ | |
triggerTempValueUpdate = true; | |
} | |
// | |
void setupApplicationData(int v1, int v2){ | |
char cRead[25 +1]; | |
sprintf(cRead , "%05d%05d%05d%05d%05d", v1 ,v2 ,0 , 0, 0 ); | |
mPc.printf("cRead=%s\n", cRead); | |
for(int i=0;i <25;i++){ | |
AdvData[i]= (uint8_t)cRead[i]; | |
} | |
// | |
print_debugData(); | |
} | |
/* Optional: Restart advertising when peer disconnects */ | |
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) | |
{ | |
BLE::Instance().gap().startAdvertising(); | |
} | |
/** | |
* This function is called when the ble initialization process has failed | |
*/ | |
void onBleInitError(BLE &ble, ble_error_t error) | |
{ | |
/* Avoid compiler warnings */ | |
(void) ble; | |
(void) error; | |
/* Initialization error handling should go here */ | |
} | |
/** | |
* Callback triggered when the ble initialization process has finished | |
*/ | |
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) | |
{ | |
BLE& ble = params->ble; | |
ble_error_t error = params->error; | |
if (error != BLE_ERROR_NONE) { | |
/* In case of error, forward the error handling to onBleInitError */ | |
onBleInitError(ble, error); | |
return; | |
} | |
/* Ensure that it is the default instance of BLE */ | |
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { | |
return; | |
} | |
/* Set device name characteristic data */ | |
ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME); | |
/* Optional: add callback for disconnection */ | |
ble.gap().onDisconnection(disconnectionCallback); | |
/* Sacrifice 3B of 31B to Advertising Flags */ | |
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); | |
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); | |
/* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ | |
setupApplicationData(1,2); | |
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); | |
/* Optional: Add name to device */ | |
//ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); | |
/* Set advertising interval. Longer interval == longer battery life */ | |
// ble.gap().setAdvertisingInterval(100); /* 100ms */ | |
ble.gap().setAdvertisingInterval(500); /* 100ms */ | |
/* Start advertising */ | |
ble.gap().startAdvertising(); | |
} | |
int mCount=0; | |
// | |
int main(void) | |
{ | |
mPc.baud( 115200 ); | |
mPc.printf("#start-BLE_GAP_Example2 \n"); | |
ticker.attach(periodicCallback, 2); /* trigger sensor polling every 2 seconds */ | |
//mTimer.start(); | |
// BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); | |
BLE &ble = BLE::Instance(); | |
/* Initialize BLE baselayer, always do this first! */ | |
// float sec2= mTimer.read(); | |
// mPc.printf( "sec.bef=%f\n",sec2); | |
ble.init(bleInitComplete); | |
// sec2 = mTimer.read(); | |
// mPc.printf( "sec.aft=%f\n",sec2); | |
while (ble.hasInitialized() == false) { /* spin loop */ } | |
/* Infinite loop waiting for BLE events */ | |
while (true) { | |
if (triggerTempValueUpdate) { | |
setupApplicationData(mCount ,2); | |
mCount +=1; | |
if(mCount >=100 ){mCount =0; } | |
ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); | |
triggerTempValueUpdate = false; | |
}else{ | |
ble.waitForEvent(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment