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