A template for applications where some small amount of data needs to be notified to a phone app over BLE. It is a good starting point for notifications.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
main.cpp@5:43a3ab27f2e4, 2015-06-20 (annotated)
- Committer:
- rgrover1
- Date:
- Sat Jun 20 00:00:32 2015 +0000
- Revision:
- 5:43a3ab27f2e4
- Parent:
- 2:84dd6d29bc59
- Child:
- 6:18d8750f39ee
updating the underlying libraries.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:28f095301cb2 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:28f095301cb2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:28f095301cb2 | 3 | * |
rgrover1 | 0:28f095301cb2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:28f095301cb2 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:28f095301cb2 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:28f095301cb2 | 7 | * |
rgrover1 | 0:28f095301cb2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:28f095301cb2 | 9 | * |
rgrover1 | 0:28f095301cb2 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:28f095301cb2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:28f095301cb2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:28f095301cb2 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:28f095301cb2 | 14 | * limitations under the License. |
rgrover1 | 0:28f095301cb2 | 15 | */ |
rgrover1 | 0:28f095301cb2 | 16 | |
rgrover1 | 0:28f095301cb2 | 17 | #include "mbed.h" |
rgrover1 | 5:43a3ab27f2e4 | 18 | #include "BLE.h" |
rgrover1 | 0:28f095301cb2 | 19 | #include "ButtonService.h" |
rgrover1 | 0:28f095301cb2 | 20 | |
rgrover1 | 0:28f095301cb2 | 21 | BLEDevice ble; |
rgrover1 | 0:28f095301cb2 | 22 | DigitalOut led1(LED1); |
rgrover1 | 0:28f095301cb2 | 23 | InterruptIn button(BUTTON1); |
rgrover1 | 0:28f095301cb2 | 24 | |
rgrover1 | 0:28f095301cb2 | 25 | const static char DEVICE_NAME[] = "Button"; |
rgrover1 | 0:28f095301cb2 | 26 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; |
rgrover1 | 0:28f095301cb2 | 27 | |
rgrover1 | 0:28f095301cb2 | 28 | ButtonService *buttonServicePtr; |
rgrover1 | 0:28f095301cb2 | 29 | |
rgrover1 | 0:28f095301cb2 | 30 | void buttonPressedCallback(void) |
rgrover1 | 0:28f095301cb2 | 31 | { |
rgrover1 | 0:28f095301cb2 | 32 | buttonServicePtr->updateButtonState(true); |
rgrover1 | 0:28f095301cb2 | 33 | } |
rgrover1 | 0:28f095301cb2 | 34 | |
rgrover1 | 0:28f095301cb2 | 35 | void buttonReleasedCallback(void) |
rgrover1 | 0:28f095301cb2 | 36 | { |
rgrover1 | 0:28f095301cb2 | 37 | buttonServicePtr->updateButtonState(false); |
rgrover1 | 0:28f095301cb2 | 38 | } |
rgrover1 | 0:28f095301cb2 | 39 | |
rgrover1 | 0:28f095301cb2 | 40 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
rgrover1 | 0:28f095301cb2 | 41 | { |
rgrover1 | 0:28f095301cb2 | 42 | ble.startAdvertising(); |
rgrover1 | 0:28f095301cb2 | 43 | } |
rgrover1 | 0:28f095301cb2 | 44 | |
rgrover1 | 0:28f095301cb2 | 45 | void periodicCallback(void) |
rgrover1 | 0:28f095301cb2 | 46 | { |
rgrover1 | 0:28f095301cb2 | 47 | led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
rgrover1 | 0:28f095301cb2 | 48 | } |
rgrover1 | 0:28f095301cb2 | 49 | |
rgrover1 | 0:28f095301cb2 | 50 | int main(void) |
rgrover1 | 0:28f095301cb2 | 51 | { |
rgrover1 | 0:28f095301cb2 | 52 | led1 = 1; |
rgrover1 | 0:28f095301cb2 | 53 | Ticker ticker; |
rgrover1 | 0:28f095301cb2 | 54 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:28f095301cb2 | 55 | button.fall(buttonPressedCallback); |
rgrover1 | 0:28f095301cb2 | 56 | button.rise(buttonReleasedCallback); |
rgrover1 | 0:28f095301cb2 | 57 | |
rgrover1 | 0:28f095301cb2 | 58 | ble.init(); |
rgrover1 | 0:28f095301cb2 | 59 | ble.onDisconnection(disconnectionCallback); |
rgrover1 | 0:28f095301cb2 | 60 | |
rgrover1 | 0:28f095301cb2 | 61 | ButtonService buttonService(ble, false /* initial value for button pressed */); |
rgrover1 | 0:28f095301cb2 | 62 | buttonServicePtr = &buttonService; |
rgrover1 | 0:28f095301cb2 | 63 | |
rgrover1 | 0:28f095301cb2 | 64 | /* setup advertising */ |
rgrover1 | 0:28f095301cb2 | 65 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 0:28f095301cb2 | 66 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 0:28f095301cb2 | 67 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 0:28f095301cb2 | 68 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 2:84dd6d29bc59 | 69 | ble.setAdvertisingInterval(1000); /* 1000ms. */ |
rgrover1 | 0:28f095301cb2 | 70 | ble.startAdvertising(); |
rgrover1 | 0:28f095301cb2 | 71 | |
rgrover1 | 0:28f095301cb2 | 72 | while (true) { |
rgrover1 | 0:28f095301cb2 | 73 | ble.waitForEvent(); |
rgrover1 | 0:28f095301cb2 | 74 | } |
rgrover1 | 0:28f095301cb2 | 75 | } |