@@ -12,6 +12,9 @@ exports.defineAutoTests = function () {
12
12
} ;
13
13
14
14
exports . defineManualTests = function ( contentEl , createActionButton ) {
15
+ let scanned = [ ] ;
16
+ let connectedDevice ;
17
+
15
18
createActionButton ( 'Is Bluetooth Enabled?' , function ( ) {
16
19
ble . isEnabled (
17
20
function ( ) {
@@ -43,11 +46,13 @@ exports.defineManualTests = function (contentEl, createActionButton) {
43
46
}
44
47
45
48
createActionButton ( 'Scan' , function ( ) {
49
+ scanned = [ ] ;
46
50
var scanSeconds = 5 ;
47
51
console . log ( 'Scanning for BLE peripherals for ' + scanSeconds + ' seconds.' ) ;
48
52
ble . startScan (
49
53
[ ] ,
50
54
function ( device ) {
55
+ scanned . push ( device ) ;
51
56
console . log ( JSON . stringify ( device ) ) ;
52
57
} ,
53
58
function ( reason ) {
@@ -66,4 +71,103 @@ exports.defineManualTests = function (contentEl, createActionButton) {
66
71
}
67
72
) ;
68
73
} ) ;
74
+
75
+ createActionButton ( 'Connect' , function ( ) {
76
+ connectedDevice = undefined ;
77
+ contentEl . innerHTML = '' ;
78
+ buttonList (
79
+ scanned ,
80
+ ( d ) => [ d . name , d . id ] ,
81
+ ( d ) => ble . connect ( d . id , onConnect , onError )
82
+ ) ;
83
+
84
+ function onConnect ( d ) {
85
+ connectedDevice = d ;
86
+ console . log ( 'BLE connected:' , JSON . stringify ( d ) ) ;
87
+ contentEl . innerHTML = '' ;
88
+ }
89
+
90
+ function onError ( e ) {
91
+ console . log ( 'Error' , e ) ;
92
+ contentEl . innerHTML = '' ;
93
+ }
94
+ } ) ;
95
+
96
+ createActionButton ( 'Read' , function ( ) {
97
+ contentEl . innerHTML = '' ;
98
+ const readable = connectedDevice . characteristics . filter ( ( c ) => c . properties . indexOf ( 'Read' ) != - 1 ) ;
99
+ buttonList (
100
+ readable ,
101
+ ( c ) => [ c . service , c . characteristic ] ,
102
+ ( c ) => ble . read ( connectedDevice . id , c . service , c . characteristic , onRead , onError )
103
+ ) ;
104
+
105
+ function onRead ( d ) {
106
+ console . log ( 'Payload read:' , JSON . stringify ( Array . from ( new Uint8Array ( d ) ) ) ) ;
107
+ contentEl . innerHTML = '' ;
108
+ }
109
+
110
+ function onError ( e ) {
111
+ console . log ( 'Error' , e ) ;
112
+ contentEl . innerHTML = '' ;
113
+ }
114
+ } ) ;
115
+
116
+ createActionButton ( 'Write' , function ( ) {
117
+ contentEl . innerHTML = '' ;
118
+ const writable = connectedDevice . characteristics . filter ( ( c ) => c . properties . indexOf ( 'Write' ) != - 1 ) ;
119
+ const payload = new Uint8Array ( 1 ) ;
120
+ payload [ 0 ] = 65 ; // Capital A
121
+ buttonList (
122
+ writable ,
123
+ ( c ) => [ c . service , c . characteristic ] ,
124
+ ( c ) => ble . write ( connectedDevice . id , c . service , c . characteristic , payload . buffer , onWrite , onError )
125
+ ) ;
126
+
127
+ function onWrite ( d ) {
128
+ console . log ( 'Payload written' ) ;
129
+ contentEl . innerHTML = '' ;
130
+ }
131
+
132
+ function onError ( e ) {
133
+ console . log ( 'Error' , e ) ;
134
+ contentEl . innerHTML = '' ;
135
+ }
136
+ } ) ;
137
+
138
+ createActionButton ( 'Notify' , function ( ) {
139
+ contentEl . innerHTML = '' ;
140
+ const notifiable = connectedDevice . characteristics . filter (
141
+ ( c ) => c . properties . indexOf ( 'Notify' ) != - 1 || c . properties . indexOf ( 'Indicate' ) != - 1
142
+ ) ;
143
+ buttonList (
144
+ notifiable ,
145
+ ( c ) => [ c . service , c . characteristic ] ,
146
+ ( c ) => ble . startNotifications ( connectedDevice . id , c . service , c . characteristic , onRead , onError )
147
+ ) ;
148
+
149
+ function onRead ( d ) {
150
+ console . log ( 'Payload received:' , JSON . stringify ( Array . from ( new Uint8Array ( d ) ) ) ) ;
151
+ contentEl . innerHTML = '' ;
152
+ }
153
+
154
+ function onError ( e ) {
155
+ console . log ( 'Error' , e ) ;
156
+ contentEl . innerHTML = '' ;
157
+ }
158
+ } ) ;
159
+
160
+ function buttonList ( items , labelFn , actionFn ) {
161
+ for ( const item of items ) {
162
+ const button = document . createElement ( 'button' ) ;
163
+ for ( const label of labelFn ( item ) ) {
164
+ button . append ( label ) ;
165
+ button . append ( document . createElement ( 'br' ) ) ;
166
+ }
167
+ button . removeChild ( button . lastElementChild ) ;
168
+ button . onclick = ( ) => actionFn ( item ) ;
169
+ contentEl . append ( button ) ;
170
+ contentEl . append ( document . createElement ( 'br' ) ) ;
171
+ }
172
+ }
69
173
} ;
0 commit comments