44*/
55
66/*
7- * Sketch: BatteryMonitor_Notification .ino
7+ * Sketch: BatteryMonitor .ino
88 *
99 * Description:
1010 * This sketch example partially implements the standard Bluetooth
1313 * For more information:
1414 * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
1515 *
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- *
2116 */
2217
2318#include < CurieBLE.h>
2419
2520BLEService batteryService (" 180F" ); // BLE Battery Service
2621
2722// 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
3026
3127int oldBatteryLevel = 0 ; // last battery level reading from analog input
3228long previousMillis = 0 ; // last time the battery level was checked, in ms
3329
3430void setup () {
35- BLE.begin ();
3631 Serial.begin (9600 ); // initialize serial communication
3732 pinMode (13 , OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
3833
34+ // begin initialization
35+ BLE.begin ();
36+
3937 /* Set a local name for the BLE device
4038 This name will appear in advertising packets
4139 and can be used by remote devices to identify this BLE device
4240 The name can be changed but maybe be truncated based on space left in advertisement packet
4341 If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
4442 */
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
4845 batteryService.addCharacteristic (batteryLevelChar); // add the battery level characteristic
46+ BLE.addService (batteryService); // Add the BLE Battery service
4947 batteryLevelChar.setValue (oldBatteryLevel); // initial value for this characteristic
5048
51- /* Now activate the BLE device . It will start continuously transmitting BLE
49+ /* Start advertising BLE. It will start continuously transmitting BLE
5250 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 */
5552
53+ // start advertising
5654 BLE.advertise ();
55+
5756 Serial.println (" Bluetooth device active, waiting for connections..." );
5857}
5958
@@ -77,14 +76,6 @@ void loop() {
7776 if (currentMillis - previousMillis >= 200 ) {
7877 previousMillis = currentMillis;
7978 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- }
8879 }
8980 }
9081 // when the central disconnects, turn off the LED:
@@ -104,36 +95,11 @@ void updateBatteryLevel() {
10495 if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
10596 Serial.print (" Battery Level % is now: " ); // print it
10697 Serial.println (batteryLevel);
107- batteryLevelChar.writeUnsignedChar (batteryLevel); // and update the battery level characteristic
98+ batteryLevelChar.setValue (batteryLevel); // and update the battery level characteristic
10899 oldBatteryLevel = batteryLevel; // save the level for next comparison
109100 }
110101}
111102
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- }
137103/*
138104 Copyright (c) 2016 Intel Corporation. All rights reserved.
139105
0 commit comments