Yihui Xiong / Mbed 2 deprecated BLE_Default_APP

Dependencies:   BLE_API mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "BLEDevice.h"
00019 
00020 #include "DFUService.h"
00021 #include "UARTService.h"
00022 #include "DeviceInformationService.h"
00023 
00024 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
00025                                * it will have an impact on code-size and power consumption. */
00026 
00027 #if NEED_CONSOLE_OUTPUT
00028 #define DEBUG(...) { printf(__VA_ARGS__); }
00029 #else
00030 #define DEBUG(...) /* nothing */
00031 #endif /* #if NEED_CONSOLE_OUTPUT */
00032 
00033 BLEDevice ble;
00034 
00035 const char *deviceName = "DefaultApp";
00036 
00037 void periodicCallback(void)
00038 {
00039     DEBUG("ping\r\n");
00040 }
00041 
00042 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00043 {
00044     DEBUG("Disconnected!\n\r");
00045     DEBUG("Restarting the advertising process\n\r");
00046     ble.startAdvertising();
00047 }
00048 
00049 int main(void)
00050 {
00051     Ticker ticker;
00052     ticker.attach(periodicCallback, 1);
00053 
00054     DEBUG("Initialising BTLE transport\n\r");
00055     ble.init();
00056     ble.onDisconnection(disconnectionCallback);
00057 
00058     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00059     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName));
00060 
00061     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */
00062     ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
00063     ble.startAdvertising();
00064 
00065     DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1");
00066 
00067     /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
00068      * control characteristic which can be used to trigger the application to
00069      * handover control to a resident bootloader. */
00070     DFUService dfu(ble);
00071 
00072     /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */
00073     UARTService uartService(ble);
00074     uartService.retargetStdout();
00075 
00076     for (;;) {
00077         ble.waitForEvent();
00078     }
00079 }