|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 3 | + * See the bottom of this file for the license terms. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <CurieBLE.h> |
| 7 | + |
| 8 | +/* |
| 9 | + This example can work with BatteryMonitor |
| 10 | + to show how to control and response the connection intelval request. |
| 11 | +*/ |
| 12 | + |
| 13 | +// set up connection params |
| 14 | + |
| 15 | +ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 |
| 16 | + 50.0, // maximum interval in ms 7.5 - |
| 17 | + 0, // latency |
| 18 | + 4000 // timeout in ms 100 - 32000ms |
| 19 | + }; |
| 20 | + |
| 21 | +const int ledPin = 13; // set ledPin to use on-board LED |
| 22 | +BLECentral bleCentral; // create central instance |
| 23 | +BLEPeripheralHelper *blePeripheral1 = NULL; |
| 24 | + |
| 25 | +BLEService batteryService("180F"); // create service with a 16-bit UUID |
| 26 | +BLECharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);// create switch characteristic |
| 27 | +//and allow remote device to read and notify |
| 28 | + |
| 29 | +bool adv_found(uint8_t type, |
| 30 | + const uint8_t *data, |
| 31 | + uint8_t data_len, |
| 32 | + void *user_data); |
| 33 | + |
| 34 | +void setup() |
| 35 | +{ |
| 36 | + Serial.begin(9600); |
| 37 | + |
| 38 | + // add service and characteristic |
| 39 | + bleCentral.addAttribute(batteryService); |
| 40 | + bleCentral.addAttribute(batteryLevelChar); |
| 41 | + |
| 42 | + // assign event handlers for connected, disconnected to central |
| 43 | + bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); |
| 44 | + bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); |
| 45 | + bleCentral.setEventHandler(BLEUpdateParam, bleCentralUpdateParam); |
| 46 | + |
| 47 | + // advertise the service |
| 48 | + bleCentral.setAdvertiseHandler(adv_found); |
| 49 | + |
| 50 | + // assign event handlers for characteristic |
| 51 | + batteryLevelChar.setEventHandler(BLEWritten, switchCharacteristicWritten); |
| 52 | + |
| 53 | + bleCentral.begin(); |
| 54 | + Serial.println(("Bluetooth device active, waiting for connections...")); |
| 55 | +} |
| 56 | + |
| 57 | +void loop() |
| 58 | +{ |
| 59 | + static unsigned int counter = 0; |
| 60 | + static char ledstate = 0; |
| 61 | + delay(2000); |
| 62 | + if (blePeripheral1) |
| 63 | + { |
| 64 | + counter++; |
| 65 | + |
| 66 | + if (counter % 3) |
| 67 | + { |
| 68 | + batteryLevelChar.read(*blePeripheral1); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + ledstate = !ledstate; |
| 73 | + batteryLevelChar.write(*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +void bleCentralConnectHandler(BLEHelper& peripheral) |
| 80 | +{ |
| 81 | + // peripheral connected event handler |
| 82 | + blePeripheral1 = (BLEPeripheralHelper *)(&peripheral); |
| 83 | + Serial.print("Connected event, peripheral: "); |
| 84 | + Serial.println(peripheral.address()); |
| 85 | + // Start discovery the profiles in peripheral device |
| 86 | + blePeripheral1->discover(); |
| 87 | +} |
| 88 | + |
| 89 | +void bleCentralDisconnectHandler(BLEHelper& peripheral) |
| 90 | +{ |
| 91 | + // peripheral disconnected event handler |
| 92 | + blePeripheral1 = NULL; |
| 93 | + Serial.print("Disconnected event, peripheral: "); |
| 94 | + Serial.println(peripheral.address()); |
| 95 | + bleCentral.startScan(); |
| 96 | +} |
| 97 | + |
| 98 | +void bleCentralUpdateParam(BLEHelper& peripheral) |
| 99 | +{ |
| 100 | + // peripheral update the connection interval event handler |
| 101 | + Serial.print("UpdateParam event, peripheral: "); |
| 102 | + blePeripheral1 = (BLEPeripheralHelper *)(&peripheral); |
| 103 | + Serial.println(peripheral.address()); |
| 104 | + |
| 105 | + // Get connection interval that peer peripheral device wanted |
| 106 | + ble_conn_param_t m_conn_param; |
| 107 | + blePeripheral1->getConnParams(m_conn_param); |
| 108 | + Serial.print("min interval = " ); |
| 109 | + Serial.println(m_conn_param.interval_min ); |
| 110 | + Serial.print("max interval = " ); |
| 111 | + Serial.println(m_conn_param.interval_max ); |
| 112 | + Serial.print("latency = " ); |
| 113 | + Serial.println(m_conn_param.latency ); |
| 114 | + Serial.print("timeout = " ); |
| 115 | + Serial.println(m_conn_param.timeout ); |
| 116 | + |
| 117 | + //Update the connection interval |
| 118 | + blePeripheral1->setConnectionInterval(m_conn_param.interval_min,m_conn_param.interval_max); |
| 119 | +} |
| 120 | + |
| 121 | +void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) |
| 122 | +{ |
| 123 | + // Read response/Notification wrote new value to characteristic, update LED |
| 124 | + Serial.print("Characteristic event, notify: "); |
| 125 | + |
| 126 | + int battery = batteryLevelChar.value(); |
| 127 | + if (battery) |
| 128 | + { |
| 129 | + Serial.print("Battery Level % is now: "); // print it |
| 130 | + Serial.println(battery); |
| 131 | + delay(100); |
| 132 | + |
| 133 | + Serial.println("LED on"); |
| 134 | + digitalWrite(ledPin, HIGH); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + Serial.println("LED off"); |
| 139 | + digitalWrite(ledPin, LOW); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +bool adv_found(uint8_t type, |
| 144 | + const uint8_t *data, |
| 145 | + uint8_t data_len, |
| 146 | + void *user_data) |
| 147 | +{ |
| 148 | + bt_addr_le_t *addr = (bt_addr_le_t *)user_data; |
| 149 | + int i; |
| 150 | + |
| 151 | + Serial.print("[AD]:"); |
| 152 | + Serial.print(type); |
| 153 | + Serial.print(" data_len "); |
| 154 | + Serial.println(data_len); |
| 155 | + |
| 156 | + switch (type) |
| 157 | + { |
| 158 | + case BT_DATA_UUID16_SOME: |
| 159 | + case BT_DATA_UUID16_ALL: |
| 160 | + { |
| 161 | + if (data_len % MAX_UUID_SIZE_16 != 0) |
| 162 | + { |
| 163 | + Serial.println("AD malformed"); |
| 164 | + return true; |
| 165 | + } |
| 166 | + struct bt_uuid * serviceuuid = batteryService.uuid(); |
| 167 | + for (i = 0; i < data_len; i += MAX_UUID_SIZE_16) |
| 168 | + { |
| 169 | + if (memcmp (&((bt_uuid_16_t*)serviceuuid)->val, &data[i], MAX_UUID_SIZE_16) != 0) |
| 170 | + { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + // Accept the advertisement |
| 175 | + if (!bleCentral.stopScan()) |
| 176 | + { |
| 177 | + Serial.println("Stop LE scan failed"); |
| 178 | + continue; |
| 179 | + } |
| 180 | + Serial.println("Connecting"); |
| 181 | + // Connect to peripheral |
| 182 | + bleCentral.connect(addr, &conn_param); |
| 183 | + return false; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return true; |
| 189 | +} |
| 190 | + |
| 191 | +/* |
| 192 | + Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 193 | +
|
| 194 | + This library is free software; you can redistribute it and/or |
| 195 | + modify it under the terms of the GNU Lesser General Public |
| 196 | + License as published by the Free Software Foundation; either |
| 197 | + version 2.1 of the License, or (at your option) any later version. |
| 198 | +
|
| 199 | + This library is distributed in the hope that it will be useful, |
| 200 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 201 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 202 | + Lesser General Public License for more details. |
| 203 | +
|
| 204 | + You should have received a copy of the GNU Lesser General Public |
| 205 | + License along with this library; if not, write to the Free Software |
| 206 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 207 | +*/ |
0 commit comments