rfcomm 在 raspberry pi 3 上无法与 tomcat7 一起使用
rfcomm not working with tomcat7 on raspberry pi 3
我正在尝试使用 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 添加到拨出组来解决问题,这样它就可以通过在终端上执行此命令来访问 rfcomm0:
Sudo usermod -a -G 拨出 tomcat7
我正在尝试使用 tomcat7 在 raspberry pi 3 上部署一个 web 服务 .. web 服务调用正常并且可能工作,甚至 tomcat7 可以执行 sudo 命令。这段代码是从蓝牙获取一个值,它与 Java 一起工作正常,但对于 tomcat7,它在 CommPortIdentifier.getPortIdentifier("/dev/rfcomm0")
行中给出异常 NoSuchPortExceptionimport 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 添加到拨出组来解决问题,这样它就可以通过在终端上执行此命令来访问 rfcomm0: Sudo usermod -a -G 拨出 tomcat7