Reputation: 1040
I have a java application in a raspberry in which I have to control an usb Bluetooth Low Energy (BLE) dongle. I want to scan for devices over a certain time. In a shell I whill call 'sudo hcitool lescan --continuous' and press ctrl-c to abort the scan.
Now in java I use ProcessBuilder to start bash and writes the command above to the OutputStream:
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
Process proc = builder.redirectOutput(Redirect.PIPE).start();
OutputStream stdin = proc.getOutputStream();
String command= "sudo hcitool lescan --continuous\n";
stdin.write(script.getBytes());
This works, I can get the InputStream of the Process to which the output of the hcitool is redirected. But how stop scanning? Writing 0x03 to the OutputStream will not work as ctrl-c is not just the value 0x03 but also a special signal SIGINT as far as I know.
So how could I do this? Is it possible to stop scanning the way I started it or do I have to choose an other way?
Upvotes: 1
Views: 511
Reputation: 1040
If someone is interested, my current solution is the following (I'm not really happy with it, but there was no time working longer on that problem):
I create a bash script which kill the process:
#!/bin/sh
VAR=$(ps -a | grep hcitool | awk {'print $1'})
sudo kill -int $VAR
Then I execute it from java.
Upvotes: 0