Hallo Micha
Danke für die Antwort. Ich glaube, dass das Modul wirklich mit dem Handy
verbunden ist. Weil das Handy hat schon gefragt ob die Verbindung mit
dem Bluenicecom zuverlässig ist. Und ich habe auch schon mal probiert
das Handy mit einem PC zu verbinden und es hat geklappt.
Hier ist der Code für Kommunikation:
//get URL from funktion servicesDiscovered
ServiceRecord rec = serviceRecord[i];
String url =
rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE _NOENCRYPT,
false);
//bluetooth connect
private void btConnect(final String url){
StreamConnection stream;
try {
stream = (StreamConnection) Connector.open(url);
DataOutputStream out = stream.openOutputStream();
InputStream in = stream.openInputStream();
startWriteThread(out);
startReadThread(in);
} catch (IOException ex) {
ex.printStackTrace();
}
}
ich glaube bis jetzt sind alles richtig. Wenn der Satz "stream
=(StreamConection) Conector.open(url)" durchgeführt wird, fragt das
Handy ob die Verbindung zuverlässig ist.
Hier sind zwei Thread für Read und Write:
StartWriteThread ist noch ok. Aber StartReadThread ist abstürzt bei dem
Satz "in.read".
private void startWriteThread(final OutputStream out) {
Thread writer = new Thread() {
public void run() {
// semaphor available
available = true;
try {
cl_frm.append("write start.\n");
String str = "h";
byte[] byte_arr = str.getBytes();
out.write(byte_arr);
out.flush();
cl_frm.append("write finish\n");
available = false;
} catch (Exception e) {
Alert alert_btConnect_1 = new Alert("Problem","btConnect
write stream error",null, AlertType.INFO);
alert_btConnect_1.setTimeout(5000);
cl_display.setCurrent(alert_btConnect_1);
} finally {
cl_frm.append("Bluetooth write stream closed.");
try {
out.close();
} catch (IOException e) {
Alert alert_btConnect_2 = new
Alert("Problem","btConnect write stream close error",null,
AlertType.INFO);
alert_btConnect_2.setTimeout(5000);
cl_display.setCurrent(alert_btConnect_2);
}
}
}
};
writer.start();
}
private void startReadThread(final InputStream in) {
Thread reader = new Thread() {
public void run() {
while(available == true){}
try {
cl_frm.append("i do try\n");
byte[] byte_arr = null;
int i;
while(true){
cl_frm.append("i do the while\n");
!!!!!!!!// Das Problem ist hier!!!!!!! in.read() abstürzt.
try{in.read();} catch (IOException ex)
{cl_frm.append("can't read");break;}
// int ia = in.read(); // geht auch nicht
// cl_frm.append("get"+ia+" \n");
// if (ia == -1) {
// cl_frm.append("Stop!!!");
//// break;
// }
}
available = false;
} catch (Throwable e) {
Alert alert_btConnect_1 = new Alert("Problem","btConnect
read stream error",null, AlertType.INFO);
alert_btConnect_1.setTimeout(5000);
cl_display.setCurrent(alert_btConnect_1);
} finally {
cl_frm.append("Bluetooth read stream closed.");
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Alert alert_btConnect_2 = new Alert("Problem","btConnect
read stream close error",null, AlertType.INFO);
alert_btConnect_2.setTimeout(5000);
cl_display.setCurrent(alert_btConnect_2);
}
}
};
reader.start();
}
}
Micha, weißt du vielleicht wo das Problem ist?
Gruß und vielen Dank im voraus!
Tim