Skip to content

Commit 9dd2f67

Browse files
committed
fixing bluetooth was being asked everytime
1 parent 658e693 commit 9dd2f67

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/main/java/com/mageddo/linux/bluetoothfix/BluetoothConnector.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
import org.apache.commons.exec.CommandLine;
99
import org.apache.commons.lang3.time.StopWatch;
1010

11+
import javax.swing.*;
12+
1113
@Slf4j
1214
public class BluetoothConnector {
15+
1316
public static final boolean PRINT_OUT = false;
1417
public static final int BLUETOOTH_POWER_ON_DELAY = 1000;
1518
private final int timeoutSecs = 10;
19+
private char[] sudoPassword;
1620

1721
public void connect(String deviceId) {
1822

@@ -70,15 +74,28 @@ boolean disconnect(String deviceId) {
7074
CommandLines.Result restartService() {
7175

7276
log.warn("systemctl will ask you for root password to restart bluetooth service ...");
77+
if (this.sudoPassword == null) {
78+
while (true) {
79+
JPasswordField pf = new JPasswordField();
80+
final int res = JOptionPane.showConfirmDialog(null, pf, "Enter Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
81+
if (res == JOptionPane.OK_OPTION) {
82+
this.sudoPassword = pf.getPassword();
83+
break;
84+
}
85+
}
86+
}
7387

7488
final CommandLine cmd = new CommandLine("/bin/sh")
7589
.addArguments(new String[]{
7690
"-c",
77-
"systemctl restart bluetooth.service",
91+
String.format(
92+
"echo %s | /usr/bin/sudo -S systemctl restart bluetooth.service",
93+
new String(this.sudoPassword)
94+
)
7895
}, false);
7996
final CommandLines.Result result = CommandLines.exec(cmd)
8097
.checkExecution();
81-
log.info("status=restarted, {}", result.toString(PRINT_OUT));
98+
log.debug("status=restarted, {}", result.toString(PRINT_OUT));
8299
Threads.sleep(BLUETOOTH_POWER_ON_DELAY); // wait some time to bluetooth power on
83100
return result;
84101
}

0 commit comments

Comments
 (0)