|
| 1 | +/* Please see code cpyright at the bottom of this example code */ |
| 2 | +/* |
| 3 | + This sketch illustrates how to change the advertising data so that it is visible but not |
| 4 | + connectable. Then after 10 seconds it changes to being connectable |
| 5 | + This sketch example partially implements the standard Bluetooth Low-Energy Battery service. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <CurieBLE.h> |
| 9 | + |
| 10 | +BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) |
| 11 | +BLEService batteryService("180F"); // BLE Battery Service |
| 12 | +int count = 0; |
| 13 | +// BLE Battery Level Characteristic" |
| 14 | +BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID |
| 15 | + BLERead | BLENotify); // remote clients will be able to |
| 16 | +// get notifications if this characteristic changes |
| 17 | + |
| 18 | +void setup() { |
| 19 | + Serial.begin(9600); // initialize serial communication |
| 20 | + pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected |
| 21 | + while (!Serial) { |
| 22 | + //wait for Serial to connect |
| 23 | + } |
| 24 | + /* Set a local name for the BLE device |
| 25 | + This name will appear in advertising packets |
| 26 | + and can be used by remote devices to identify this BLE device |
| 27 | + The name can be changed but maybe be truncated based on space left in advertisement packet */ |
| 28 | + blePeripheral.setLocalName("BatteryAdvChangeSketch"); |
| 29 | + blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID |
| 30 | + blePeripheral.addAttribute(batteryService); // Add the BLE Battery service |
| 31 | + blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic |
| 32 | + |
| 33 | + /* Now activate the BLE device. It will start continuously transmitting BLE |
| 34 | + advertising packets and will be visible to remote BLE central devices |
| 35 | + until it receives a new connection */ |
| 36 | + |
| 37 | + blePeripheral.begin(); |
| 38 | + Serial.println("Bluetooth device active, waiting for connections..."); |
| 39 | + Serial.println("Starts in Connectable mode"); |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + // listen for BLE peripherals to connect: |
| 44 | + BLECentralHelper central = blePeripheral.central(); |
| 45 | + // wait |
| 46 | + Serial.print(". "); |
| 47 | + if (count == 10) { |
| 48 | + Serial.print("\nReached count "); |
| 49 | + Serial.println(count); |
| 50 | + |
| 51 | + } |
| 52 | + delay (1000); |
| 53 | + count++; |
| 54 | + // Switch from Connectable to Non Connectable and vice versa |
| 55 | + if (count > 10 ) { |
| 56 | + static bool change_discover = false; |
| 57 | + Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible"); |
| 58 | + // Some central devices (phones included) may cache previous scan inofrmation |
| 59 | + // restart your central and it should not see this peripheral once stopAdvertising() is called |
| 60 | + blePeripheral.stopAdvertising(); |
| 61 | + delay(10000); |
| 62 | + |
| 63 | + if (change_discover) |
| 64 | + { |
| 65 | + |
| 66 | + // Using the function setAdvertisingParam we specify that it now NOT connectable |
| 67 | + // The loop is for 10 seconds. Your central device may timeout later than that |
| 68 | + // and may eventually connect when we set it back to connectable mode below |
| 69 | + blePeripheral.setConnectable(false); |
| 70 | + Serial.println("In Non Connectable mode"); |
| 71 | + |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + |
| 76 | + //using the function setAdvertisingParam we specify that it now connectable |
| 77 | + blePeripheral.setConnectable(true); |
| 78 | + Serial.println("In Connectable mode"); |
| 79 | + } |
| 80 | + Serial.println("Start Adv"); |
| 81 | + blePeripheral.startAdvertising(); |
| 82 | + if (change_discover) { |
| 83 | + Serial.println("Adding 5 second delay in Non Connect Mode"); |
| 84 | + delay(5000); |
| 85 | + } |
| 86 | + change_discover = !change_discover; |
| 87 | + count = 0; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/* |
| 92 | + Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 93 | +
|
| 94 | + This library is free software; you can redistribute it and/or |
| 95 | + modify it under the terms of the GNU Lesser General Public |
| 96 | + License as published by the Free Software Foundation; either |
| 97 | + version 2.1 of the License, or (at your option) any later version. |
| 98 | +
|
| 99 | + This library is distributed in the hope that it will be useful, |
| 100 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 101 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 102 | + Lesser General Public License for more details. |
| 103 | +
|
| 104 | + You should have received a copy of the GNU Lesser General Public |
| 105 | + License along with this library; if not, write to the Free Software |
| 106 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 107 | +*/ |
| 108 | + |
0 commit comments