我正在尝试使用 tomcat7 在 raspberry pi 3 上部署一个 web 服务。该 web 服务调用良好并且可能工作,甚至 tomcat7 也可以执行 sudo 命令。这段代码是从蓝牙获取一个值,它在 Java 中工作得很好,但在 tomcat7 中却给出了 CommPortIdentifier.getPortIdentifier("/dev/rfcomm0") 行中的异常 NoSuchPortException
import java.io.*;
import gnu.io.*;
//Connect the Raspberry with serial port
public class AGetBlueIn {
private static InputStream inStream;
public String getRate(){
String reading="";
//Connecting to BluetoothDevice
try {
Runtime.getRuntime().exec("sudo rfcomm listen rfcomm0 1");
System.out.println("waiting for connection");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
return "Error in Starting the Connection Command";
}
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("/dev/rfcomm0");
SerialPort serialPort = (SerialPort) portId.open("BlueHeart", 5000);
// Change baud rate if not 115200
/*serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);*/
inStream = serialPort.getInputStream();
System.out.println("Enter a value");
for(int i=0;i<10;i++) {
Thread.sleep(1000);
if(inStream.available() > 0) {
int b = inStream.read();
System.out.print((char)b);
reading=reading+(char)b;
}
}
} catch (Exception ex) {
StringWriter errors=new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
return errors.toString();
}
return reading;
//serialPort.close();
}
}
最佳答案
我通过将 tomcat7 添加到 dialout 组来解决该问题,这样它就可以通过在终端上执行以下命令来访问 rfcomm0: sudo usermod -a -G 拨出 tomcat7
关于java - rfcomm 无法在树莓派 3 上与 tomcat7 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170765/