Simple bluetooth low energy service, featuring three characteristics - two sliders and one button

Dependents:   Nucleo_BLE_Demo

Committer:
berlingeradam
Date:
Wed May 27 18:58:49 2015 +0000
Revision:
2:510cac0a0250
Parent:
1:fd7adeffebbe
Copyright update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
berlingeradam 2:510cac0a0250 1 /* Demo BLE service
berlingeradam 2:510cac0a0250 2 * Copyright (c) 2006-2013 ARM Limited
berlingeradam 2:510cac0a0250 3 * Copyright (c) 2015 Adam Berlinger
berlingeradam 2:510cac0a0250 4 *
berlingeradam 2:510cac0a0250 5 * Licensed under the Apache License, Version 2.0 (the "License");
berlingeradam 2:510cac0a0250 6 * you may not use this file except in compliance with the License.
berlingeradam 2:510cac0a0250 7 * You may obtain a copy of the License at
berlingeradam 2:510cac0a0250 8 *
berlingeradam 2:510cac0a0250 9 * http://www.apache.org/licenses/LICENSE-2.0
berlingeradam 2:510cac0a0250 10 *
berlingeradam 2:510cac0a0250 11 * Unless required by applicable law or agreed to in writing, software
berlingeradam 2:510cac0a0250 12 * distributed under the License is distributed on an "AS IS" BASIS,
berlingeradam 2:510cac0a0250 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
berlingeradam 2:510cac0a0250 14 * See the License for the specific language governing permissions and
berlingeradam 2:510cac0a0250 15 * limitations under the License.
berlingeradam 2:510cac0a0250 16 */
berlingeradam 2:510cac0a0250 17
berlingeradam 0:866f2c528c01 18 #include "DemoAppService.h"
berlingeradam 0:866f2c528c01 19
berlingeradam 1:fd7adeffebbe 20
berlingeradam 1:fd7adeffebbe 21 #include "mbed.h"
berlingeradam 1:fd7adeffebbe 22 #include "BLEDevice.h"
berlingeradam 1:fd7adeffebbe 23 #include "DeviceInformationService.h"
berlingeradam 1:fd7adeffebbe 24 #include "UARTService.h"
berlingeradam 1:fd7adeffebbe 25 #include "Utils.h"
berlingeradam 1:fd7adeffebbe 26
berlingeradam 0:866f2c528c01 27 const uint8_t DemoAppService::ServiceUUID[LENGTH_OF_LONG_UUID] = {
berlingeradam 0:866f2c528c01 28 0x6E, 0x40, 0x00, 0x10, 0xB5, 0xA3, 0xF3, 0x93,
berlingeradam 0:866f2c528c01 29 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
berlingeradam 0:866f2c528c01 30 };
berlingeradam 0:866f2c528c01 31
berlingeradam 0:866f2c528c01 32 const uint8_t DemoAppService::buttonCharacteristicUUID[LENGTH_OF_LONG_UUID] = {
berlingeradam 0:866f2c528c01 33 0x6E, 0x40, 0x00, 0x11, 0xB5, 0xA3, 0xF3, 0x93,
berlingeradam 0:866f2c528c01 34 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
berlingeradam 0:866f2c528c01 35 };
berlingeradam 0:866f2c528c01 36
berlingeradam 0:866f2c528c01 37 const uint8_t DemoAppService::slider1CharacteristicUUID[LENGTH_OF_LONG_UUID] = {
berlingeradam 0:866f2c528c01 38 0x6E, 0x40, 0x00, 0x12, 0xB5, 0xA3, 0xF3, 0x93,
berlingeradam 0:866f2c528c01 39 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
berlingeradam 0:866f2c528c01 40 };
berlingeradam 0:866f2c528c01 41
berlingeradam 0:866f2c528c01 42 const uint8_t DemoAppService::slider2CharacteristicUUID[LENGTH_OF_LONG_UUID] = {
berlingeradam 0:866f2c528c01 43 0x6E, 0x40, 0x00, 0x13, 0xB5, 0xA3, 0xF3, 0x93,
berlingeradam 0:866f2c528c01 44 0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
berlingeradam 1:fd7adeffebbe 45 };
berlingeradam 1:fd7adeffebbe 46
berlingeradam 1:fd7adeffebbe 47 static BLEDevice *ble;
berlingeradam 1:fd7adeffebbe 48
berlingeradam 1:fd7adeffebbe 49 /* Callback called when the device is disconnected */
berlingeradam 1:fd7adeffebbe 50 static void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
berlingeradam 1:fd7adeffebbe 51 {
berlingeradam 1:fd7adeffebbe 52 DEBUG("Disconnected!\n\r");
berlingeradam 1:fd7adeffebbe 53 DEBUG("Restarting the advertising process\n\r");
berlingeradam 1:fd7adeffebbe 54
berlingeradam 1:fd7adeffebbe 55 ble->startAdvertising();
berlingeradam 1:fd7adeffebbe 56 //connected = false;
berlingeradam 1:fd7adeffebbe 57 }
berlingeradam 1:fd7adeffebbe 58
berlingeradam 1:fd7adeffebbe 59 /* Callback called when the device is connected */
berlingeradam 1:fd7adeffebbe 60 static void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
berlingeradam 1:fd7adeffebbe 61 {
berlingeradam 1:fd7adeffebbe 62 DEBUG("Connected\r\n");
berlingeradam 1:fd7adeffebbe 63
berlingeradam 1:fd7adeffebbe 64 //connected = true;
berlingeradam 1:fd7adeffebbe 65 }
berlingeradam 1:fd7adeffebbe 66
berlingeradam 1:fd7adeffebbe 67 const static char DEVICE_NAME[] = "BlueNRG_UART";
berlingeradam 1:fd7adeffebbe 68 DemoAppService *startDemoBLE(const char* name){
berlingeradam 1:fd7adeffebbe 69 ble = new BLEDevice();
berlingeradam 1:fd7adeffebbe 70 ble->init();
berlingeradam 1:fd7adeffebbe 71 /* Set callback functions */
berlingeradam 1:fd7adeffebbe 72 ble->onDisconnection(disconnectionCallback);
berlingeradam 1:fd7adeffebbe 73 ble->onConnection(connectionCallback);
berlingeradam 1:fd7adeffebbe 74
berlingeradam 1:fd7adeffebbe 75 DeviceInformationService deviceInfo(*ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
berlingeradam 1:fd7adeffebbe 76 /* setup advertising */
berlingeradam 1:fd7adeffebbe 77 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
berlingeradam 1:fd7adeffebbe 78 ble->setAdvertisingType (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
berlingeradam 1:fd7adeffebbe 79
berlingeradam 1:fd7adeffebbe 80 //ble->accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME , (const uint8_t *)"BlueNRG_UART" , sizeof("BlueNRG_UART") - 1);
berlingeradam 1:fd7adeffebbe 81 //ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
berlingeradam 1:fd7adeffebbe 82 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)name , strlen(name)+1);
berlingeradam 1:fd7adeffebbe 83
berlingeradam 1:fd7adeffebbe 84 /* Start advertising */
berlingeradam 1:fd7adeffebbe 85 ble->setAdvertisingInterval(160);
berlingeradam 1:fd7adeffebbe 86 ble->startAdvertising();
berlingeradam 1:fd7adeffebbe 87
berlingeradam 1:fd7adeffebbe 88 return new DemoAppService(*ble);
berlingeradam 1:fd7adeffebbe 89 }