test

Dependencies:   BLE_API mbed nRF51822

main.cpp

Committer:
wenhuanglin
Date:
2016-09-09
Revision:
5:63d48ab9ffc1
Parent:
4:6b09cced6fb9
Parent:
3:f71a35d85001
Child:
6:e287fa7f2e1c

File content as of revision 5:63d48ab9ffc1:

/* 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"

Ticker timer;

DigitalIn myInputPin(P0_10);

BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);

/* Optional: Device Name, add for human read-ability */

const static char     DEVICE_NAME[] = "BLE_BEACON";


/* You have up to 26 bytes of advertising data to use. */
uint8_t AdvData[] = {0x01,0x02,0x00,0x04,0x12};   /* Example of hex data */
//const static uint8_t AdvData[] = {"ChangeThisData"};         /* Example of character data */


class AdvertisementPacket {
public:
    AdvertisementPacket(BLE&);
    void updateAdvertisementPacket(){
        
        AdvData[1] = myInputPin;
        
        ble.gap().clearAdvertisingPayload();
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
        };
private:
    BLE &ble;
};

AdvertisementPacket::AdvertisementPacket(BLE &bleIn): ble(bleIn){
        
};
/*

/* 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);
    
    
     /* Configure advertisements */
    //ble.gap().setTxPower(radioPowerLevels[txPowerMode]);
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
    //ble.gap().setAdvertisingInterval(beaconPeriod);
    //ble.gap().onRadioNotification(this, &EddystoneService::radioNotificationCallback);
    //ble.gap().initRadioNotification();
    

    /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */
    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(1000); /* 100ms */
    
    ble.gap().clearAdvertisingPayload();
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData));
    
    

    /* Start advertising */
    ble.gap().startAdvertising();
}

int main(void)
{
    
    
    
 
    /* Initialize BLE baselayer, always do this first! */
    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 */ }
    
    AdvertisementPacket f(ble);

    timer.attach( &f, &AdvertisementPacket::updateAdvertisementPacket, 1.0); 

    while (true) {
        ble.waitForEvent();
    }
}