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