Hello everybody. I am new in programming Java and using XCODE.
I am trying to make a code that can access to the Bluetooth of my macbook pro as a serial port. I am trying to compile this example. If you have another simplest example I would apreciate you give me some link.
For now, I have this code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
/* The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"/dev/tty.HC-06-DevB", /
};
/*
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
*/
private BufferedReader input;
/* The output stream to the port */
private OutputStream output;
/* Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/* Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
/
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
/
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
/
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
/
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
/
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/*
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
/*
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/
}
public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
Thread t=new Thread() {
public void run() {
/
/
try {Thread.sleep(10000);} catch (InterruptedException ie) {}
}
};
System.out.println("Started");
t.start();
t.join();
main.close();
System.out.println("Stopped");
}
}
I tried to compile this from the terminal and I had this error
Humbertos-MacBook-Pro:~ humbertoriveiro$ cd Documents/OneDrive/ManoRobotica/BTtest/BTTEST
Humbertos-MacBook-Pro:BTTEST humbertoriveiro$ javac SerialTest.java
SerialTest.java:5: package gnu.io does not exist
import gnu.io.CommPortIdentifier;
^
SerialTest.java:6: package gnu.io does not exist
import gnu.io.SerialPort;
^
SerialTest.java:7: package gnu.io does not exist
import gnu.io.SerialPortEvent;
^
SerialTest.java:8: package gnu.io does not exist
import gnu.io.SerialPortEventListener;
^
SerialTest.java:12: cannot find symbol
symbol: class SerialPortEventListener
public class SerialTest implements SerialPortEventListener {
^
SerialTest.java:13: cannot find symbol
symbol : class SerialPort
location: class smartOfficeJava.SerialTest
SerialPort serialPort;
^
SerialTest.java:88: cannot find symbol
symbol : class SerialPortEvent
location: class smartOfficeJava.SerialTest
public synchronized void serialEvent(SerialPortEvent oEvent) {
^
SerialTest.java:33: cannot find symbol
symbol : class CommPortIdentifier
location: class smartOfficeJava.SerialTest
CommPortIdentifier portId = null;
^
SerialTest.java:34: cannot find symbol
symbol : variable CommPortIdentifier
location: class smartOfficeJava.SerialTest
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
^
SerialTest.java:38: cannot find symbol
symbol : class CommPortIdentifier
location: class smartOfficeJava.SerialTest
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
^
SerialTest.java:38: cannot find symbol
symbol : class CommPortIdentifier
location: class smartOfficeJava.SerialTest
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
^
SerialTest.java:53: cannot find symbol
symbol : class SerialPort
location: class smartOfficeJava.SerialTest
serialPort = (SerialPort) portId.open(this.getClass().getName(),
^
SerialTest.java:58: cannot find symbol
symbol : variable SerialPort
location: class smartOfficeJava.SerialTest
SerialPort.DATABITS_8,
^
SerialTest.java:59: cannot find symbol
symbol : variable SerialPort
location: class smartOfficeJava.SerialTest
SerialPort.STOPBITS_1,
^
SerialTest.java:60: cannot find symbol
symbol : variable SerialPort
location: class smartOfficeJava.SerialTest
SerialPort.PARITY_NONE);
^
SerialTest.java:89: cannot find symbol
symbol : variable SerialPortEvent
location: class smartOfficeJava.SerialTest
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
^
16 errors
----------------------
I think I have to add those headers but I dont know where they are and how to add them.
Help please!