@@ -374,14 +374,21 @@ export default class ReactNativeBleTransport {
374
374
} ) ;
375
375
376
376
const disconnectSubscription = device . onDisconnected ( ( ) => {
377
- this . Log . debug ( 'device disconnect: ' , device ?. id ) ;
378
- this . emitter ?. emit ( 'device-disconnect' , {
379
- name : device ?. name ,
380
- id : device ?. id ,
381
- connectId : device ?. id ,
382
- } ) ;
383
- this . release ( uuid ) ;
384
- disconnectSubscription ?. remove ( ) ;
377
+ try {
378
+ this . Log . debug ( 'device disconnect: ' , device ?. id ) ;
379
+ this . emitter ?. emit ( 'device-disconnect' , {
380
+ name : device ?. name ,
381
+ id : device ?. id ,
382
+ connectId : device ?. id ,
383
+ } ) ;
384
+ if ( this . runPromise ) {
385
+ this . runPromise . reject ( ERRORS . TypedError ( HardwareErrorCode . BleConnectedError ) ) ;
386
+ }
387
+ } finally {
388
+ this . runPromise = null ;
389
+ this . release ( uuid ) ;
390
+ disconnectSubscription ?. remove ( ) ;
391
+ }
385
392
} ) ;
386
393
387
394
return { uuid } ;
@@ -392,41 +399,45 @@ export default class ReactNativeBleTransport {
392
399
let buffer : any [ ] = [ ] ;
393
400
const subscription = characteristic . monitor ( ( error , c ) => {
394
401
if ( error ) {
395
- this . Log . debug (
396
- `error monitor ${ characteristic . uuid } , deviceId: ${ characteristic . deviceID } : ${
397
- error as unknown as string
398
- } `
399
- ) ;
400
- if ( this . runPromise ) {
401
- let ERROR :
402
- | typeof HardwareErrorCode . BleDeviceBondError
403
- | typeof HardwareErrorCode . BleCharacteristicNotifyError
404
- | typeof HardwareErrorCode . BleTimeoutError =
405
- HardwareErrorCode . BleCharacteristicNotifyError ;
406
- if ( error . reason ?. includes ( 'The connection has timed out unexpectedly' ) ) {
407
- ERROR = HardwareErrorCode . BleTimeoutError ;
408
- }
409
- if ( error . reason ?. includes ( 'Encryption is insufficient' ) ) {
410
- ERROR = HardwareErrorCode . BleDeviceBondError ;
411
- }
412
- if (
413
- error . reason ?. includes ( 'Cannot write client characteristic config descriptor' ) ||
414
- error . reason ?. includes ( 'Cannot find client characteristic config descriptor' ) ||
415
- error . reason ?. includes ( 'The handle is invalid' )
416
- ) {
417
- this . runPromise . reject (
418
- ERRORS . TypedError (
419
- HardwareErrorCode . BleCharacteristicNotifyChangeFailure ,
420
- error . message ?? error . reason
421
- )
422
- ) ;
423
- this . Log . debug (
424
- `${ HardwareErrorCode . BleCharacteristicNotifyChangeFailure } ${ error . message } ${ error . reason } `
425
- ) ;
426
- return ;
402
+ try {
403
+ this . Log . debug (
404
+ `error monitor ${ characteristic . uuid } , deviceId: ${ characteristic . deviceID } : ${
405
+ error as unknown as string
406
+ } `
407
+ ) ;
408
+ if ( this . runPromise ) {
409
+ let ERROR :
410
+ | typeof HardwareErrorCode . BleDeviceBondError
411
+ | typeof HardwareErrorCode . BleCharacteristicNotifyError
412
+ | typeof HardwareErrorCode . BleTimeoutError =
413
+ HardwareErrorCode . BleCharacteristicNotifyError ;
414
+ if ( error . reason ?. includes ( 'The connection has timed out unexpectedly' ) ) {
415
+ ERROR = HardwareErrorCode . BleTimeoutError ;
416
+ }
417
+ if ( error . reason ?. includes ( 'Encryption is insufficient' ) ) {
418
+ ERROR = HardwareErrorCode . BleDeviceBondError ;
419
+ }
420
+ if (
421
+ error . reason ?. includes ( 'Cannot write client characteristic config descriptor' ) ||
422
+ error . reason ?. includes ( 'Cannot find client characteristic config descriptor' ) ||
423
+ error . reason ?. includes ( 'The handle is invalid' )
424
+ ) {
425
+ this . runPromise . reject (
426
+ ERRORS . TypedError (
427
+ HardwareErrorCode . BleCharacteristicNotifyChangeFailure ,
428
+ error . message ?? error . reason
429
+ )
430
+ ) ;
431
+ this . Log . debug (
432
+ `${ HardwareErrorCode . BleCharacteristicNotifyChangeFailure } ${ error . message } ${ error . reason } `
433
+ ) ;
434
+ return ;
435
+ }
436
+ this . runPromise . reject ( ERRORS . TypedError ( ERROR , error . reason ?? error . message ) ) ;
437
+ this . Log . debug ( ': monitor notify error, and has unreleased Promise' ) ;
427
438
}
428
- this . runPromise . reject ( ERRORS . TypedError ( ERROR , error . reason ?? error . message ) ) ;
429
- this . Log . debug ( ': monitor notify error, and has unreleased Promise' ) ;
439
+ } finally {
440
+ this . runPromise = null ;
430
441
}
431
442
return ;
432
443
}
@@ -516,30 +527,55 @@ export default class ReactNativeBleTransport {
516
527
this . Log . debug ( 'transport-react-native' , 'call-' , ' name: ' , name , ' data: ' , data ) ;
517
528
}
518
529
519
- const buffers = buildBuffers ( messages , name , data ) ;
530
+ const buffers = buildBuffers ( messages , name , data ) as Array < ByteBuffer > ;
520
531
521
- if ( name === 'FirmwareUpload' || name === 'EmmcFileWrite' ) {
532
+ async function writeChunkedData (
533
+ buffers : ByteBuffer [ ] ,
534
+ writeFunction : ( data : string ) => Promise < void > ,
535
+ onError : ( e : any ) => void
536
+ ) {
522
537
const packetCapacity = Platform . OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH ;
523
538
let index = 0 ;
524
539
let chunk = ByteBuffer . allocate ( packetCapacity ) ;
540
+
525
541
while ( index < buffers . length ) {
526
542
const buffer = buffers [ index ] . toBuffer ( ) ;
527
543
chunk . append ( buffer ) ;
528
544
index += 1 ;
545
+
529
546
if ( chunk . offset === packetCapacity || index >= buffers . length ) {
530
547
chunk . reset ( ) ;
531
- // Upgrading Packet Logs, Too much content to ignore
532
- // this.Log.debug('send more packet hex strting: ', chunk.toString('hex'));
533
548
try {
534
- await transport . writeCharacteristic . writeWithoutResponse ( chunk . toString ( 'base64' ) ) ;
549
+ await writeFunction ( chunk . toString ( 'base64' ) ) ;
535
550
chunk = ByteBuffer . allocate ( packetCapacity ) ;
536
551
} catch ( e ) {
537
- this . runPromise = null ;
538
- this . Log . error ( 'writeCharacteristic write error: ' , e ) ;
539
- return ;
552
+ onError ( e ) ;
553
+ throw ERRORS . TypedError ( HardwareErrorCode . BleWriteCharacteristicError ) ;
540
554
}
541
555
}
542
556
}
557
+ }
558
+
559
+ if ( name === 'EmmcFileWrite' ) {
560
+ await writeChunkedData (
561
+ buffers ,
562
+ data => transport . writeWithRetry ( data ) ,
563
+ e => {
564
+ this . runPromise = null ;
565
+ this . Log . error ( 'writeCharacteristic write error: ' , e ) ;
566
+ }
567
+ ) ;
568
+ } else if ( name === 'FirmwareUpload' ) {
569
+ await writeChunkedData (
570
+ buffers ,
571
+ async data => {
572
+ await transport . writeCharacteristic . writeWithoutResponse ( data ) ;
573
+ } ,
574
+ e => {
575
+ this . runPromise = null ;
576
+ this . Log . error ( 'writeCharacteristic write error: ' , e ) ;
577
+ }
578
+ ) ;
543
579
} else {
544
580
for ( const o of buffers ) {
545
581
const outData = o . toString ( 'base64' ) ;
@@ -552,11 +588,11 @@ export default class ReactNativeBleTransport {
552
588
this . runPromise = null ;
553
589
if ( e . errorCode === BleErrorCode . DeviceDisconnected ) {
554
590
throw ERRORS . TypedError ( HardwareErrorCode . BleDeviceNotBonded ) ;
555
- }
556
- if ( e . errorCode === BleErrorCode . OperationStartFailed ) {
591
+ } else if ( e . errorCode === BleErrorCode . OperationStartFailed ) {
557
592
throw ERRORS . TypedError ( HardwareErrorCode . BleWriteCharacteristicError , e . reason ) ;
593
+ } else {
594
+ throw ERRORS . TypedError ( HardwareErrorCode . BleWriteCharacteristicError ) ;
558
595
}
559
- return ;
560
596
}
561
597
}
562
598
}
0 commit comments