Pracuję nad połączeniem Bluetooth rfcomm. W Próbce Androida jest wiersz, którego nie mogę zrozumieć i niestety nie mogłem znaleźć dobrej odpowiedzi w innych kwestiach i zasobach.Nie można zrozumieć mHandler.obtainMessage() w przykładzie Android Bluetooth
Oto cały kod:
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) { }
}
nie mogę zrozumieć ten wiersz:
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
mHandler
nie jest zdefiniowany w tym kodzie, a także MESSAGE_READ
nie mogę rozumiesz, co robi bytes
?
Myślę, że jak wspomniano w komentarzu, wysyła otrzymane bajty do aktywności, którą ustawiam jako moją główną aktywność. Czy mogę utworzyć Static TextView
w mojej głównej aktywności zamiast sendToTarget(), aby wyświetlić odebraną wiadomość?
mHandler jest prawdopodobnie Zmienna instancji, zadeklarowana gdzieś w klasie. MESSAGE_READ jest prawdopodobnie stałą, zdefiniowaną w klasie lub poprzez statyczny import. – assylias