Mam problem z przesylem tekstu pomiedzy dwoma urzadzeniami (w j2me) przez bluetooth. Moj program na poczatku wyszukuje urzadzenia, nastepnie uslugi. Z uslug tworzy liste z ktorej mozna wybrac pod ktora sie podlaczyc (jedna z komend to umozliwia). Niestety pierwsza linia w kodzie wykonania dla tej komendy:
notifier = (StreamConnectionNotifier) Connector.open(url);
rzuca wyjatek (Class Cast Exception). Prawdopodobnie nie mozna rzutowac Connector-a na StreamConnectionNotifier. Staram sie znalezc rozwiazanie ktore umozliwi zczytywanie tekstu przesylanego na moje urzadzenie z innych. Chodzi mi o otwarcie input stream i zczytywanie byte'ow ... czy jest to mozliwie?
z gory dzieki
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
(many imports)
import javax.microedition.midlet.*;
/**
* @author bart
*/
public class btMidlet extends MIDlet implements CommandListener, DiscoveryListener, Runnable {
private Alert al = new Alert("title", "text", null, AlertType.INFO);
private List list = new List("Devices:", List.IMPLICIT);
private Vector devices = new Vector();
private Vector records = new Vector();
private Command chooseDevice = new Command("info", Command.OK, 0);
private Command connectDevice = new Command("connect", Command.OK, 0);
private Command viewLogs = new Command("view logs", Command.OK, 0);
private boolean inqCompleted = false;
private int servSearchCode;
private UUID RFCOMM_UUID = new UUID(0x0003);
private StreamConnection con = null;
private StreamConnectionNotifier notifier = null;
public btMidlet() {
al.setTimeout(Alert.FOREVER);
list.addCommand(chooseDevice);
list.addCommand(connectDevice);
list.addCommand(viewLogs);
list.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
try {
int id = list.getSelectedIndex();
ServiceRecord btRecord = (ServiceRecord) records.elementAt(id);
String url = btRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
String friendlyName = btRecord.getHostDevice().getFriendlyName(true);
if (c == chooseDevice) {
al.setTitle("Service info");
al.setString(
"friendly name: " + friendlyName +
", url: " + url);
} else if (c == connectDevice) {
notifier = (StreamConnectionNotifier) Connector.open(url);
con = notifier.acceptAndOpen();
InputStream is = con.openInputStream();
// READING DATA FROM INPUT STREAM
} else if (c == viewLogs) {
getDisplay().setCurrent(al);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Display getDisplay() {
return Display.getDisplay(this);
}
public void startApp() {
this.run();
getDisplay().setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass dc) {
if (devices.indexOf(btDevice) == -1) {
System.out.println("found: " + btDevice.getBluetoothAddress());
devices.addElement(btDevice);
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
records.addElement(servRecord[i]);
}
}
public void serviceSearchCompleted(int transID, int respCode) {
synchronized (this) {
servSearchCode = respCode;
notify();
}
}
public void inquiryCompleted(int discType) {
synchronized (this) {
inqCompleted = true;
notify();
}
}
public synchronized void run() {
try {
DiscoveryAgent discoveryAgent = null;
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
while (!inqCompleted) {
wait();
}
for (int i = 0; i < devices.size(); i++) {
RemoteDevice device = (RemoteDevice) devices.elementAt(i);
UUID[] uuidSet = new UUID[1];
uuidSet[0] = RFCOMM_UUID;
discoveryAgent.searchServices(null, uuidSet, device, this);
wait();
}
String url = null;
for (int i = 0; i < records.size(); i++) {
ServiceRecord btrec = (ServiceRecord) records.elementAt(i);
url = btrec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
list.append(url, null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}