Hi, I'm trying to write a program in Java to send AT commands to my mobile and recive information via BlueT from it. I've already got a program to search and fide blueT Devices, and thats how i receve:
1.name of device
2.blueTooth Adress
How to use this two things to send AT commands, what should I use ?

Thx for help.

This is what I have right now.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Vector;

import javax.bluetooth.ServiceRecord;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class ATSend {

	public static void main(String[] args) throws IOException,
InterruptedException {
		String url="btgoep://0017E510DEC8:4;authenticate=false;encrypt=false;master=false";
		if (url != null)
		{
			System.out.println("Working with URL: " + url);

			// open a connection to the device
			StreamConnection connection = (StreamConnection) Connector.open(url);
			// Send/receive data
			String msg = "AT+CBC;\r";
			OutputStream os = connection.openOutputStream();
			InputStream is = connection.openInputStream();
			// wait some time for the AT interpreter to be loaded on the phone
			System.out.println("waiting...");
			Thread.sleep(3000);
			// send data to the server
			os.write(msg.getBytes());
			os.flush();
			System.out.println("Sent: " + msg.toString());
			// read data from the server
			String theString = null;
			try {
				theString = convertStreamToString(is);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  /* HANGS!*/
			System.out.println("Received: " + theString);
			is.close();
			os.close();
			connection.close();
		}
		else
			System.out.println("No valid URL found, aborting...");
    }

	public static String convertStreamToString(InputStream ins) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {   /* HANGS HERE after done */
      sb.append(line + "\n");
    }
    ins.close();
    return sb.toString();
}
}

OUTCOME:

Working with URL: btspp://localhost:00112233445566778899AABBCCDDEEFF;name=serialconn
BlueCove version 2.1.1-SNAPSHOT on winsock
Exception in thread "main" java.lang.ClassCastException: com.intel.bluetooth.BluetoothRFCommConnectionNotifier cannot be cast to javax.microedition.io.StreamConnection
at ATSend.main(ATSend.java:22)
BlueCove stack shutdown completed

I found this code and changed it a bit.

Edited 5 Years Ago by hatebin: one line more

You can check it on Wiki. What I would like to know is why can't I cast Connecgor on StreamConnection.
Thanks.

This article has been dead for over six months. Start a new discussion instead.