@@ -6,7 +6,7 @@ var debugRaw = require('debug')('metaboot');
66
77var NobleDevice = require ( 'noble-device' ) ;
88var noble = require ( '@abandonware/noble' ) ;
9- var ref = require ( 'ref' ) ;
9+ var ref = require ( 'ref-napi ' ) ;
1010var events = require ( 'events' ) ;
1111var util = require ( 'util' ) ;
1212var os = require ( 'os' ) ;
@@ -121,11 +121,13 @@ MetaWear.prototype.connectAndSetUp = function (callback, initBuf) {
121121 MetaWear . mbl_mw_metawearboard_deserialize ( this . board , initBuf , initBuf . length ) ;
122122 }
123123 // Setup the CPP SDK
124+ console . log ( this . board )
124125 MetaWear . mbl_mw_metawearboard_initialize ( this . board , ref . NULL , MetaWear . FnVoid_VoidP_MetaWearBoardP_Int . toPointer ( function onInitialize ( context , board , code ) {
125126 // Adjust link speed for macOS only since we can't adjust it elsewhere
126127 if ( os . platform ( ) === 'darwin' ) {
127128 MetaWear . mbl_mw_settings_set_connection_parameters ( this . board , 15.0 , 15.0 , 0 , 4000 ) ;
128129 }
130+ console . log ( "init" ) ;
129131 this . readManufacturerName ( function ( error , manufacturerName ) {
130132 this . manufacturerName = manufacturerName ;
131133 } . bind ( this ) ) ;
@@ -140,8 +142,10 @@ MetaWear.prototype.connectAndSetUp = function (callback, initBuf) {
140142 } . bind ( this ) ) ;
141143 this . readModelNumber ( function ( error , modelNumber ) {
142144 this . modelNumber = modelNumber ;
145+ console . log ( "read model number" ) ;
143146 this . modelDescription = MetaWear . mbl_mw_metawearboard_get_model_name ( this . board ) ;
144147 debug ( "Finished MetaWear Init" ) ;
148+ console . log ( "done" ) ;
145149 initializeCallback ( code == 0 ? null : code ) ;
146150 } . bind ( this ) ) ;
147151 } . bind ( this ) ) ) ;
@@ -220,34 +224,42 @@ function bytesToString(array, start, stop) {
220224function NativeGattChar ( array ) {
221225 // always initialize all instance properties
222226 this . serviceUUID = bytesToString ( array , 7 , 0 ) + bytesToString ( array , 15 , 8 ) ;
227+ console . log ( this . serviceUUID ) ;
223228 this . shortServiceUUID = bytesToString ( array , 5 , 4 ) ;
229+ console . log ( this . shortServiceUUID ) ;
224230 this . characteristicUUID = bytesToString ( array , 23 , 16 ) + bytesToString ( array , 31 , 24 ) ;
231+ console . log ( this . characteristicUUID ) ;
225232 this . shortCharacteristicUUID = bytesToString ( array , 21 , 20 ) ;
233+ console . log ( this . shortCharacteristicUUID ) ;
226234}
227235
228236function writeGattChar ( context , caller , writeType , characteristicPtr , valuePtr , length ) {
229- var data = ref . reinterpret ( valuePtr , length , 0 ) ;
237+ var bufs = [ ] ;
238+ var data = Buffer . alloc ( length ) ;
239+ for ( let i = 0 ; i < length ; i ++ ) {
240+ bufs [ i ] = ref . reinterpret ( valuePtr , 1 , i ) ;
241+ }
242+ data = Buffer . concat ( bufs ) ;
230243 var characteristic = new NativeGattChar ( characteristicPtr ) ;
231244 if ( ! this . hasCharacteristic ( characteristic . serviceUUID , characteristic . characteristicUUID ) ) {
232- console . error ( 'cant find ' + characteristic . characteristicUUID ) ;
233245 return ;
234246 }
235247 var printer = this . isMetaBoot ? debugRaw : debug ;
236- printer ( 'Writing: ' + characteristic . characteristicUUID + " " + data . toString ( 'hex' ) ) ;
248+ //console.log ('Writing: ' + characteristic.characteristicUUID + " " + data.toString('hex'));
237249
238250 var withoutResponse = true ;
239- if ( ! this . isMetaBoot ) {
251+ // if (!this.isMetaBoot) {
240252 // TODO: For now we need to do everything with-reponse, further
241253 // testing and enabling is needed before we can turn this off
242- withoutResponse = false ;
243- } else if ( characteristic . characteristicUUID === METABOOT_DFU_CONTROL_POINT_UUID ) {
244- withoutResponse = false ;
245- }
254+ // withoutResponse = false;
255+ // } else if (characteristic.characteristicUUID === METABOOT_DFU_CONTROL_POINT_UUID) {
256+ // withoutResponse = false;
257+ // }
246258 this . _characteristics [ characteristic . serviceUUID ] [ characteristic . characteristicUUID ] . write ( data , withoutResponse , function ( error ) {
247259 if ( error ) {
248- printer ( error ) ;
260+ console . log ( error ) ;
249261 } else {
250- printer ( 'Writing Success' ) ;
262+ //console.log ('Writing Success');
251263 }
252264 } ) ;
253265}
@@ -262,7 +274,7 @@ function readGattChar(context, caller, characteristicPtr, callback) {
262274 if ( error ) {
263275 console . error ( error ) ;
264276 } else {
265- debug ( "DidRead: " + data ) ;
277+ //console.log ("DidRead: " + data);
266278 callback ( caller , data , data . length ) ;
267279 }
268280 } ) ;
@@ -275,17 +287,16 @@ function enableNotifications(context, caller, characteristicPtr, onData, subscri
275287 charToNotify = this . _characteristics [ characteristic . shortServiceUUID ] [ characteristic . shortCharacteristicUUID ] ;
276288 }
277289 charToNotify . on ( 'data' , function ( buffer ) {
278- debug ( 'Did Update: ' , buffer ) ;
290+ //console.log ('Did Update: ', buffer);
279291 if ( ! buffer ) {
280- console . error ( 'bad buffer' ) ;
281292 } else {
282293 onData ( caller , buffer , buffer . length ) ;
283294 }
284295 } ) ;
285296 // Turn on the notification stream
286297 charToNotify . subscribe ( function ( error ) {
287298 if ( error ) {
288- console . error ( error ) ;
299+ console . log ( error ) ;
289300 }
290301 subscribeComplete ( caller , error == null ? 0 : 1 ) ;
291302 } ) ;
0 commit comments