Android: Bluetooth - How zum Lesen von eingehenden Daten
Habe ich erfolgreich Pairing und Verbindung mit einem Bluetooth-Gerät. Ich bin jetzt daran interessiert, den Empfang aller übertragenen Daten zwischen 2 und zu sehen, was ist, was.
Bin ich immer der der input-stream aus der Steckdose und versucht es zu Lesen. Ich zurück und melden es.
Ich weiß nur einen Weg, dies zu tun, von dem, was ich gelesen habe ist nur nicht Lesen mit einem byte-Puffer int zurückgeben. Aber ich sollte haben jede Menge Daten kommen durch. Wie kann ich mich kontinuierlich Auslesen Daten übertragen werden, und auch format als Byte statt int.
Dank.
Vollständigen code weiter unten:
public class ConnectThread {
private BluetoothSocketWrapper bluetoothSocket;
private BluetoothDevice device;
private boolean secure;
private BluetoothAdapter adapter;
private List<UUID> uuidCandidates;
private int candidate;
/**
* @param device the device
* @param secure if connection should be done via a secure socket
* @param adapter the Android BT adapter
* @param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used
*/
public ConnectThread(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
List<UUID> uuidCandidates) {
this.device = device;
this.secure = secure;
this.adapter = adapter;
this.uuidCandidates = uuidCandidates;
if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
this.uuidCandidates = new ArrayList<UUID>();
this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
}
public BluetoothSocketWrapper connect() throws IOException {
boolean success = false;
while (selectSocket()) {
adapter.cancelDiscovery();
try {
bluetoothSocket.connect();
success = true;
break;
} catch (IOException e) {
//try the fallback
try {
bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket());
Thread.sleep(500);
bluetoothSocket.connect();
success = true;
break;
} catch (FallbackException e1) {
Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
} catch (InterruptedException e1) {
Log.w("BT", e1.getMessage(), e1);
} catch (IOException e1) {
Log.w("BT", "Fallback failed. Cancelling.", e1);
}
}
}
if (!success) {
throw new IOException("Could not connect to device: "+ device.getAddress());
}
receiveData(bluetoothSocket);
return bluetoothSocket;
}
private boolean selectSocket() throws IOException {
if (candidate >= uuidCandidates.size()) {
return false;
}
BluetoothSocket tmp;
UUID uuid = uuidCandidates.get(candidate++);
Log.i("BT", "Attempting to connect to Protocol: "+ uuid);
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(uuid);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
}
bluetoothSocket = new NativeBluetoothSocket(tmp);
return true;
}
public static interface BluetoothSocketWrapper {
InputStream getInputStream() throws IOException;
OutputStream getOutputStream() throws IOException;
String getRemoteDeviceName();
void connect() throws IOException;
String getRemoteDeviceAddress();
void close() throws IOException;
BluetoothSocket getUnderlyingSocket();
}
public static class NativeBluetoothSocket implements BluetoothSocketWrapper {
private BluetoothSocket socket;
public NativeBluetoothSocket(BluetoothSocket tmp) {
this.socket = tmp;
}
@Override
public InputStream getInputStream() throws IOException {
return socket.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
@Override
public String getRemoteDeviceName() {
return socket.getRemoteDevice().getName();
}
@Override
public void connect() throws IOException {
socket.connect();
}
@Override
public String getRemoteDeviceAddress() {
return socket.getRemoteDevice().getAddress();
}
@Override
public void close() throws IOException {
socket.close();
}
@Override
public BluetoothSocket getUnderlyingSocket() {
return socket;
}
}
public class FallbackBluetoothSocket extends NativeBluetoothSocket {
private BluetoothSocket fallbackSocket;
public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException {
super(tmp);
try
{
Class<?> clazz = tmp.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[] {Integer.valueOf(1)};
fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
}
catch (Exception e)
{
throw new FallbackException(e);
}
}
@Override
public InputStream getInputStream() throws IOException {
return fallbackSocket.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return fallbackSocket.getOutputStream();
}
@Override
public void connect() throws IOException {
fallbackSocket.connect();
}
@Override
public void close() throws IOException {
fallbackSocket.close();
}
}
public static class FallbackException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public FallbackException(Exception e) {
super(e);
}
}
public void sendData(BluetoothSocketWrapper socket, int data) throws IOException{
ByteArrayOutputStream output = new ByteArrayOutputStream(4);
output.write(data);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(output.toByteArray());
}
public int receiveData(BluetoothSocketWrapper socket) throws IOException{
byte[] buffer = new byte[256];
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
InputStream inputStream = socket.getInputStream();
inputStream.read(buffer);
return input.read();
}
}
Du musst angemeldet sein, um einen Kommentar abzugeben.
In den ersten Platz, stoppen Sie die Verwendung
ByteArrayInputStream
undByteArrayOutputStream
für mehr Kontrolle.Wenn der socket sendet/empfängt, text, dies zu tun.
ZU SENDEN:
ZU ERHALTEN:
Den
socketOutputStream
sollte IhrbluetoothSocket.getOutputStream()
.Wenn der socket sendet/empfängt große Lasten von Daten, der Schlüssel ist die while-Schleife zu verhindern, dass out-of-memory-Ausnahmen. Die Daten werden gelesen von Blöcken (z.B. alle 4-KB-Puffer-Größe), wenn Sie die Größe des Puffers, sollten Sie die heap-Größe, wenn Sie live-streaming-Medien, betrachten die Latenz und die Qualität auch.
ZU SENDEN:
ZU ERHALTEN:
FAQ:
largeDataInputStream
undlargeDataOutputStream
? Diese streams können normale I/O-streamsFileInputStream
/FileOutputStream
oder etc.BluetoothSocket
nie endet? Die Buchse input kontinuierlich Daten empfängt, und dieread()
Methoden blockiert sich, bis die Daten erkannt werden. Um zu verhindern, dass die Blockierung der code in dieser Zeile while-Schleife muss gebrochen werden.HINWEIS: Diese Antwort könnte eine Bearbeiten. Ich bin kein Englisch-Muttersprachler.
Folgenden obige Ratschläge, ich bin jetzt mit diesem code zum abrufen von Daten.