11/*
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+ */
55
66#include < CurieBLE.h>
77
88/*
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
1511*/
1612
17-
18-
13+ /* */
1914BLEService batteryService (" 180F" ); // BLE Battery Service
2015
2116// 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
2420
2521int oldBatteryLevel = 0 ; // last battery level reading from analog input
2622long previousMillis = 0 ; // last time the battery level was checked, in ms
2723
2824void setup () {
29- BLE.begin ();
3025 Serial.begin (9600 ); // initialize serial communication
3126 pinMode (13 , OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
3227
28+ // begin initialization
29+ BLE.begin ();
30+
3331 /* Set a local name for the BLE device
3432 This name will appear in advertising packets
3533 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
4138 BLE.addService (batteryService); // Add the BLE Battery service
42- batteryService.addCharacteristic (batteryLevelChar); // add the battery level characteristic
4339 batteryLevelChar.setValue (oldBatteryLevel); // initial value for this characteristic
4440
45- /* Now activate the BLE device . It will start continuously transmitting BLE
41+ /* Start advertising BLE. It will start continuously transmitting BLE
4642 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
5045 BLE.advertise ();
46+
5147 Serial.println (" Bluetooth device active, waiting for connections..." );
5248}
5349
@@ -71,14 +67,6 @@ void loop() {
7167 if (currentMillis - previousMillis >= 200 ) {
7268 previousMillis = currentMillis;
7369 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- }
8270 }
8371 }
8472 // when the central disconnects, turn off the LED:
@@ -98,36 +86,11 @@ void updateBatteryLevel() {
9886 if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
9987 Serial.print (" Battery Level % is now: " ); // print it
10088 Serial.println (batteryLevel);
101- batteryLevelChar.writeUnsignedChar (batteryLevel); // and update the battery level characteristic
89+ batteryLevelChar.setValue (batteryLevel); // and update the battery level characteristic
10290 oldBatteryLevel = batteryLevel; // save the level for next comparison
10391 }
10492}
10593
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- }
13194/*
13295 Copyright (c) 2016 Intel Corporation. All rights reserved.
13396
0 commit comments