@@ -109,6 +109,7 @@ public class ConnectedFragment extends BaseBackFragment implements View.OnClickL
109109 private Disposable connectionDisposable ;
110110 private Observable <RxBleConnection > mConnectionObservable ;
111111 private PublishSubject <Boolean > disconnectTriggerSubject = PublishSubject .create ();
112+ private int mergeProcessCounter = 0 ;
112113
113114 // GUI components
114115 private Toolbar mToolbar ;
@@ -447,7 +448,7 @@ private void initDelayView() {
447448
448449 connectionDisposable = device .establishConnection (false ) // <-- autoConnect flag
449450 .observeOn (AndroidSchedulers .mainThread ())
450- .doFinally (this ::dispose )
451+ .doFinally (this ::onConnectionDispose )
451452 .subscribe (this ::onConnectionReceived , this ::onConnectionFailure );
452453 }
453454
@@ -473,33 +474,42 @@ private void morphingBeaconButtonClicked( ) {
473474 singles .add (rxBleConnection .writeCharacteristic (mAlfaAppleBeaconMajorCharacteristic , majorArray ));
474475 singles .add (rxBleConnection .writeCharacteristic (mAlfaAppleBeaconMinorCharacteristic , minorArray ));
475476 singles .add (rxBleConnection .writeCharacteristic (mAlfaAppleBeaconTxmCharacteristic , txmArray ));
476-
477+ mergeProcessCounter = 0 ;
477478 Single .merge (singles )
478- .observeOn (AndroidSchedulers .mainThread ())
479+ .observeOn (AndroidSchedulers .mainThread ())
479480 .subscribe (bytes -> {
480- Log .d (TAG , String .format ("update beacon profile Success" ));
481- morphToCompleted (iBeaconMorphingButton );
481+ Log .d (TAG , String .format ("update beacon profile Success %s" , ParserUtils .bytesToHex (bytes )));
482+ mergeProcessCounter ++;
483+ if (mergeProcessCounter == singles .size ()) {
484+ mergeProcessCounter = 0 ;
485+ morphToCompleted (iBeaconMorphingButton );
486+ }
482487 }, this ::onConnectionFailure );
488+
483489 }
484490
485491 private void morphingRadioButtonClicked () {
486492
487493 morphToProcess (radioMorphingButton );
488494 byte [] radioIntervalArray = new byte [2 ];
489495 byte [] radioTxPowerArray = new byte [1 ];
490- radioIntervalArray [0 ] = (byte )(( radioInterval >> 8 ) & 0xff );
491- radioIntervalArray [1 ] = (byte )(radioInterval & 0xff );
496+ radioIntervalArray [0 ] = (byte )(radioInterval & 0xff );
497+ radioIntervalArray [1 ] = (byte )(( radioInterval >> 8 ) & 0xff );
492498 radioTxPowerArray [0 ] = (byte )radioTxPower ;
493499
494500 List <Single <byte []>> singles = new ArrayList <>();
495501 singles .add (rxBleConnection .writeCharacteristic (mAlfaRadioIntervalCharacteristic , radioIntervalArray ));
496502 singles .add (rxBleConnection .writeCharacteristic (mAlfaRadioTxPowerCharacteristic , radioTxPowerArray ));
497-
503+ mergeProcessCounter = 0 ;
498504 Single .merge (singles )
499505 .observeOn (AndroidSchedulers .mainThread ())
500506 .subscribe (bytes -> {
501507 Log .d (TAG , String .format ("update radio profile Success" ));
502- morphToCompleted (radioMorphingButton );
508+ mergeProcessCounter ++;
509+ if (mergeProcessCounter == singles .size ()) {
510+ mergeProcessCounter = 0 ;
511+ morphToCompleted (radioMorphingButton );
512+ }
503513 }, this ::onConnectionFailure );
504514 }
505515
@@ -534,6 +544,7 @@ private void updateRssi(Long along) {
534544 private void onConnectionReceived (RxBleConnection connection ) {
535545 // start discovery
536546 rxBleConnection = connection ;
547+
537548 rxBleConnection .discoverServices ()
538549 .observeOn (AndroidSchedulers .mainThread ())
539550 .subscribe (rxBleDeviceServices -> {
@@ -586,20 +597,16 @@ private void onConnectionReceived(RxBleConnection connection) {
586597 rxBleConnection .readCharacteristic (mAlfaRadioIntervalCharacteristic )
587598 .observeOn (AndroidSchedulers .mainThread ())
588599 .subscribe ( value -> {
589- // iBeaconTxmEdit.setText(String.format("%d", (int)value[0]));
590- int interval = (int )value [1 ] << 8 ;
591- interval = interval | (int )value [0 ];
592- radioInterval = interval ;
593- Log .i (TAG , String .format ("found Radio interval characteristic %d" , interval ));
594- updateRadioIntervalSeekbar (interval );
600+ radioInterval = (value [1 ] & 0xff ) * 0x100 + (value [0 ] & 0xff );
601+ Log .i (TAG , String .format ("found Radio interval characteristic %d" , radioInterval ));
602+ updateRadioIntervalSeekbar (radioInterval );
595603 }, this ::onConnectionFailure );
596604 } else if (character .getUuid ().equals (UUID_ALFA_RADIO_CHARACTER_TXPOWER )) {
597605 Log .i (TAG , String .format ("found Radio txpower characteristic" ));
598606 mAlfaRadioTxPowerCharacteristic = character ;
599607 rxBleConnection .readCharacteristic (mAlfaRadioTxPowerCharacteristic )
600608 .observeOn (AndroidSchedulers .mainThread ())
601609 .subscribe ( value -> {
602- // iBeaconTxmEdit.setText(String.format("%d", (int)value[0]));
603610 int txpower = (int )value [0 ] ;
604611 radioTxPower = txpower ;
605612 Log .i (TAG , String .format ("found Radio txpower characteristic %d" , txpower ));
@@ -675,7 +682,8 @@ public void onClick(DialogInterface dialog, int which) {
675682 .show ();
676683 }
677684
678- private void dispose () {
685+ private void onConnectionDispose () {
686+ Log .d (TAG , "dispose event" );
679687 connectionDisposable = null ;
680688 pop ();
681689 }
@@ -735,13 +743,23 @@ private void morphToProcess(final IndeterminateProgressButton btnMorph) {
735743 }
736744
737745 private void morphToCompleted (final IndeterminateProgressButton btnMorph ) {
738- btnMorph .unblockTouch ();
739- morphToSuccess (btnMorph );
740746 Handler handler = new Handler ();
747+
741748 handler .postDelayed (new Runnable () {
742749 @ Override
743750 public void run () {
744- morphToSquare (btnMorph , 0 );
751+ morphToSuccess (btnMorph );
752+ Handler handler2 = new Handler ();
753+ // morphToSquare(btnMorph, 1500);
754+ // btnMorph.unblockTouch();
755+ handler2 .postDelayed (new Runnable () {
756+ @ Override
757+ public void run () {
758+ morphToSquare (btnMorph , 500 );
759+ btnMorph .unblockTouch ();
760+
761+ }
762+ }, 1500 );
745763 }
746764 }, 1500 );
747765 }
0 commit comments