ArduinoBT Java serproxy COMMs

Dear Admin,,

Please can we add a forum dedicated to the ArduinoBT issues? Reasoning: Its been suggested before: Many ArduinoBT users have problems: existing open source software has not really been tested with the major BT stacks.

As a case in point: serproxy.exe was a candidate workaround for my Java rxtx problem, an issue with auto: connecting & disconnecting from a BT device, as reported in this earlier... Findings were: Unlike rxtx, serproxy does auto-connect, but does not echo text sent to the echo program on the ArduinoBT; and does not disconnect. So no workaround for rxtx as yet; a new serproxy issue found. Java code follows; note: It worked with non-BT aka arduino NG echo, so it may be useful to others needing serproxy i.e. connecting to a local com port from a remote host in Java. Hopefully I am not alone; other BT users out there will confirm this.

Kind Regards,



public class SocketEchoTest implements Runnable {

//private static final int SPEED = 115200; // setup baud in proxy config.

private OutputStream outputStream = null; private InputStream inputStream = null; private PrintWriter pw = null; private BufferedReader rd = null; private Socket sock; private int port = 5335;//// setup baud in proxy config.


public static void main(String[] args) { SocketEchoTest st = new SocketEchoTest(); String param = (args.length > 0) ? args[0] : null; boolean connected = st.connect(param); if (connected) { Thread xThread = new Thread(st); xThread.start(); try { xThread.join(); } catch (InterruptedException e) { } finally { st.close(); } System.exit(0); } System.exit(-1);


private boolean connect(String host) { // Create a socket with a timeout boolean success = false; if(host==null || host.length()==0) { host="localhost"; } try { InetAddress addr = InetAddress.getByName(host); SocketAddress sockaddr = new InetSocketAddress(addr, port); // Create an unbound socket sock = new Socket(); // This method will block no more than timeoutMs. // If the timeout occurs, SocketTimeoutException is thrown. int timeoutMs = 2000; // 2 seconds sock.connect(sockaddr, timeoutMs); inputStream = sock.getInputStream(); outputStream = sock.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream)); pw = new PrintWriter(bw); rd = new BufferedReader(new InputStreamReader(inputStream)); success = true;

} catch (Exception e) { e.printStackTrace(); } return success; } public void run() { try { println("1cha cha cha!"); println("2cha cha cha!!"); println("3cha cha cha!!!"); } catch (Throwable e) { System.out.println(e.getMessage()); e.printStackTrace(); System.exit(-1); } }

public void println(final String s) { pw.println(s); pw.flush(); try { Thread.sleep(100); } catch (InterruptedException e) { } read(); }

private void read() { try { char[] cArray = new char[60]; StringBuilder sb = new StringBuilder(); int cnt = 0; while (rd.ready() && (>0) { sb.append(new String(cArray).substring(0, cnt)); } System.out.println("Received '" + sb.toString() + "'"); } catch (IOException e) { e.printStackTrace(); } }

public void close() { try { pw.flush(); pw.close(); rd.close(); } catch (IOException e) { } finally { try { sock.close(); } catch (IOException e) { e.printStackTrace(); } } } }

Good news! I now have serproxy working with my ArduinoBT.
First I upgraded the bluesoleil driver - this may have fixed the close connection problem.
Then added a sleep() for 2 seconds before doing a read - bluetooth comms is sluggish.
As follows:

public void println(final String s) {
try {
Thread.sleep(2000); // ← wait 2secs
} catch (InterruptedException e) {}

And now it works. Connection through the COM port via serproxy, Yay!