|
| 1 | +/* |
| 2 | + Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- |
| 17 | + 1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +// This example can work with CallbackLED to show the profile read/write operation in central |
| 21 | +#include <CurieBLE.h> |
| 22 | + |
| 23 | +struct bt_le_conn_param conn_param = {0x18, 0x28, 0, 400}; |
| 24 | + |
| 25 | +const int ledPin = 13; // set ledPin to use on-board LED |
| 26 | +BLECentral bleCentral; // create central instance |
| 27 | +BLEPeripheralHelper *blePeripheral1 = NULL; |
| 28 | + |
| 29 | +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service |
| 30 | +// create switch characteristic and allow remote device to read and write |
| 31 | +BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); |
| 32 | + |
| 33 | + |
| 34 | +void bleCentralConnectHandler(BLEHelper& peripheral) |
| 35 | +{ |
| 36 | + // peripheral connected event handler |
| 37 | + blePeripheral1 = (BLEPeripheralHelper *)(&peripheral); |
| 38 | + Serial.print("Connected event, peripheral: "); |
| 39 | + Serial.println(peripheral.address()); |
| 40 | + // Start discovery the profiles in peripheral device |
| 41 | + blePeripheral1->discover(); |
| 42 | +} |
| 43 | + |
| 44 | +void bleCentralDisconnectHandler(BLEHelper& peripheral) |
| 45 | +{ |
| 46 | + // peripheral disconnected event handler |
| 47 | + blePeripheral1 = NULL; |
| 48 | + Serial.print("Disconnected event, peripheral: "); |
| 49 | + Serial.println(peripheral.address()); |
| 50 | + bleCentral.startScan(); |
| 51 | +} |
| 52 | + |
| 53 | +void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) |
| 54 | +{ |
| 55 | + // Read response/Notification wrote new value to characteristic, update LED |
| 56 | + Serial.print("Characteristic event, written: "); |
| 57 | + |
| 58 | + if (switchChar.value()) |
| 59 | + { |
| 60 | + Serial.println("LED on"); |
| 61 | + digitalWrite(ledPin, HIGH); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + Serial.println("LED off"); |
| 66 | + digitalWrite(ledPin, LOW); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +bool adv_found(uint8_t type, |
| 71 | + const uint8_t *data, |
| 72 | + uint8_t data_len, |
| 73 | + void *user_data) |
| 74 | +{ |
| 75 | + bt_addr_le_t *addr = (bt_addr_le_t *)user_data; |
| 76 | + int i; |
| 77 | + |
| 78 | + pr_debug(LOG_MODULE_APP, "[AD]: %u data_len %u\n", type, data_len); |
| 79 | + |
| 80 | + switch (type) |
| 81 | + { |
| 82 | + case BT_DATA_UUID128_SOME: |
| 83 | + case BT_DATA_UUID128_ALL: |
| 84 | + { |
| 85 | + if (data_len % MAX_UUID_SIZE != 0) |
| 86 | + { |
| 87 | + pr_debug(LOG_MODULE_APP, "AD malformed\n"); |
| 88 | + return true; |
| 89 | + } |
| 90 | + struct bt_uuid * serviceuuid = ledService.uuid(); |
| 91 | + for (i = 0; i < data_len; i += MAX_UUID_SIZE) |
| 92 | + { |
| 93 | + if (memcmp (((struct bt_uuid_128*)serviceuuid)->val, &data[i], MAX_UUID_SIZE) != 0) |
| 94 | + { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // Accept the advertisement |
| 99 | + if (!bleCentral.stopScan()) |
| 100 | + { |
| 101 | + pr_debug(LOG_MODULE_APP, "Stop LE scan failed\n"); |
| 102 | + continue; |
| 103 | + } |
| 104 | + pr_debug(LOG_MODULE_APP, "Connecting\n"); |
| 105 | + // Connect to peripheral |
| 106 | + bleCentral.connect(addr, &conn_param); |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | +void setup() { |
| 116 | + Serial1.begin(115200); |
| 117 | + |
| 118 | + Serial.begin(9600); |
| 119 | + pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output |
| 120 | + |
| 121 | + // add service and characteristic |
| 122 | + bleCentral.addAttribute(ledService); |
| 123 | + bleCentral.addAttribute(switchChar); |
| 124 | + |
| 125 | + // assign event handlers for connected, disconnected to central |
| 126 | + bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); |
| 127 | + bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); |
| 128 | + |
| 129 | + // advertise the service |
| 130 | + bleCentral.setAdvertiseHandler(adv_found); |
| 131 | + |
| 132 | + // assign event handlers for characteristic |
| 133 | + switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); |
| 134 | + |
| 135 | + bleCentral.begin(); |
| 136 | + Serial.println(("Bluetooth device active, waiting for connections...")); |
| 137 | +} |
| 138 | + |
| 139 | +void loop() |
| 140 | +{ |
| 141 | + static unsigned char counter = 0; |
| 142 | + static char ledstate = 0; |
| 143 | + delay(2000); |
| 144 | + if (blePeripheral1) |
| 145 | + { |
| 146 | + counter++; |
| 147 | + |
| 148 | + if (counter % 3) |
| 149 | + { |
| 150 | + switchChar.read(*blePeripheral1); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + ledstate = !ledstate; |
| 155 | + switchChar.write(*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate)); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments