This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path$autorun$.iBeacon.sb
1485 lines (1285 loc) · 72.5 KB
/
$autorun$.iBeacon.sb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filename ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// $autorun$.iBeacon.sb
//
//
//Version of this app in Device Info Service
#define APP_VERSION "2.1.0"
//**************************************************************************************
//**** Uncomment the first #define line for firmware older than 1.3.57.19 and older ****
//**************************************************************************************
//#define HANDLERCHARVAL HndlrCharVal(ByVal charHndl) //for <= v1.3.57.19
#define HANDLERCHARVAL HndlrCharVal(ByVal charHndl, ByVal offset, ByVal len)
//
// ###################################################################################
// # Version history in reverse chronological order (Customer change history) #
// ###################################################################################
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 05/12/13 -> 2.1.0 #
// # ----------------------------------------------------------------------------- #
// # #
// ###################################################################################
//
//
// ###################################################################################
// # #
// # See file $autorun$.iBeacon.sblib for usage instructions #
// # #
// ###################################################################################
//
//
//******************************************************************************
//******************************************************************************
// Definitions that customers use to customise the source
//******************************************************************************
//Default Uuid for beacon
#define BEACON_UUID "\e2\c5\6d\b5\df\fb\48\d2\b0\60\d0\f5\a7\10\96\e0"
//Default Major value for beacon (16 bit number)
#define BEACON_MAJOR 0x1234
//Default Minor value for beacon (16 bit number)
#define BEACON_MINOR 0x5678
//Default TxPower, one of (4,0,-4,-8,-12,-16,-20,-30,-55)
#define BEACON_TXPOWER_DBM 4
//RSSI at 1m from BL600 when tx power = 4
//Set this by advertising at 4 dBm and measuring the RSSI 1m from the BL600
#define CALIBRTD_VALUE_DBM -60
//Default value for iBeacon format
// --> 0 : Full AD
// --> 1 : UUID + Major + Minor
// --> 2 : UUID
// --> Default : 0
#define BEACON_FORMAT 0
//Default time to remain connectible : 1-255
#define REMAIN_CONN_TME_SEC 20
//Min time to remain connectible when updated over the air : 3-255
#define REMAIN_CONN_TME_SEC_MIN 3
//Default advertising interval -- higher the number, lower the current consumption
#define ADV_INTERVAL_MS 200 //should be in range 100..5000
//Advertise time out (0==forever)
#define ADV_TIMEOUT_MINUTES 0
//How often to update the battery level (minutes)
#define BATT_LEVEL_POLL_MIN 10
//Threshold Voltage in mv at which GetBattLevel() will return a battery level of 100%
#define BATT_100_PERC_THRESH_MV 3000
//Threshold Voltage in mv at which GetBattLevel() will return a battery level of 0%
#define BATT_0_PERC_THRESH_MV 1800
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// The following information is for the GAP service (UUID = 0x1800)
// Feel free to modify to suite your need
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//See BT Spec for other assigned values
#define APPEARANCE 512 //Generic Tag
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// The following information is for the DEVICE INFO service (UUID = 0x180A)
// Feel free to modify to suite your need
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define MANF_NAME "Laird Technologies"
#define MODELSTR "BL600"
#define SERIALNUM "12345"
#define HWREV "1.0"
//******************************************************************************
// Library Import
//******************************************************************************
// -- lib\iBeacon.sblib -- START
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ Customers should not make modifications to this file. If this is ++
// +++++ essential, it is recommended that the modified version of this file ++
// +++++ is submitted to Laird, so that your changes are tracked. ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// $autorun$.iBeacon.sblib (MUST be d in a top level .sb file)
//
//
// ###################################################################################
// # Version history in reverse chronological order #
// ###################################################################################
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 15/10/14 -> 2.2.0 #
// # ----------------------------------------------------------------------------- #
// # - Added battery service. Battery level is by default updated every 10 minutes #
// # -> See #define on line 75 of $autorun$.iBeacon.sb #
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 06/12/13 -> 2.1.0 #
// # ----------------------------------------------------------------------------- #
// # - Refactored so that it is split in two files #
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 05/12/13 -> 2.1.0 #
// # ----------------------------------------------------------------------------- #
// # - Refactored so that using #define HANDLERCHARVAL this app will work in all #
// # firmware versions #
// # - Changed default transmit power to +4dBm #
// # - removed SWREV #defined and using APP_VERSION instead #
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 03/12/13 -> 2.0.0 #
// # ----------------------------------------------------------------------------- #
// # - Removed BleAdvertStop() from HndlrBleAdvTmt() - redundant #
// # - Validating advIntvl in HndlrCharVal() now instead of in StartAdvertising() #
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 02/12/13 -> 2.0.0 #
// # ----------------------------------------------------------------------------- #
// # - Added ability to change device name via extra char and save it to memory #
// # --- Changed char UUIDs to accommodate new char #
// # --- Removed global variable 'dn$' #
// # --- GAP Device Name will mirror custom Device Name char #
// # #
// # - Rewrote HndlrCharVal() with a case statement #
// # #
// # ----------------------------------------------------------------------------- #
// # ::Changelog:: 29/11/13 #
// # ----------------------------------------------------------------------------- #
// # - Added UUIDs to description below #
// # - Changed HWREV define to "1.0" #
// # - Changed Major and Minor defines #
// # - Added units to defines for char values & indexes #
// # - Changed description of AssertRC (result code and line number) #
// # - Added new chars to comments on charHndles[] #
// # - Removed UUID + Tx Power from possible formats in CreateIBeaconAD$() #
// # - Changed NewChar() - Max data length is now derived in sub #
// # - Forcibly writing leftmost 20 chars of device name to memory #
// # - Changed name of Calibration Value char to Calibrated RSSI Value #
// # - Changed adv interval, time out, remain conn time to 1 byte #
// # - Changed TxPower char for adopted Tx Power Level char #
// # - StartAdvertising() - Convert Tmt, took out redundant if statement #
// # - Added empty if statement "if ble connected do nothing" for BleAdvertStart #
// # - Added TxPower validation #
// # - Now setting advTmt with remConnTime and using timeout event instead of timer #
// # - Removed need for Button. RemConnTime=0 is now invalid #
// # - Added description of characteristics, how to use and UUIDs (see below) #
// # - Added the APP_VERSION #define to track version #
// # - Added BleSvcRegDevInfo() to register device info service with GATT server #
// # #
// # #
// ###################################################################################
//
//
// #######################################################################################
// # #
// # This sample app turns the BL600 into an iBeacon by creating and advertising with #
// # an Advertising Record (AD) that can be used by a receiving device for the intended #
// # use case. #
// # #
// # The receiver can connect to the BL600 to change the iBeacon data and some of the #
// # advertising parameters via the characteristics described below. #
// # #
// # #
// # --------------------------------------------------------------------------------- #
// # ::Characteristics:: #
// # --------------------------------------------------------------------------------- #
// # #
// # [Device Name]: 569a2010-b87f-490c-92cb-11ba5ea5167c #
// # - BL600's Device name as seen by other devices #
// # #
// # [AD Format]: 569a2011-b87f-490c-92cb-11ba5ea5167c #
// # - Specifies the format of the Advertising Record (AD) #
// # - Length: 1 byte #
// # --> 0 : Full AD #
// # --> 1 : UUID + Major + Minor #
// # --> 2 : UUID #
// # --> Default : 0 #
// # #
// # [iBeacon UUID]: 569a2012-b87f-490c-92cb-11ba5ea5167c #
// # - Uniquely defines iBeacon type or company #
// # - Length: 16 bytes #
// # #
// # [Major Value]: 569a2013-b87f-490c-92cb-11ba5ea5167c #
// # - Differentiates iBeacons with the same UUID #
// # - Length: 2 bytes #
// # #
// # [Minor Value]: 569a2014-b87f-490c-92cb-11ba5ea5167c #
// # - Differentiates iBeacons with the same UUID and major value #
// # - Length: 2 bytes #
// # #
// # [Tx Power]: #
// # - Transmitting Power - Adopted Characteristic #
// # - Length: 1 byte #
// # - Unit: dBm (signed 8 bit) #
// # - Valid values: -55, -30, -20, -16, -12, -8, -4, 0, 4 #
// # hex values- C9 E2 EC F0 F4 F8 FC 0 4 #
// # #
// # [Advertising Interval]: 569a2015-b87f-490c-92cb-11ba5ea5167c #
// # - Time between two advertisement events #
// # - Length: 1 byte (unsigned 8 bit) #
// # - Unit: 20 ms (e.g. 50 will give 1000ms) #
// # - Note: Advertising Interval shall be no lower than 100 ms #
// # #
// # [Advertising Time out]: 569a2016-b87f-490c-92cb-11ba5ea5167c #
// # - Time in minutes after which module stops advertising, 0 == forever #
// # - Length: 1 byte (unsigned 8 bit) #
// # - Unit: minutes (e.g. 10 will give 600000ms) #
// # #
// # [Calibrated RSSI Value]: 569a2017-b87f-490c-92cb-11ba5ea5167c #
// # - RSSI measured 1m from BL600 when Tx Power = 4 dBm #
// # - Length: 1 byte (signed 8 bit) #
// # - Unit: dBm (eg -128 = 0x80 -60==0xC4 -1==0xFF ) #
// # #
// # [Remain Connectible Time]: 569a2018-b87f-490c-92cb-11ba5ea5167c #
// # - Time to advertise with type 0 (connectible) #
// # - Length: 1 byte (signed 8 bit) #
// # - Unit: seconds (e.g. 10 will give 10000ms) #
// # - Valid values: 1-255 #
// # #
// # #
// # #
// # --------------------------------------------------------------------------------- #
// # ::UUIDs:: #
// # Format: 569aABCD-b87f-490c-92cb-11ba5ea5167c #
// # Where 0xABCD is a 16 bit offset from the Laird Base UUID #
// # --------------------------------------------------------------------------------- #
// # - Laird Base: 569a0000-b87f-490c-92cb-11ba5ea5167c #
// # - iBeacon Service: 569a1900-b87f-490c-92cb-11ba5ea5167c #
// # - Device Name Char: 569a2010-b87f-490c-92cb-11ba5ea5167c #
// # - Format Char: 569a2011-b87f-490c-92cb-11ba5ea5167c #
// # - iBeacon UUID Char: 569a2012-b87f-490c-92cb-11ba5ea5167c #
// # - Major Char: 569a2013-b87f-490c-92cb-11ba5ea5167c #
// # - Minor Char: 569a2014-b87f-490c-92cb-11ba5ea5167c #
// # - Advertising Interval Char: 569a2015-b87f-490c-92cb-11ba5ea5167c #
// # - Advertising Time out Char: 569a2016-b87f-490c-92cb-11ba5ea5167c #
// # - Calibrated RSSI Value Char: 569a2017-b87f-490c-92cb-11ba5ea5167c #
// # - Remain Connectible Time Char: 569a2018-b87f-490c-92cb-11ba5ea5167c #
// # #
// # :Adopted Characteristics: (16 bit value as offset to Bluetooth SIG Base UUID) #
// # - Tx Power Level: 0x2A07 #
// # #
// # #
// # #
// # -------------------------------------------------------------------------------- #
// # ::How to use:: #
// # -------------------------------------------------------------------------------- #
// # #
// # [Change iBeacon Data & Connection Parameters] #
// # #
// # ::Android:: #
// # With the app "nRF Master Control Panel", you can view the AD by tapping "iBeacon" #
// # when it appears in the device list. You connect to the BL600 to change #
// # characteristic values by tapping the "Connect" button. (See Examples below) #
// # #
// # ::iOS:: #
// # With the app "LightBlue", you can connect to the BL600 to change characteristic #
// # values by tapping "iBeacon" when it appears under "Scanning for Peripherals...". #
// # (See Examples below) #
// # #
// # #
// # [Test iBeacon Functionality] #
// # #
// # When advertising, the app "iBeacon Locate" on Android and its iOS equivalent, #
// # "Locate for iBeacon" will identify the BL600 as an iBeacon and display it's UUID, #
// # major and minor values, the RSSI and an approximate distance from the iBeacon. #
// # #
// # Tapping on the device under "Visible iBeacons" will allow you to calibrate it for #
// # this app to give you a more accurate reading of the distance. #
// # #
// # It is recommended that you calibrate the BL600 as well by setting the TxPower #
// # characteristic value to 4 dBm and measuring the RSSI on another device that is #
// # 1 metre away. Update the Calibrated RSSI Value char with this measured RSSI #
// # #
// # ::Note:: - This Android/iOS app recognises iBeacons with any one of the following #
// # Apple defined UUIDs: #
// # #
// # e2c56db5-dffb-48d2-b060-d0f5a71096e0 #
// # 5a4bcfce-174e-4bac-a814-092e77f6b7e5 #
// # 74278bda-b644-4520-8f0c-720eaf059935 #
// # #
// # - The custom Device Name characteristic must be used to change the #
// # device name and NOT the one in the GAP service #
// # #
// # #
// # [Examples] #
// # --- To change the Major value to 1551 (0x060F): #
// # 1. Connect to iBeacon #
// # 2. Select the iBeacon service (569a1900-b87f-490c-92cb-11ba5ea5167c) #
// # 3. Select the Major characteristic (569a2011b87f-490c-92cb-11ba5ea5167c) #
// # 4. Write HEX value 0x060F #
// # 5. Disconnect from the iBeacon #
// # 6. AD will now contain new Major value at position 10 #
// # #
// # --- To set the transmitting power to -50 dBm (0xFFFFFFCE in two's complement) #
// # 1. Connect to iBeacon #
// # 2. Select the iBeacon service (569a1900-b87f-490c-92cb-11ba5ea5167c) #
// # 3. Select the Tx Power characteristic (569a2013-b87f-490c-92cb-11ba5ea5167c) #
// # 4. Write HEX value 0xCE #
// # 5. Disconnect from the iBeacon #
// # 6. BL600 will now advertise with Tx power of -50 #
// # #
// # #
// #######################################################################################
//
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
//Default device name to advertise with which can be changed via
//characteristic 569a2010-b87f-490c-92cb-11ba5ea5167c
#define DEVICENAME "Laird iBeacon"
//Laird Technologies 128 bit Base UUID
#define LT_BASE_UUID "\56\9a\00\00\b8\7f\49\0c\92\cb\11\ba\5e\a5\16\7c\"
//Service 16 bit UUID
#define IBEACON_SVC_UUID 0x1900
//Company ID - currently Apple Inc
#define COMPANY_ID 0x004C
//AD Data Type 2 = iBeacon
#define BEACON_DATA_TYPE 2
#define ADV_MIN_INTERVAL_MS 100 //should NOT be < 100
#define ADV_INTERVAL_SCALER 20
//Array index for device name
#define DVC_NME_INDEX 0
//Array index for format
#define FORMAT_INDEX 1
//Array index for UUID value
#define UUID_INDEX 2
//Array index for Major value
#define MAJOR_INDEX 3
//Array index for Minor value
#define MINOR_INDEX 4
//Array index for TxPower value
#define TXPOWER_INDEX 5
//Array index for Interval (supplied in 20ms units)
#define ADV_INTVL_INDEX 6
//Array index for Time out (supplied as minutes)
#define ADV_TMT_INDEX 7
//Array index for Calibration Value
#define CAL_VAL_INDEX 8
//Array index for Remain Connectible Time (supplied in seconds)
#define REM_CONN_TME_INDEX 9
#define MAX_DEVNAME_CHRS_IN_ADVERT 0
//DiscoverableMode (2==General)
#define DISCOVERY_MODE 2
//Minimum acceptable connection interval (0.5 seconds)
#define MIN_CONN_INTERVAL 100000
//Maximum acceptable connection interval (1 second).
#define MAX_CONN_INTERVAL 200000
//Slave latency -- number of conn events that can be missed
//Effective Conn interval will be (SLAVE_LATENCY+1)*ACTUAL_CONN_INTERVAL
#define SLAVE_LATENCY 0
//Connection supervisory timeout (4 seconds) - max 32 seconds
#define CONN_SUP_TIMEOUT 4000000
//EvBleMsg ID for a BLE disconnection event
#define BLE_DISCONNECTED 1
//EvBleMsg ID when device name is written to
#define DEVICENAME_WRITTEN 21
//Record number for device name
#define DEVICENAME_REC_NUM 20
//Number of characteristics
#define NUM_OF_CHARS 10
//Min/max device names allowed to arrive via characteristic DVC_NME_INDEX
#define MIN_DEVICENAME_LEN 4
#define MAX_DEVICENAME_LEN 20
//Ble is connected error
#define BLE_IS_CONNECTED 0x604C
//******************************************************************************
// Library Import
//******************************************************************************
// -- lib\ble.sblib -- START
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ###### #######
// ###### Library File meant to be d in files #######
// ###### #######
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This library contains all the BLE related #defines that will make your app
// more readable
//
//******************************************************************************
//******************************************************************************
// Library Import
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#define BLE_CONN_HANDLE_INVALID 0xFFFF
#define BLE_INVALID_UUID_HANDLE 0
#define BLE_INVALID_CHAR_HANDLE 0
#define BLE_INVALID_SERVICE_HANDLE 0
#define CIRCBUFFER_INVALID_HANDLE -1
//Set BLE_DISCOVERABILITY_NONE if whitelist is enabled in BleAdvertStart()
//as the 4.0 spec says if whitelist is enabled then discoverability is not allowed
#define BLE_DISCOVERABILITY_NONE 0
#define BLE_DISCOVERABILITY_LIMITED 1
#define BLE_DISCOVERABILITY_GENERAL 2
#define BLE_DISCOVERABILITY_BOTH 3
#define BLE_APPEARANCE_UNKNOWN 0
#define BLE_APPEARANCE_GENERIC_PHONE 64
#define BLE_APPEARANCE_GENERIC_COMPUTER 128
#define BLE_APPEARANCE_GENERIC_WATCH 192
#define BLE_APPEARANCE_WATCH_SPORTS_WATCH 193
#define BLE_APPEARANCE_GENERIC_CLOCK 256
#define BLE_APPEARANCE_GENERIC_DISPLAY 320
#define BLE_APPEARANCE_GENERIC_REMOTE_CONTROL 384
#define BLE_APPEARANCE_GENERIC_EYE_GLASSES 448
#define BLE_APPEARANCE_GENERIC_TAG 512
#define BLE_APPEARANCE_GENERIC_KEYRING 576
#define BLE_APPEARANCE_GENERIC_MEDIA_PLAYER 640
#define BLE_APPEARANCE_GENERIC_BARCODE_SCANNER 704
#define BLE_APPEARANCE_GENERIC_THERMOMETER 768
#define BLE_APPEARANCE_THERMOMETER_EAR 769
#define BLE_APPEARANCE_GENERIC_HEART_RATE_SENSOR 832
#define BLE_APPEARANCE_HEART_RATE_SENSOR_HEART_RATE_BELT 833
#define BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE 896
#define BLE_APPEARANCE_BLOOD_PRESSURE_ARM 897
#define BLE_APPEARANCE_BLOOD_PRESSURE_WRIST 898
#define BLE_APPEARANCE_HUMAN_INTERFACE_DEVICE_HID_GENERIC 960
#define BLE_APPEARANCE_KEYBOARD_HID_SUBTYPE 961
#define BLE_APPEARANCE_MOUSE_HID_SUBTYPE 962
#define BLE_APPEARANCE_JOYSTICK_HID_SUBTYPE 963
#define BLE_APPEARANCE_GAMEPAD_HID_SUBTYPE 964
#define BLE_APPEARANCE_DIGITIZER_TABLET_HID_SUBTYPE 965
#define BLE_APPEARANCE_CARD_READER_HID_SUBTYPE 966
#define BLE_APPEARANCE_DIGITAL_PEN_HID_SUBTYPE 967
#define BLE_APPEARANCE_BARCODE_SCANNER_HID_SUBTYPE 968
#define BLE_APPEARANCE_GENERIC_GLUCOSE_METER 1024
#define UUID_ALERT_NOTIFICATION_SERVICE 0x1811
#define UUID_BATTERY_SERVICE 0x180F
#define UUID_BLOOD_PRESSURE_SERVICE 0x1810
#define UUID_CURRENT_TIME_SERVICE 0x1805
#define UUID_DEVICE_INFORMATION_SERVICE 0x180A
#define UUID_GLUCOSE_SERVICE 0x1808
#define UUID_HEALTH_THERMOMETER_SERVICE 0x1809
#define UUID_HEART_RATE_SERVICE 0x180D
#define UUID_HUMAN_INTERFACE_DEVICE_SERVICE 0x1812
#define UUID_IMMEDIATE_ALERT_SERVICE 0x1802
#define UUID_LINK_LOSS_SERVICE 0x1803
#define UUID_NEXT_DST_CHANGE_SERVICE 0x1807
#define UUID_PHONE_ALERT_STATUS_SERVICE 0x180E
#define UUID_REFERENCE_TIME_UPDATE_SERVICE 0x1806
#define UUID_SCAN_PARAMETERS_SERVICE 0x1813
#define UUID_TX_POWER_SERVICE 0x1804
#define BLE_SERVICE_SECONDARY 0
#define BLE_SERVICE_PRIMARY 1
#define BLE_CHAR_METADATA_ATTR_NOT_PRESENT 0
#define BLE_ATTR_ACCESS_NONE 0
#define BLE_ATTR_ACCESS_OPEN 1
#define BLE_ATTR_ACCESS_ENC_NO_MITM 2
#define BLE_ATTR_ACCESS_ENC_WITH_MITM 3
#define BLE_ATTR_ACCESS_SIGNED_NO_MITM 4
#define BLE_ATTR_ACCESS_SIGNED_WITH_MITM 5
#define BLE_CHAR_PROPERTIES_BROADCAST 0x01
#define BLE_CHAR_PROPERTIES_READ 0x02
#define BLE_CHAR_PROPERTIES_WRITE_WO_RESPONSE 0x04
#define BLE_CHAR_PROPERTIES_WRITE 0x08
#define BLE_CHAR_PROPERTIES_NOTIFY 0x10
#define BLE_CHAR_PROPERTIES_INDICATE 0x20
#define BLE_CHAR_PROPERTIES_AUTH_SIGNED_WR 0x40
#define BLE_CHAR_PROPERTIES_RELIABLE_WRITE 0x80
//Body Sensor Location values
#define BLE_HRS_BODY_SENSOR_LOCATION_OTHER 0
#define BLE_HRS_BODY_SENSOR_LOCATION_CHEST 1
#define BLE_HRS_BODY_SENSOR_LOCATION_WRIST 2
#define BLE_HRS_BODY_SENSOR_LOCATION_FINGER 3
#define BLE_HRS_BODY_SENSOR_LOCATION_HAND 4
#define BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE 5
#define BLE_HRS_BODY_SENSOR_LOCATION_FOOT 6
//Types of advert packets
#define ADV_IND 0
#define ADV_DIRECT_IND 1
#define ADV_SCAN_IND 2
#define ADV_NONCONN_IND 3
//Types of filter policy
#define ADV_FILTERPOLICY_ANY 0
#define ADV_FILTERPOLICY_SCANREQ 1
#define ADV_FILTERPOLICY_CONNREQ 2
#define ADV_FILTERPOLICY_BOTH 3
//BLE EVENT MSG IDs
#define BLE_EVBLEMSGID_CONNECT 0 //msgCtx = connection handle
#define BLE_EVBLEMSGID_DISCONNECT 1 //msgCtx = connection handle
#define BLE_EVBLEMSGID_IMMEDIATE_ALERT_SERVICE_ALERT 2 //msgCtx = alert level
#define BLE_EVBLEMSGID_LINKLOSS_SERVICE_ALERT 3 //msgCtx = alert level
#define BLE_EVBLEMSGID_SERVICE_ERROR 4 //msgCtx = resultcode
#define BLE_EVBLEMSGID_HTS_INDICATION_STATE 5 //msgCtx = new indication state 0=off, 1=on
#define BLE_EVBLEMSGID_HTS_INDICATION_CNF 6 //msgCtx = not used
#define BLE_EVBLEMSGID_BPS_INDICATION_STATE 7 //msgCtx = new indication state 0=off, 1=on
#define BLE_EVBLEMSGID_BPS_INDICATION_CNF 8 //msgCtx = not used
#define BLE_EVBLEMSGID_DISPLAY_PASSKEY 9 //msgCtx = passcode to display
#define BLE_EVBLEMSGID_NEW_BOND 10 //msgCtx = connection handle
#define BLE_EVBLEMSGID_AUTH_KEY_REQUEST 11 //msgCtx = 1 for passkey, 2 for 16 byte OOB data
#define BLE_EVBLEMSGID_HRS_NOTIFICATION_STATE 12 //msgCtx = new notification state 0=off, 1=on
//#define FUTURE_USE 13
#define BLE_EVBLEMSGID_CONN_PARMS_UPDATE 14 //msgCtx = connection handle
#define BLE_EVBLEMSGID_CONN_PARMS_UPDATE_FAIL 15 //msgCtx = connection handle
#define BLE_EVBLEMSGID_CONN_TO_BONDED_MASTER 16 //msgCtx = connection handle
#define BLE_EVBLEMSGID_UPDATED_BOND 17 //msgCtx = connection handle
#define BLE_EVBLEMSGID_ENCRYPTED 18 //msgCtx = connection handle
#define BLE_EVBLEMSGID_POWER_FAILURE_WARNING 19 //msgCtx = current supply voltage
#define BLE_EVBLEMSGID_UNENCRYPTED 20 //msgCtx = connection handle
#define BLE_EVBLEMSGID_DEVICENAME_WRITE 21 //msgCtx = connection handle
//BLE Alert Levels
#define BLE_ALERT_LEVEL_NO_ALERT 0
#define BLE_ALERT_LEVEL_MILD_ALERT 1
#define BLE_ALERT_LEVEL_HIGH_ALERT 2
//Blood Pressure Feature Bits
#define BLE_BPS_FEATURE_BODY_MOVEMENT_BIT 1
#define BLE_BPS_FEATURE_CUFF_FIT_BIT 2
#define BLE_BPS_FEATURE_IRREGULAR_PULSE_BIT 4
#define BLE_BPS_FEATURE_PULSE_RATE_RANGE_BIT 8
#define BLE_BPS_FEATURE_MEASUREMENT_POSITION_BIT 16
#define BLE_BPS_FEATURE_MULTIPLE_BOND_BIT 32
//Blood Pressure Measurement Status flags
#define BLE_BPS_MEAS_STAT_BODY_MOVEMENT 1
#define BLE_BPS_MEAS_STAT_CUFF_FIT 2
#define BLE_BPS_MEAS_STAT_IRREGULAR_PULSE 4
#define BLE_BPS_MEAS_STAT_PULSE_RATE_RANGE 8
#define BLE_BPS_MEAS_STAT_MEASUREMENT_POSITION 16
//Pairing i/o capability
#define BLE_IOCAPABILITY_NONE 0
#define BLE_IOCAPABILITY_DISPLAY_YESNO 1
#define BLE_IOCAPABILITY_KEYBOARD_ONLY 2
#define BLE_IOCAPABILITY_DISPLAY_ONLY 3
#define BLE_IOCAPABILITY_KEYBOARD_DISPLAY 4
#define BLE_IOCAPABILITY_ILLEGAL 5
//Health thermometer sensor location
#define BLE_HTS_TEMP_TYPE_ARMPIT 1
#define BLE_HTS_TEMP_TYPE_BODY 2
#define BLE_HTS_TEMP_TYPE_EAR 3
#define BLE_HTS_TEMP_TYPE_FINGER 4
#define BLE_HTS_TEMP_TYPE_GI_TRACT 5
#define BLE_HTS_TEMP_TYPE_MOUTH 6
#define BLE_HTS_TEMP_TYPE_RECTUM 7
#define BLE_HTS_TEMP_TYPE_TOE 8
#define BLE_HTS_TEMP_TYPE_EAR_DRUM 9
//HCI Status Codes
#define BLE_HCI_AUTHENTICATION_FAILURE 0x05
#define BLE_HCI_CONNECTION_TIMEOUT 0x08
//Some Useful Result Codes to help with documentation
#define RESCODE_BLE_SVC_REGISTERED 0x6018
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//******************************************************************************
// Handler definitions
//******************************************************************************
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
// -- lib\ble.sblib -- END
//------------------------------------------------------------------------------
// For debugging
// --- rc = result code
// --- ln = line number
//------------------------------------------------------------------------------
Sub AssertResCode(rc,ln)
if rc!=0 then
print "\nFail :";integer.h' rc;" at tag ";ln
endif
EndSub
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
DIM rc //Result code
DIM advRpt$ //Advert report
DIM scnRpt$ //Scan report
DIM dta$[NUM_OF_CHARS] //Array of characteristic value attributes
DIM hSvc //Service handler
DIM dtaLen[NUM_OF_CHARS] //Array of valid data lengths for each char
DIM charHndles[NUM_OF_CHARS] //Array of characteristic handles
// 0 : Device Name
// 1 : Format
// 2 : iBeacon UUID
// 3 : iBeacon Major
// 4 : iBeacon Minor
// 5 : iBeacon TxPower (Adopted Char from Bluetooth SIG)
// 6 : Advertising Interval
// 7 : Advert Time out
// 8 : Calibrated RSSI Value
// 9 : Remain Connectible Time
//---------------------------------------
DIM newCfg //Will be non-zero if characteristics were written to
//while in a connection
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
newCfg = 0
dtaLen[DVC_NME_INDEX]=20
dtaLen[FORMAT_INDEX]=1
dtaLen[UUID_INDEX]=16
dtaLen[MAJOR_INDEX]=2
dtaLen[MINOR_INDEX]=2
dtaLen[TXPOWER_INDEX]=1
dtaLen[ADV_INTVL_INDEX]=1
dtaLen[ADV_TMT_INDEX]=1
dtaLen[CAL_VAL_INDEX]=1
dtaLen[REM_CONN_TME_INDEX]=1
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//------------------------------------------------------------------------------
// Get index of the given handle in the 'charHndles[]' array
//------------------------------------------------------------------------------
Function GetCharIndex(ByVal cHndl)
dim i
for i=0 to NUM_OF_CHARS-1
if charHndles[i]==cHndl then
ExitFunc i //Return the index of given handle
endif
next
EndFunc -1 //At this point, whole array has been searched.
//-1 indicates that handle was not found
//------------------------------------------------------------------------------
// Validate TxPower characteristic value
//------------------------------------------------------------------------------
Function ValidateTxPwr(tp$)
//Array of valid tx power values
dim txp[9],tp
txp[0]=4 : txp[1]=0 : txp[2]=-4 : txp[3]=-8 : txp[4]=-12: txp[5]=-16: txp[6]=-20: txp[7]=-30: txp[8]=-55
//Convert tx power to integer
rc=BleDecodeS8(tp$,tp,0)
dim i
for i = 0 to 8
//Take given TxPower and return the nearest valid TxPower value
if tp >=txp[i] then
ExitFunc txp[i]
endif
next
Endfunc txp[8] //Just return minimum power
//------------------------------------------------------------------------------
// Swap the order of the 2 lower bytes of the argument
//------------------------------------------------------------------------------
Function SwapBytes(v)
Endfunc ((v>>8)&0xFF) + ((v<<8)&0xFF00)
//------------------------------------------------------------------------------
// Initialises dta$[] with default data (See Definitions)
//------------------------------------------------------------------------------
Sub InitGattCharData()
dim i
//Convert default values to string and place in array 'dta$[]'
dta$[DVC_NME_INDEX]=DEVICENAME
rc=BleEncode8(dta$[FORMAT_INDEX],BEACON_FORMAT,0)
AssertResCode(rc,411)
dta$[UUID_INDEX]=BEACON_UUID //Already a string so no need to convert
rc=BleEncode16(dta$[MAJOR_INDEX],SwapBytes(BEACON_MAJOR),0)
AssertResCode(rc,414)
rc=BleEncode16(dta$[MINOR_INDEX],SwapBytes(BEACON_MINOR),0)
AssertResCode(rc,416)
rc=BleEncode8(dta$[TXPOWER_INDEX],BEACON_TXPOWER_DBM,0)
AssertResCode(rc,418)
//Ensure advertising interval is NOT < the minimum
i = (ADV_INTERVAL_MS/ADV_INTERVAL_SCALER)
if i < (ADV_MIN_INTERVAL_MS/ADV_INTERVAL_SCALER) then
i = (ADV_MIN_INTERVAL_MS/ADV_INTERVAL_SCALER)
endif
rc=BleEncode8(dta$[ADV_INTVL_INDEX],i,0)
AssertResCode(rc,425)
rc=BleEncode8(dta$[ADV_TMT_INDEX],ADV_TIMEOUT_MINUTES,0)
AssertResCode(rc,427)
rc=BleEncode8(dta$[CAL_VAL_INDEX],CALIBRTD_VALUE_DBM,0)
AssertResCode(rc,429)
rc=BleEncode8(dta$[REM_CONN_TME_INDEX],REMAIN_CONN_TME_SEC,0)
AssertResCode(rc,431)
EndSub
//------------------------------------------------------------------------------
// Load saved data from non volatile memory
//------------------------------------------------------------------------------
Sub LoadSavedData()
dim i
for i=1 to NUM_OF_CHARS
//Load from selected record in memory into array 'dta$[]'
//If nothing is read, the default value (see InitGattCharData()) is used
rc=NvRecordGetEx(i,dta$[i-1],dta$[i-1])
next
EndSub
//------------------------------------------------------------------------------
// Write new characteristic value attributes to non-volatile memory
//------------------------------------------------------------------------------
Sub SaveNewData()
dim i
for i=1 to NUM_OF_CHARS
//Write selected element in array 'dta$[]' to record in memory
rc=NvRecordSet(i,dta$[i-1])
next
EndSub
//------------------------------------------------------------------------------
// Create manufacturer specific iBeacon AD element - param in BleAdvRptAppendAD
//------------------------------------------------------------------------------
Function CreateIBeaconAD$()
//--- Format:
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
// CompanyID
// DataType
// Length
// UUID
// Major
// Minor
// TxPower
// 4C00 02 15 E2C56DB5DFFB48D2B060D0F5A71096E0 0000 0000 C5
// ---
dim mad$ //Octet string for the AD (advertising record)
rc=BleEncode16(mad$,COMPANY_ID,0) //Write company id to string
AssertResCode(rc,478)
rc=BleEncode8(mad$,BEACON_DATA_TYPE,2) //Write data type to string
AssertResCode(rc,480)
//Convert Calibrated RSSI Value to integer
dim calVal : rc=BleDecodeS8(dta$[CAL_VAL_INDEX],calVal,0)
//Convert Tx Power to integer
dim txPwr : rc=BleDecodeS8(dta$[TXPOWER_INDEX],txPwr,0)
//Calculate Measured RSSI (appears at end of the AD)
dim mesTxPwr : mesTxPwr = calVal - (4 - txPwr)
//Write Measured RSSI to string
dim mesTxPwr$ : rc=BleEncode8(mesTxPwr$,mesTxPwr,0)
AssertResCode(rc,491)
//Put together AD depending on selected format
dim len$ //Number of bytes to follow in AD
dim bcnFrmt //To select AD format
rc=BleDecodeU8(dta$[FORMAT_INDEX],bcnFrmt,0) //Convert format char value to integer
select bcnFrmt
case 1
rc=BleEncode8(len$,0x14,0) //Length = 20 bytes
AssertResCode(rc,501)
//UUID - Major - Minor]
mad$=mad$+len$+dta$[UUID_INDEX]+dta$[MAJOR_INDEX]+dta$[MINOR_INDEX]
case 2
rc=BleEncode8(len$,0x10,0) //Length = 16 bytes
AssertResCode(rc,506)
//UUID
mad$=mad$+len$+dta$[UUID_INDEX]
case else
rc=BleEncode8(len$,0x15,0) //Length = 21 bytes
AssertResCode(rc,511)
//Full AD
mad$=mad$+len$+dta$[UUID_INDEX]+dta$[MAJOR_INDEX]+dta$[MINOR_INDEX]+mesTxPwr$
endselect
EndFunc mad$
//------------------------------------------------------------------------------
// Calculates and returns the current battery level from the power supply voltage
//------------------------------------------------------------------------------
Function GetBattLevel()
dim mv : mv = READPWRSUPPLYMV() //Power supply in mv
dim perc //Battery level in %
if(mv >= BATT_100_PERC_THRESH_MV) then
perc = 100
elseif (mv <= BATT_0_PERC_THRESH_MV) then
perc = 0
else
perc = ((mv * 833) - 1500000) / 10000
endif
Endfunc perc
//------------------------------------------------------------------------------
// Used by custom.battery.service.sblib to add battery service uuid to scan report
//------------------------------------------------------------------------------
sub AddUuid(uuid)
rc=BleAdvRptAddUuid16(scnRpt$,UUID_BATTERY_SERVICE,-1,-1,-1,-1,-1)
AssertResCode(rc,557)
endsub
//------------------------------------------------------------------------------
// Library Import
//------------------------------------------------------------------------------
// -- lib\custom.battery.service.sblib -- START
// Copyright (c) 2013, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ###### #######
// ###### Library File meant to be d in files #######
// ###### #######
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// This library contains contains the battery service manager and should be
// d in your app
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
//******************************************************************************
// Library Import
//******************************************************************************
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim shBat as integer
dim chBatlvl as integer
dim batlvl$ as string
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
shBat = 0
chBatlvl = 0
batlvl$ ="\64"
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//==============================================================================
//==============================================================================
function AddCharBatLevel(byval btlvl as integer, byval fNfy as integer) as integer
dim rc
if btLvl < 0 then
btLvl = 0
elseif btLvl > 100 then
btLvl = 100
endif
//----------------------------------------------------------------------------
// Create the Battery Level Characteristic which has a UUID of 0x2A19
//----------------------------------------------------------------------------
dim mdAttr
dim mdCccd
dim mdSccd
dim chProp
dim format : format = 0x04 //uint8 - Unsigned 8-bit integer
dim exponent : exponent = 0
dim unit : unit = 0x27AD //Percentage
dim namespace : namespace = 0x01 //Bluetooth SIG
dim namespacedesc : namespacedesc = 0