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