4
4
*/
5
5
6
6
/*
7
- * Sketch: BatteryMonitor_Notification .ino
7
+ * Sketch: BatteryMonitor .ino
8
8
*
9
9
* Description:
10
10
* This sketch example partially implements the standard Bluetooth
13
13
* For more information:
14
14
* https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
15
15
*
16
- * Notes:
17
- *
18
- * - Expected to work with BatteryMonitor_Central sketch.
19
- * You can also use an android or IOS app that supports notifications.
20
- *
21
16
*/
22
17
23
18
#include < CurieBLE.h>
24
19
25
20
BLEService batteryService (" 180F" ); // BLE Battery Service
26
21
27
22
// BLE Battery Level Characteristic"
28
- BLEUnsignedCharCharacteristic batteryLevelChar (" 2A19" , BLERead | BLENotify); // standard 16-bit characteristic UUID defined in the URL above
29
- // remote clients will be able to get notifications if this characteristic changes
23
+ BLEUnsignedCharCharacteristic batteryLevelChar (" 2A19" , // standard 16-bit characteristic UUID
24
+ BLERead | BLENotify); // remote clients will be able to
25
+ // get notifications if this characteristic changes
30
26
31
27
int oldBatteryLevel = 0 ; // last battery level reading from analog input
32
28
long previousMillis = 0 ; // last time the battery level was checked, in ms
33
29
34
30
void setup () {
35
- BLE.begin ();
36
31
Serial.begin (9600 ); // initialize serial communication
37
32
pinMode (13 , OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
38
33
34
+ // begin initialization
35
+ BLE.begin ();
36
+
39
37
/* Set a local name for the BLE device
40
38
This name will appear in advertising packets
41
39
and can be used by remote devices to identify this BLE device
42
40
The name can be changed but maybe be truncated based on space left in advertisement packet
43
41
If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
44
42
*/
45
- BLE.setLocalName (" BatteryMonitorSketch" );
46
- BLE.setAdvertisedServiceUuid (batteryService.uuid ()); // add the service UUID
47
- BLE.addService (batteryService); // Add the BLE Battery service
43
+ BLE.setLocalName (" BatteryMonitor" );
44
+ BLE.setAdvertisedService (batteryService); // add the service UUID
48
45
batteryService.addCharacteristic (batteryLevelChar); // add the battery level characteristic
46
+ BLE.addService (batteryService); // Add the BLE Battery service
49
47
batteryLevelChar.setValue (oldBatteryLevel); // initial value for this characteristic
50
48
51
- /* Now activate the BLE device . It will start continuously transmitting BLE
49
+ /* Start advertising BLE. It will start continuously transmitting BLE
52
50
advertising packets and will be visible to remote BLE central devices
53
- until it receives a new connection
54
- */
51
+ until it receives a new connection */
55
52
53
+ // start advertising
56
54
BLE.advertise ();
55
+
57
56
Serial.println (" Bluetooth device active, waiting for connections..." );
58
57
}
59
58
@@ -77,14 +76,6 @@ void loop() {
77
76
if (currentMillis - previousMillis >= 200 ) {
78
77
previousMillis = currentMillis;
79
78
updateBatteryLevel ();
80
-
81
- static unsigned short count = 0 ;
82
- count++;
83
- // update the connection interval
84
- if (count % 5 == 0 ) {
85
- delay (1000 );
86
- updateIntervalParams (central);
87
- }
88
79
}
89
80
}
90
81
// when the central disconnects, turn off the LED:
@@ -104,36 +95,11 @@ void updateBatteryLevel() {
104
95
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
105
96
Serial.print (" Battery Level % is now: " ); // print it
106
97
Serial.println (batteryLevel);
107
- batteryLevelChar.writeUnsignedChar (batteryLevel); // and update the battery level characteristic
98
+ batteryLevelChar.setValue (batteryLevel); // and update the battery level characteristic
108
99
oldBatteryLevel = batteryLevel; // save the level for next comparison
109
100
}
110
101
}
111
102
112
- void updateIntervalParams (BLEDevice central) {
113
- // read and update the connection interval that peer central device
114
- static unsigned short interval = 0x60 ;
115
- ble_conn_param_t m_conn_param;
116
- // Get connection interval that peer central device wanted
117
- // central.getConnParams(m_conn_param);
118
- Serial.print (" min interval = " );
119
- Serial.println (m_conn_param.interval_min );
120
- Serial.print (" max interval = " );
121
- Serial.println (m_conn_param.interval_max );
122
- Serial.print (" latency = " );
123
- Serial.println (m_conn_param.latency );
124
- Serial.print (" timeout = " );
125
- Serial.println (m_conn_param.timeout );
126
-
127
- // Update connection interval
128
- Serial.println (" set Connection Interval" );
129
- central.setConnectionInterval (interval, interval);
130
-
131
- interval++;
132
- if (interval < 0x06 )
133
- interval = 0x06 ;
134
- if (interval > 0x100 )
135
- interval = 0x06 ;
136
- }
137
103
/*
138
104
Copyright (c) 2016 Intel Corporation. All rights reserved.
139
105
0 commit comments