|
8 | 8 | import org.apache.commons.exec.CommandLine; |
9 | 9 | import org.apache.commons.lang3.time.StopWatch; |
10 | 10 |
|
| 11 | +import javax.swing.*; |
| 12 | + |
11 | 13 | @Slf4j |
12 | 14 | public class BluetoothConnector { |
| 15 | + |
13 | 16 | public static final boolean PRINT_OUT = false; |
14 | 17 | public static final int BLUETOOTH_POWER_ON_DELAY = 1000; |
15 | 18 | private final int timeoutSecs = 10; |
| 19 | + private char[] sudoPassword; |
16 | 20 |
|
17 | 21 | public void connect(String deviceId) { |
18 | 22 |
|
@@ -70,15 +74,28 @@ boolean disconnect(String deviceId) { |
70 | 74 | CommandLines.Result restartService() { |
71 | 75 |
|
72 | 76 | 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 | + } |
73 | 87 |
|
74 | 88 | final CommandLine cmd = new CommandLine("/bin/sh") |
75 | 89 | .addArguments(new String[]{ |
76 | 90 | "-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 | + ) |
78 | 95 | }, false); |
79 | 96 | final CommandLines.Result result = CommandLines.exec(cmd) |
80 | 97 | .checkExecution(); |
81 | | - log.info("status=restarted, {}", result.toString(PRINT_OUT)); |
| 98 | + log.debug("status=restarted, {}", result.toString(PRINT_OUT)); |
82 | 99 | Threads.sleep(BLUETOOTH_POWER_ON_DELAY); // wait some time to bluetooth power on |
83 | 100 | return result; |
84 | 101 | } |
|
0 commit comments