Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
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 #include "RelayService.h" 00020 00021 BLEDevice ble; 00022 00023 DigitalOut relay(p5); 00024 00025 const static char DEVICE_NAME[] = "RELAY"; 00026 static const uint16_t uuid16_list[] = {RelayService::RELAY_SERVICE_UUID}; 00027 00028 RelayService *relayServicePtr; 00029 00030 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00031 { 00032 ble.startAdvertising(); 00033 } 00034 00035 void onDataWrittenCallback(const GattCharacteristicWriteCBParams *params) 00036 { 00037 if ((params->charHandle == relayServicePtr->getValueHandle()) && (params->len == 1)) { 00038 relay = *(params->data); 00039 } 00040 } 00041 00042 int main(void) 00043 { 00044 relay = 0; 00045 00046 ble.init(); 00047 ble.onDisconnection(disconnectionCallback); 00048 ble.onDataWritten(onDataWrittenCallback); 00049 00050 bool initialValueForRelayCharacteristic = false; 00051 RelayService relayService(ble, initialValueForRelayCharacteristic); 00052 relayServicePtr = &relayService; 00053 00054 /* setup advertising */ 00055 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00056 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00057 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00058 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00059 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); 00060 ble.startAdvertising(); 00061 00062 while (true) { 00063 ble.waitForEvent(); 00064 } 00065 }
Generated on Sat Jul 16 2022 00:09:01 by
1.7.2