Witam, tworze program, który ma za zadanie połączyć się z modułem btm-222 podłączonym do ATmegi i sterować nią. Program na mikrokontrolerze jest napisany poprawnie, bo gdy przy pomocy programu BlueTerm wysyłam znak to odpowiada poprawnie. Program na telefon piszę w AndroidStudio. Jak na razie umiem się połączyć z modułem jako client, ale nie potrafię nic wysłać. Szukałem w internecie różnych rozwiązań ale nic nie pomaga. Czy mógłby mi ktoś podpowiedzieć jak stworzyć klasę lub funkcję wysyłającą dane (najlepiej w string lub int). Od razu przydałaby się funkcja do odbierania danych. Zaznaczam, że nie jestem biegły w programowaniu w Java. Przesyłam również klasę do łączenia się z modułem:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.UUID;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
public class ClientBluetooth extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ClientBluetooth(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
tmp = device.createRfcommSocketToServiceRecord(uuid);
} catch (Exception e) { }
mmSocket = tmp;
}
public void run() {
try {
Log.d("INFO","proba polaczenia....");
mmSocket.connect();
Log.d("INFO","polaczono z serwerem!");
BufferedReader in = new BufferedReader(new InputStreamReader(mmSocket.getInputStream()));
String input = in.readLine();
Log.d("INFO","Serwer mowi: "+input);
} catch (Exception ce) {
try {
mmSocket.close();
} catch (Exception cle) { }
}
}
}
A to klasa za pomocą której próbowałem wysyłać dane:
public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//private final BluetoothDevice mmDevice;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
//mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}