@@ -21,73 +21,74 @@ public void connect(String deviceId, String sudoPassword) {
2121 return ;
2222 }
2323
24- final var stopWatch = StopWatch .createStarted ();
24+ final StopWatch stopWatch = StopWatch .createStarted ();
2525 Occurrence status = null ;
2626 do {
2727
2828 stopWatch .split ();
2929
3030 if (status != null ) {
3131 switch (status ) {
32- case CONNECTED_BUT_SOUND_NOT_CONFIGURED , ERROR_CONNECTION_BUSY -> {
32+ case CONNECTED_BUT_SOUND_NOT_CONFIGURED :
33+ case ERROR_CONNECTION_BUSY :
3334 this .disconnect (deviceId );
34- }
35+ break ;
3536 }
3637 }
3738
3839 this .restartService (sudoPassword );
3940 status = this .connect0 (deviceId );
4041
4142 log .debug (
42- "status=tried, occurrence={}, time={}" ,
43- status , stopWatch .getTime () - stopWatch .getSplitTime ()
43+ "status=tried, occurrence={}, time={}" ,
44+ status , stopWatch .getTime () - stopWatch .getSplitTime ()
4445 );
4546 Threads .sleep (1000 );
4647
4748 } while (status != Occurrence .CONNECTED );
4849 log .debug (
49- "status=successfullyConnected!, device={}, totalTime={}" ,
50- deviceId , stopWatch .getTime ()
50+ "status=successfullyConnected!, device={}, totalTime={}" ,
51+ deviceId , stopWatch .getTime ()
5152 );
5253 }
5354
5455 boolean disconnect (String deviceId ) {
5556 try {
56- final var result = CommandLines .exec (
57- "bluetoothctl --timeout %d disconnect %s" , timeoutSecs , deviceId
58- )
59- .checkExecution ();
57+ final CommandLines . Result result = CommandLines .exec (
58+ "bluetoothctl --timeout %d disconnect %s" , timeoutSecs , deviceId
59+ )
60+ .checkExecution ();
6061 log .debug ("status=disconnected, {}" , result .toString (PRINT_OUT ));
6162 return true ;
6263 } catch (ExecutionValidationFailedException e ) {
6364 log .debug ("status=failedToDisconnect, {}" , e .result ()
64- .toString (PRINT_OUT ));
65+ .toString (PRINT_OUT ));
6566 return false ;
6667 }
6768 }
6869
6970 CommandLines .Result restartService (String sudoPassword ) {
70- final var cmd = new CommandLine ("/bin/sh" )
71- .addArguments (new String []{
72- "-c" ,
73- String .format (
74- "echo %s | /usr/bin/sudo -S systemctl restart bluetooth.service" ,
75- sudoPassword
76- )
77- }, false );
78- final var result = CommandLines .exec (cmd )
79- .checkExecution ();
71+ final CommandLine cmd = new CommandLine ("/bin/sh" )
72+ .addArguments (new String []{
73+ "-c" ,
74+ String .format (
75+ "echo %s | /usr/bin/sudo -S systemctl restart bluetooth.service" ,
76+ sudoPassword
77+ )
78+ }, false );
79+ final CommandLines . Result result = CommandLines .exec (cmd )
80+ .checkExecution ();
8081 log .debug ("status=restarted, {}" , result .toString (PRINT_OUT ));
8182 Threads .sleep (BLUETOOTH_POWER_ON_DELAY ); // wait some time to bluetooth power on
8283 return result ;
8384 }
8485
8586 boolean isConnected (String deviceId ) {
86- final var result = CommandLines .exec (
87- "bluetoothctl info %s" , deviceId
88- )
89- .checkExecution ();
90- final var out = result .getOutAsString ();
87+ final CommandLines . Result result = CommandLines .exec (
88+ "bluetoothctl info %s" , deviceId
89+ )
90+ .checkExecution ();
91+ final String out = result .getOutAsString ();
9192 if (out .contains ("Connected: yes" )) {
9293 return true ;
9394 } else if (out .contains ("Connected: no" )) {
@@ -100,16 +101,16 @@ boolean isConnected(String deviceId) {
100101 Occurrence connect0 (String deviceId ) {
101102 try {
102103 log .debug ("status=tryConnecting, device={}" , deviceId );
103- final var result = CommandLines
104- .exec (
105- "bluetoothctl --timeout %d connect %s" , timeoutSecs , deviceId
106- )
107- .checkExecution ();
108- final var occurrence = OccurrenceParser .parse (result );
104+ final CommandLines . Result result = CommandLines
105+ .exec (
106+ "bluetoothctl --timeout %d connect %s" , timeoutSecs , deviceId
107+ )
108+ .checkExecution ();
109+ final BluetoothConnector . Occurrence occurrence = OccurrenceParser .parse (result );
109110 if (occurrence != null ) {
110111 return occurrence ;
111112 }
112- final var occur = this .connectionOccurrenceCheck (deviceId );
113+ final Occurrence occur = this .connectionOccurrenceCheck (deviceId );
113114 log .debug ("status=done, occurrence={}" , occur );
114115 return occur ;
115116 } catch (ExecutionValidationFailedException e ) {
@@ -118,7 +119,7 @@ Occurrence connect0(String deviceId) {
118119 }
119120
120121 Occurrence connectionOccurrenceCheck (String deviceId ) {
121- final var connected = this .isConnected (deviceId );
122+ final boolean connected = this .isConnected (deviceId );
122123 if (connected ) {
123124 if (this .isSoundDeviceConfigured (deviceId )) {
124125 return Occurrence .CONNECTED ;
@@ -134,18 +135,18 @@ Occurrence connectionOccurrenceCheck(String deviceId) {
134135 * bluez_sink.94_DB_56_F5_78_41.a2dp_sink
135136 */
136137 boolean isSoundDeviceConfigured (String deviceId ) {
137- final var audioSinkId = String .format (
138- "bluez_sink.%s.a2dp_sink" , deviceId .replaceAll (":" , "_" )
138+ final String audioSinkId = String .format (
139+ "bluez_sink.%s.a2dp_sink" , deviceId .replaceAll (":" , "_" )
139140 );
140- final var cmd = new CommandLine ("/bin/sh" )
141- .addArguments (new String []{"-c" , "pactl list | grep 'Sink'" }, false );
141+ final CommandLine cmd = new CommandLine ("/bin/sh" )
142+ .addArguments (new String []{"-c" , "pactl list | grep 'Sink'" }, false );
142143
143- final var result = CommandLines .exec (cmd )
144- .checkExecution ();
144+ final CommandLines . Result result = CommandLines .exec (cmd )
145+ .checkExecution ();
145146
146- final var found = result
147- .getOutAsString ()
148- .contains (audioSinkId );
147+ final boolean found = result
148+ .getOutAsString ()
149+ .contains (audioSinkId );
149150
150151 log .debug ("found={}, {}" , found , result .toString (PRINT_OUT ));
151152 return found ;
@@ -157,7 +158,6 @@ public enum Occurrence {
157158 CONNECTED ,
158159 DISCONNECTED ,
159160 ERROR_UNKNOWN ,
160-
161161 CONNECTED_BUT_SOUND_NOT_CONFIGURED ;
162162 }
163163
0 commit comments