Run Linux Bluetooth Command Java

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

I scanned and paired Bluetooth with commands on Linux.

I'm going to move the process to Java.

Runtime running the Java Linux command was used, but it did not run.

This is the Java code that I made.

public class ParingBluetooth {

    public static void main(String[] args) {
        try {
            Process p = null;
            //p = Runtime.getRuntime().exec("hcitool scan");
            //p.waitFor();
            p = Runtime.getRuntime().exec("sudo bluetoothctl");
            p.waitFor();
            p = null;
            p = Runtime.getRuntime().exec("scan on");
            p.waitFor();
            p = null;
            //p = Runtime.getRuntime().exec("discoverable on");
            //p.waitFor();
            p = Runtime.getRuntime().exec("pair 94:76:B7:43:65:DC");
            p.waitFor();
            p = null;
            p = Runtime.getRuntime().exec("yes");
            p.waitFor();
        } } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

If you run this on Linux, this error appears.

java.io.IOException: Cannot run program "scan": error=2, No such file or directory
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
        at java.base/java.lang.Runtime.exec(Runtime.java:592)
        at java.base/java.lang.Runtime.exec(Runtime.java:416)
        at java.base/java.lang.Runtime.exec(Runtime.java:313)
        at ParingBluetooth.main(ParingBluetooth.java:11)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
        at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
        at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)

If you look at the error, I think there is an error when trying to execute the scan command, but the sudo bluetoothctl command seems to have gone into the exception because it has not been executed since the scan on entering the Bluetooth command.

Can't I run Linux Bluetooth commands with Runtime?

How do I run Linux Bluetooth commands in Java?

linux java bluetooth

2022-09-20 10:48

1 Answers

I answer myself.

public class ParingBluetooth {

    public static void main(String[] args) {
        try {
            Process p = null;
            //p = Runtime.getRuntime().exec("hcitool scan");
            //p.waitFor();
            p = Runtime.getRuntime().exec("sudo bluetoothctl -- scan on");
            p.waitFor();
            p = null;
            //p = Runtime.getRuntime().exec("sudo bluetoothctl -- discoverable on");
            //p.waitFor();
            p = Runtime.getRuntime().exec("sudo bluetoothctl -- pair 94:76:B7:43:65:DC");
            p.waitFor();
            p = null;
            p = Runtime.getRuntime().exec("sudo bluetoothctl -- yes");
            p.waitFor();
        } } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

It runs well by putting sudo Bluetoothctl -- in front of the Bluetooth-related command.


2022-09-20 10:48

If you have any answers or tips


© 2022 pinfo. All rights reserved.