Skip to content

Commit 1d2bbed

Browse files
committed
refactoring
1 parent d9aa681 commit 1d2bbed

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ shadowJar {
2727
// System.setProperty("logFileName", "linux-bluetooth-connection-fix")
2828

2929
manifest {
30-
attributes("Main-Class": "com.mageddo.bluetoothfix.Main")
30+
attributes("Main-Class": "com.mageddo.linux.bluetoothfix.Main")
3131
}
3232

3333
mergeServiceFiles()

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class BluetoothConnector {
1414
public static final int BLUETOOTH_POWER_ON_DELAY = 1000;
1515
private final int timeoutSecs = 10;
1616

17-
public void connect(String deviceId, String sudoPassword) {
17+
public void connect(String deviceId) {
1818

1919
if (this.isSoundDeviceConfigured(deviceId)) {
2020
log.info("status=bluetooth-device-already-configured-and-working, deviceId={}", deviceId);
@@ -36,7 +36,7 @@ public void connect(String deviceId, String sudoPassword) {
3636
}
3737
}
3838

39-
this.restartService(sudoPassword);
39+
this.restartService();
4040
status = this.connect0(deviceId);
4141

4242
log.debug(
@@ -67,14 +67,11 @@ boolean disconnect(String deviceId) {
6767
}
6868
}
6969

70-
CommandLines.Result restartService(String sudoPassword) {
70+
CommandLines.Result restartService() {
7171
final CommandLine cmd = new CommandLine("/bin/sh")
7272
.addArguments(new String[]{
7373
"-c",
74-
String.format(
75-
"echo %s | /usr/bin/sudo -S systemctl restart bluetooth.service",
76-
sudoPassword
77-
)
74+
"systemctl restart bluetooth.service",
7875
}, false);
7976
final CommandLines.Result result = CommandLines.exec(cmd)
8077
.checkExecution();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mageddo.linux.bluetoothfix;
2+
3+
import com.sun.security.auth.module.UnixSystem;
4+
5+
public class Linux {
6+
7+
private Linux() {
8+
}
9+
10+
public static long findUserId() {
11+
return new UnixSystem().getUid();
12+
}
13+
14+
public static boolean runningAsRoot() {
15+
return findUserId() == 0;
16+
}
17+
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
import lombok.extern.slf4j.Slf4j;
44

5-
import javax.swing.*;
6-
75
@Slf4j
86
public class Main {
97
public static void main(String[] args) {
10-
new BluetoothConnector().connect("94:DB:56:F5:78:41", askForPassword());
11-
}
128

13-
static String askForPassword() {
14-
return JOptionPane.showInputDialog(
15-
null, "Sudo password to restart bluetooth service",
16-
"Sudo Password", JOptionPane.QUESTION_MESSAGE
17-
);
9+
if (!Linux.runningAsRoot()) {
10+
log.warn("you need to run as root due to restart bluetooth service, exiting...");
11+
System.exit(1);
12+
}
13+
new BluetoothConnector().connect("94:DB:56:F5:78:41");
1814
}
1915

2016
}

0 commit comments

Comments
 (0)