BlueCove with Bluesoleil problems with sending data back from mobile.
843790Feb 5 2009 — edited Feb 5 2009Hello.
I can establish connection with my mobile phone (Motorola L2) using dongle USB Bluetooth with the newst Bluesoleil antenna and send some data there. But when I am trying to receive data from mobile it never works, lenght is always (-1). I am not sure if it is not fault in com port, which may just support one way. Anyway mobile does not tell me any error when sending data and I think it must be something with configuration, I cannot figure it out. Can anyone help me ?
here is mobile (server) class:
package bluetooth;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.bluetooth.*;
import javax.microedition.io.*;
import java.io.*;
/**
*
* @author dijef
*/
public class SerwerBT implements Runnable {
private LocalDevice urzadzenie;
private ServiceRecord usluga;
private StreamConnectionNotifier powiadamiacz;
private StreamConnection pol = null;
private BluetoothMIDlet m;
private boolean maDzialac = true;
private static final int ATRYBUT_WERSJI = 0x3232;
private static final UUID UUID_SERWERA = new UUID("ABCDEF12345678998877665544332211", false);
private static final String NAZWA = "SERWER_KRYPT";
public SerwerBT(BluetoothMIDlet m) {
this.m = m;
Thread w = new Thread(this);
w.start();
}
public void run() {
try {
urzadzenie = LocalDevice.getLocalDevice();
urzadzenie.setDiscoverable(DiscoveryAgent.GIAC);
String url = "btspp://localhost:" + UUID_SERWERA.toString() + ";name=" + NAZWA + ";authorize=false";
powiadamiacz = (StreamConnectionNotifier)Connector.open(url);
usluga = urzadzenie.getRecord(powiadamiacz);
DataElement parametr = new DataElement(DataElement.INT_1, 1);
usluga.setAttributeValue(ATRYBUT_WERSJI, parametr);
} catch (Exception e) {
e.printStackTrace();
this.koniec();
m.notifyDestroyed();
}
while (maDzialac) {
try {
pol = powiadamiacz.acceptAndOpen();
String dane = this.wczytaj();
//dodane
m.pokazNaSerwerze(dane);
String rezultat = this.rot13(dane);
//zmienione z rezultat aby ominac rot13
this.wyslij(dane);
m.pokazNaSerwerze("Wyslane");
pol.close();
pol = null;
} catch (Exception e) {
e.printStackTrace();
this.koniec();
m.notifyDestroyed();
}
}
}
public String wczytaj() throws Exception {
InputStream wej = pol.openInputStream();
int dlugosc = wej.read();
byte[] bufor = new byte[dlugosc];
int licznik = 0;
while (licznik < dlugosc) {
int liczba = wej.read(bufor, licznik, dlugosc - licznik);
licznik += liczba;
}
wej.close();
return new String(bufor, "ISO-8859-1");
}
public String rot13(String dane) {
String wynik = new String(dane);
for (int i=0; i<dane.length(); i++)
{
wynik = wynik.substring(0, i) + zamien13(wynik.charAt(i)) + wynik.substring(i+1, wynik.length());
}
return wynik;
}
public char zamien13(char ch) {
int kod = (byte)ch;
if (kod>=97 && kod<=122)
{
kod += 13;
if (kod>122) kod = kod - 26;
}
else if (kod>=65 && kod<=90)
{
kod += 13;
if (kod>90) kod = kod - 26;
}
else kod = 32;
return (char)kod;
}
public void wyslij(String dane) throws Exception {
try {
OutputStream out = pol.openOutputStream();
//2 lijnijki dodane przeze mnie
byte dlugosc = (byte)dane.length();
out.write(dlugosc);
out.write(dane.getBytes("ISO-8859-1"));
out.flush();
out.close();
} catch (IOException e) {
m.pokazNaSerwerze("Can't send data: " + e);
}
}
public void koniec() {
if (powiadamiacz != null)
{
try
{
maDzialac = false;
if (pol != null) pol.close();
powiadamiacz.close();
} catch (Exception e) {}
}
}
}