No changes
Dependencies: BLE_API mbed nRF51822
Fork of SDP_Version3_Abdul by
BLE_Init.h@4:caab577334f0, 2017-02-26 (annotated)
- Committer:
- galism
- Date:
- Sun Feb 26 02:33:32 2017 +0000
- Revision:
- 4:caab577334f0
No changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
galism | 4:caab577334f0 | 1 | #include "mbed.h" |
galism | 4:caab577334f0 | 2 | #include "ble/BLE.h" |
galism | 4:caab577334f0 | 3 | #include "MMA8452Q.h" |
galism | 4:caab577334f0 | 4 | #include "AcclerationService.h" |
galism | 4:caab577334f0 | 5 | #include "ReedSwitchService.h" |
galism | 4:caab577334f0 | 6 | |
galism | 4:caab577334f0 | 7 | |
galism | 4:caab577334f0 | 8 | const static char DEVICE_NAME[] = "BLE_Bike"; //Name of BLE Device |
galism | 4:caab577334f0 | 9 | static const uint16_t uuid16_list[] = {ReedSwitchService::REED_SWITCH_SERVICE_UUID, //UUID's of Services |
galism | 4:caab577334f0 | 10 | AccelerationService::ACCELERATION_SERVICE_UUID}; |
galism | 4:caab577334f0 | 11 | |
galism | 4:caab577334f0 | 12 | static ReedSwitchService *reedSwitchServicePtr; |
galism | 4:caab577334f0 | 13 | static AccelerationService *accelerationServicePtr; |
galism | 4:caab577334f0 | 14 | |
galism | 4:caab577334f0 | 15 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
galism | 4:caab577334f0 | 16 | { |
galism | 4:caab577334f0 | 17 | BLE::Instance().gap().startAdvertising(); |
galism | 4:caab577334f0 | 18 | } |
galism | 4:caab577334f0 | 19 | |
galism | 4:caab577334f0 | 20 | /** |
galism | 4:caab577334f0 | 21 | * This function is called when the ble initialization process has failed |
galism | 4:caab577334f0 | 22 | */ |
galism | 4:caab577334f0 | 23 | void onBleInitError(BLE &ble, ble_error_t error) |
galism | 4:caab577334f0 | 24 | { |
galism | 4:caab577334f0 | 25 | } |
galism | 4:caab577334f0 | 26 | |
galism | 4:caab577334f0 | 27 | /** |
galism | 4:caab577334f0 | 28 | * Callback triggered when the ble initialization process has finished |
galism | 4:caab577334f0 | 29 | */ |
galism | 4:caab577334f0 | 30 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
galism | 4:caab577334f0 | 31 | { |
galism | 4:caab577334f0 | 32 | BLE& ble = params->ble; |
galism | 4:caab577334f0 | 33 | ble_error_t error = params->error; |
galism | 4:caab577334f0 | 34 | |
galism | 4:caab577334f0 | 35 | if (error != BLE_ERROR_NONE) { |
galism | 4:caab577334f0 | 36 | /* In case of error, forward the error handling to onBleInitError */ |
galism | 4:caab577334f0 | 37 | onBleInitError(ble, error); |
galism | 4:caab577334f0 | 38 | return; |
galism | 4:caab577334f0 | 39 | } |
galism | 4:caab577334f0 | 40 | |
galism | 4:caab577334f0 | 41 | /* Ensure that it is the default instance of BLE */ |
galism | 4:caab577334f0 | 42 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
galism | 4:caab577334f0 | 43 | return; |
galism | 4:caab577334f0 | 44 | } |
galism | 4:caab577334f0 | 45 | |
galism | 4:caab577334f0 | 46 | ble.gap().onDisconnection(disconnectionCallback); |
galism | 4:caab577334f0 | 47 | |
galism | 4:caab577334f0 | 48 | /* Setup primary service */ |
galism | 4:caab577334f0 | 49 | reedSwitchServicePtr = new ReedSwitchService(ble, false /* initial value for button pressed */); |
galism | 4:caab577334f0 | 50 | accelerationServicePtr = new AccelerationService(ble); |
galism | 4:caab577334f0 | 51 | |
galism | 4:caab577334f0 | 52 | /* setup advertising */ |
galism | 4:caab577334f0 | 53 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
galism | 4:caab577334f0 | 54 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
galism | 4:caab577334f0 | 55 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
galism | 4:caab577334f0 | 56 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
galism | 4:caab577334f0 | 57 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
galism | 4:caab577334f0 | 58 | ble.gap().startAdvertising(); |
galism | 4:caab577334f0 | 59 | |
galism | 4:caab577334f0 | 60 | } |