S
Schütz12
Neues Mitglied
- 0
Hallo leute,
Ich verzweifle schon richtig bei meiner APP
Ich versuche über Bluetooth Daten zu verschicken.
Dabei verwende ich einen eigenen Thread, weiß nur nicht ob ich den richtig verwende
Hier mein Code:
Dies ist meine Klasse für das senden und empfangen von Daten. Könnte jemand mal drübersehen und sagen was ich falsch mache?
Danke vielmals.
Ich verzweifle schon richtig bei meiner APP
Ich versuche über Bluetooth Daten zu verschicken.
Dabei verwende ich einen eigenen Thread, weiß nur nicht ob ich den richtig verwende
Hier mein Code:
Code:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
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;
Runnable d = new Runnable() {
@Override
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
bluetoothIn.obtainMessage(handlerState, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
};
Thread data = new Thread(d);
data.start();
}
public void sendText(String text){
byte[] toSend;
toSend=text.getBytes();
write(toSend);
}
/* 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) { }
}
}
}
Dies ist meine Klasse für das senden und empfangen von Daten. Könnte jemand mal drübersehen und sagen was ich falsch mache?
Danke vielmals.