@@ -88,9 +88,11 @@ public class ConnectedFragment extends BaseBackFragment implements View.OnClickL
8888 public final static UUID UUID_ALFA_RADIO_SERVICE = UUID .fromString ("19FA0001-F6C2-09A3-E9F9-128ABCA31297" );
8989 public static final UUID UUID_ALFA_RADIO_CHARACTER_INTERVAL = UUID .fromString ("19FA0002-F6C2-09A3-E9F9-128ABCA31297" );
9090 public static final UUID UUID_ALFA_RADIO_CHARACTER_TXPOWER = UUID .fromString ("19FA0003-F6C2-09A3-E9F9-128ABCA31297" );
91+ public static final UUID UUID_ALFA_RADIO_CHARACTER_CONN = UUID .fromString ("19FA0004-F6C2-09A3-E9F9-128ABCA31297" );
9192 private BluetoothGattService mAlfaRadioService ;
9293 private BluetoothGattCharacteristic mAlfaRadioIntervalCharacteristic ;
9394 private BluetoothGattCharacteristic mAlfaRadioTxPowerCharacteristic ;
95+ private BluetoothGattCharacteristic mAlfaRadioConnCharacteristic ;
9496
9597 /** AlfaBeacon Alfa2477s Service UUID */
9698 public final static UUID UUID_ALFA_2477S_SERVICE = UUID .fromString ("903E0001-F6C2-09A3-E9F9-128ABCA31297" );
@@ -109,6 +111,7 @@ public class ConnectedFragment extends BaseBackFragment implements View.OnClickL
109111 private RxBleConnection rxBleConnection ;
110112 private Disposable connectionDisposable ;
111113 private int mergeProcessCounter = 0 ;
114+ private boolean disconnecClicked = false ;
112115
113116 // GUI components
114117 private Toolbar mToolbar ;
@@ -185,17 +188,29 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
185188
186189 private void initView (View view ) {
187190 // init toolbar
191+ disconnecClicked = false ;
188192 mToolbar = (Toolbar ) view .findViewById (R .id .toolbar );
189193 mToolbar .setTitle (String .format ("%s - %s" , getResources ().getString (R .string .establish_connection ), macAddress ));
190194 mToolbar .setNavigationIcon (R .drawable .ic_arrow_back_white_24dp );
191195 mToolbar .setNavigationOnClickListener (new View .OnClickListener () {
192196 @ Override
193197 public void onClick (View v ) {
194- // RxBleDevice rxdevice = rxBleClient.getBleDevice(macAddress);
195- // RxBleConnection.RxBleConnectionState state = rxdevice.getConnectionState();
196- // if (state == )
197- if (connectionDisposable != null ) {
198- connectionDisposable .dispose ();
198+ if (disconnecClicked ) {
199+ return ;
200+ }
201+ disconnecClicked = true ;
202+ byte [] disconnect_command = new byte [1 ];
203+ disconnect_command [0 ] = 0x01 ;
204+ if (mAlfaRadioConnCharacteristic != null && rxBleConnection != null ) {
205+ rxBleConnection .writeCharacteristic (mAlfaRadioConnCharacteristic , disconnect_command )
206+ .observeOn (AndroidSchedulers .mainThread ())
207+ .subscribe (bytes -> {
208+ Log .d (TAG , "disconnected" );
209+ });
210+ } else {
211+ if (connectionDisposable != null ) {
212+ connectionDisposable .dispose ();
213+ }
199214 }
200215 }
201216 });
@@ -651,6 +666,9 @@ private void onConnectionReceived(RxBleConnection connection) {
651666 Log .i (TAG , String .format ("found Radio txpower characteristic %d" , txpower ));
652667 updateRadioTxPowerSeekbar (txpower );
653668 }, this ::onConnectionFailure );
669+ } else if (character .getUuid ().equals (UUID_ALFA_RADIO_CHARACTER_CONN )) {
670+ Log .i (TAG , String .format ("found Radio connection characteristic" ));
671+ mAlfaRadioConnCharacteristic = character ;
654672 }
655673 }
656674 radioView .setVisibility (View .VISIBLE );
@@ -709,20 +727,25 @@ public void onClick(DialogInterface dialog, int which) {
709727
710728 private void onConnectionFailure (Throwable throwable ) {
711729 // show the dialog
712- new AlertDialog .Builder (_mActivity )
713- .setMessage (R .string .connect_failure )
714- .setPositiveButton (R .string .ok , new DialogInterface .OnClickListener () {
715- @ Override
716- public void onClick (DialogInterface dialog , int which ) {
717- if (connectionDisposable != null )
718- connectionDisposable .dispose ();
719- }
720- })
721- .show ();
730+ Log .i (TAG , "onConnectionFailure " + throwable .toString ());
731+ if (connectionDisposable != null ) {
732+ if (!disconnecClicked ) {
733+ new AlertDialog .Builder (_mActivity )
734+ .setMessage (R .string .connect_failure )
735+ .setPositiveButton (R .string .ok , new DialogInterface .OnClickListener () {
736+ @ Override
737+ public void onClick (DialogInterface dialog , int which ) {
738+ if (connectionDisposable != null )
739+ connectionDisposable .dispose ();
740+ }
741+ })
742+ .show ();
743+ }
744+ }
722745 }
723746
724747 private void onConnectionDispose () {
725- Log .d (TAG , "dispose event " );
748+ Log .d (TAG , "onConnectionDispose " );
726749 connectionDisposable = null ;
727750 pop ();
728751 }
0 commit comments