|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 3 | + * See the bottom of this file for the license terms. |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * Sketch: CentralDouble.ino. |
| 8 | + * |
| 9 | + * Description: |
| 10 | + * This is a simple BLE sketch that initiates the |
| 11 | + * Arduino platform as a Central. It reads a double value from |
| 12 | + * a connected Peripheral. The sketch exercises: Scanning for |
| 13 | + * a specific Peripheral, connecting to it, discover its Attributes, |
| 14 | + * and exercise a specific Characteristic to read a double value |
| 15 | + * from the Peripheral. |
| 16 | + * |
| 17 | + * Notes: |
| 18 | + * Expected Peripheral name: DataTest |
| 19 | + * Expected Peripheral Characteristic: 19b20001e8f2537e4f6cd104768a1214 |
| 20 | + * Expected Characteristic read value: double. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "CurieBLE.h" |
| 24 | + |
| 25 | + |
| 26 | +// LED pin |
| 27 | +#define LED_PIN 13 |
| 28 | + |
| 29 | +void setup() { |
| 30 | + Serial.begin(9600); |
| 31 | + |
| 32 | + // set LED pin to output mode |
| 33 | + pinMode(LED_PIN, OUTPUT); |
| 34 | + |
| 35 | + // begin initialization |
| 36 | + BLE.begin(); |
| 37 | + Serial.println(BLE.address()); |
| 38 | + |
| 39 | + BLE.scanForName("DataTest"); |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + BLEDevice peripheral = BLE.available(); |
| 44 | + if (peripheral) |
| 45 | + { |
| 46 | + Serial.println(peripheral.address()); |
| 47 | + BLE.stopScan(); |
| 48 | + // central connected to peripheral |
| 49 | + controlLogic(peripheral); |
| 50 | + BLE.scanForName("DataTest"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +void controlLogic(BLEDevice &peripheral) |
| 56 | +{ |
| 57 | + // connect to the peripheral |
| 58 | + Serial.print("Connecting ... "); |
| 59 | + Serial.println(peripheral.address()); |
| 60 | + |
| 61 | + if (peripheral.connect()) |
| 62 | + { |
| 63 | + Serial.print("Connected: "); |
| 64 | + Serial.println(peripheral.address()); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + Serial.println("Failed to connect!"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (peripheral.discoverAttributes() == false) |
| 73 | + { |
| 74 | + Serial.println("Discover failed, Disconnecting..."); |
| 75 | + peripheral.disconnect(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + BLECharacteristic doubleCharacteristic = peripheral.characteristic("19b20001e8f2537e4f6cd104768a1214"); |
| 80 | + |
| 81 | + if (!doubleCharacteristic) |
| 82 | + { |
| 83 | + peripheral.disconnect(); |
| 84 | + Serial.println("Peripheral does not have test double characteristic!"); |
| 85 | + delay(5000); |
| 86 | + return; |
| 87 | + } |
| 88 | + doubleCharacteristic.subscribe(); |
| 89 | + |
| 90 | + while (peripheral.connected()) |
| 91 | + { |
| 92 | + doubleCharacteristic.read(); |
| 93 | + delay(1000); |
| 94 | + if (doubleCharacteristic.valueUpdated()) |
| 95 | + { |
| 96 | + Serial.print("Double characteristic value: "); |
| 97 | + Serial.println(doubleCharacteristic.doubleValue()); |
| 98 | + } |
| 99 | + delay(1000); |
| 100 | + } |
| 101 | + Serial.print("Disconnected"); |
| 102 | + Serial.println(peripheral.address()); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +/* |
| 108 | + Arduino BLE Peripheral Double read/write test example |
| 109 | + Copyright (c) 2016 Arduino LLC. All right reserved. |
| 110 | +
|
| 111 | + This library is free software; you can redistribute it and/or |
| 112 | + modify it under the terms of the GNU Lesser General Public |
| 113 | + License as published by the Free Software Foundation; either |
| 114 | + version 2.1 of the License, or (at your option) any later version. |
| 115 | +
|
| 116 | + This library is distributed in the hope that it will be useful, |
| 117 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 118 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 119 | + Lesser General Public License for more details. |
| 120 | +
|
| 121 | + You should have received a copy of the GNU Lesser General Public |
| 122 | + License along with this library; if not, write to the Free Software |
| 123 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 124 | +*/ |
| 125 | + |
| 126 | + |
0 commit comments