1
1
/*
2
2
--------------------------------------------------------------------
3
- Puck.js BLE Interface library
4
- Copyright 2020 Gordon Williams ([email protected] )
3
+ Puck.js BLE Interface library for Nordic UART
4
+ Copyright 2021 Gordon Williams ([email protected] )
5
+ https://github.com/espruino/EspruinoWebTools
5
6
--------------------------------------------------------------------
6
7
This Source Code Form is subject to the terms of the Mozilla Public
7
8
License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -47,6 +48,13 @@ Or more advanced usage with control of the connection
47
48
});
48
49
});
49
50
51
+ ChangeLog:
52
+
53
+ ...
54
+ 1v00 : Added Promises to write/eval
55
+ 1v01 : Raise default Chunk Size to 20
56
+ Auto-adjust chunk size up if we receive >20 bytes in a packet
57
+
50
58
*/
51
59
( function ( root , factory ) {
52
60
if ( typeof define === 'function' && define . amd ) {
@@ -88,15 +96,15 @@ Or more advanced usage with control of the connection
88
96
window . location = "https://itunes.apple.com/us/app/webble/id1193531073" ;
89
97
} else {
90
98
if ( confirm ( "This Web Browser doesn't support Web Bluetooth.\nPlease click Ok to see instructions for enabling it." ) )
91
- window . location = "https://www.espruino.com/Puck.js+ Quick+Start" ;
99
+ window . location = "https://www.espruino.com/Quick+Start+BLE#with-web-bluetooth " ;
92
100
}
93
101
return false ;
94
102
}
95
103
96
104
var NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e" ;
97
105
var NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" ;
98
106
var NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" ;
99
- var CHUNKSIZE = 16 ;
107
+ var DEFAULT_CHUNKSIZE = 20 ;
100
108
101
109
function log ( level , s ) {
102
110
if ( puck . log ) puck . log ( level , s ) ;
@@ -140,6 +148,7 @@ Or more advanced usage with control of the connection
140
148
var rxCharacteristic ;
141
149
var txDataQueue = [ ] ;
142
150
var flowControlXOFF = false ;
151
+ var chunkSize = DEFAULT_CHUNKSIZE ;
143
152
144
153
connection . close = function ( ) {
145
154
connection . isOpening = false ;
@@ -154,6 +163,7 @@ Or more advanced usage with control of the connection
154
163
btServer = undefined ;
155
164
txCharacteristic = undefined ;
156
165
rxCharacteristic = undefined ;
166
+ chunkSize = DEFAULT_CHUNKSIZE ;
157
167
}
158
168
} ;
159
169
@@ -173,12 +183,12 @@ Or more advanced usage with control of the connection
173
183
}
174
184
var txItem = txDataQueue [ 0 ] ;
175
185
puck . writeProgress ( txItem . maxLength - txItem . data . length , txItem . maxLength ) ;
176
- if ( txItem . data . length <= CHUNKSIZE ) {
186
+ if ( txItem . data . length <= chunkSize ) {
177
187
chunk = txItem . data ;
178
188
txItem . data = undefined ;
179
189
} else {
180
- chunk = txItem . data . substr ( 0 , CHUNKSIZE ) ;
181
- txItem . data = txItem . data . substr ( CHUNKSIZE ) ;
190
+ chunk = txItem . data . substr ( 0 , chunkSize ) ;
191
+ txItem . data = txItem . data . substr ( chunkSize ) ;
182
192
}
183
193
connection . txInProgress = true ;
184
194
log ( 2 , "Sending " + JSON . stringify ( chunk ) ) ;
@@ -203,10 +213,9 @@ Or more advanced usage with control of the connection
203
213
filters :[
204
214
{ namePrefix : 'Puck.js' } ,
205
215
{ namePrefix : 'Pixl.js' } ,
216
+ { namePrefix : 'Jolt.js' } ,
206
217
{ namePrefix : 'MDBT42Q' } ,
207
- { namePrefix : 'RuuviTag' } ,
208
- { namePrefix : 'iTracker' } ,
209
- { namePrefix : 'Thingy' } ,
218
+ { namePrefix : 'Bangle.js' } ,
210
219
{ namePrefix : 'Espruino' } ,
211
220
{ services : [ NORDIC_SERVICE ] }
212
221
] , optionalServices : [ NORDIC_SERVICE ] } ) . then ( function ( device ) {
@@ -240,6 +249,10 @@ Or more advanced usage with control of the connection
240
249
rxCharacteristic . addEventListener ( 'characteristicvaluechanged' , function ( event ) {
241
250
var dataview = event . target . value ;
242
251
var data = ab2str ( dataview . buffer ) ;
252
+ if ( puck . increaseMTU && ( data . length > chunkSize ) ) {
253
+ log ( 2 , "Received packet of length " + data . length + ", increasing chunk size" ) ;
254
+ chunkSize = data . length ;
255
+ }
243
256
if ( puck . flowControl ) {
244
257
for ( var i = 0 ; i < data . length ; i ++ ) {
245
258
var ch = data . charCodeAt ( i ) ;
@@ -386,6 +399,10 @@ Or more advanced usage with control of the connection
386
399
var puck = {
387
400
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
388
401
debug : 1 ,
402
+ /** When we receive more than 20 bytes, should we increase the chunk size we use
403
+ for writing to match it? Normally this is fine but it seems some phones have
404
+ a broken bluetooth implementation that doesn't allow it. */
405
+ increaseMTU : true ,
389
406
/// Should we use flow control? Default is true
390
407
flowControl : true ,
391
408
/// Used internally to write log information - you can replace this with your own function
0 commit comments