Witam !
Mam taki problem, chciałem napisac prosta aplikacje na androida w której serwer bedzie wysyłał wiadomość po kliknieciu w button a klient bedzie ja odbierał niestety coś nie działa, mógłby ktoś spojrzeć.
Oto kod:
SERVER
public class Server extends Thread {
private final BluetoothServerSocket mmServerSocket;
public Server() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp = null;
try {
UUID uuid = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Usługa witająca", uuid);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
PrintWriter out;
BluetoothSocket socket = null;
public void run() {
Log.d("INFO", "Uruchamiam serwer");
try {
Log.d("INFO", "Czekam na połączenie od clienta");
socket = mmServerSocket.accept();
out = new PrintWriter(socket.getOutputStream(), true);
Log.d("INFO", "Mam clienta!");
send("Czesc");
} catch (IOException e) {
}
}
public void send(String wiadomosc) {
try {
out.println(wiadomosc);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
CLIENT
public Client(BluetoothDevice device, TextView tv) {
this.tv = tv;
BluetoothSocket tmp = null;
mmDevice = device;
ClientRun cr;
try {
UUID uuid = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (Exception e) {
}
mmSocket = tmp;
try {
} catch (Exception e) {
}
}
public void run() {
while (true) {
try {
mmSocket.connect();
Log.d("INFO", "Polaczono z serwerem!");
in = new BufferedReader(new InputStreamReader(mmSocket.getInputStream()));
Log.d("INFO", "Przed readem");
input = in.readLine();
Log.d("INFO", input);
mmSocket.close();
} catch (Exception e) {
}
}
}
}