【发布时间】:2015-06-17 20:32:05
【问题描述】:
我的Bluetooth 应用程序也可以发送图像,但我无法检查内容的类型,如果接收到的字节来自图像,则字节必须适用于ImageView,如果接收到的字节来自文本,字节必须显示在Toast.makeText().show()
private class StreamThread extends Thread {
public StreamThread() {
}
public void run() {
byte[] buffer = new byte[1000000];
int bytes;
while (true) {
try {
bytes = bluetoothIn.read(buffer);
//Received bytes can represent text or image
//if (hereSomeTypeDetector=="image/*") {
// update the UI and show the image
//} else if (hereSomeTypeDetector=="text/plain") {
// show a toast
//}
} catch (IOException e) {
Log.e(TAG, "Unable to receive bytes");
break;
}
}
}
public void write(byte[] data) {
try {
bluetoothOut.write(data);
} catch (IOException e) {
Log.e(TAG, "Unable to send bytes");
}
}
}
我的问题是,我需要检测接收字节的内容类型,使用一些代码来代替hereSomeTypeDetector?
注意:
这里的SomeTypeDetector 只是一个例子。
【问题讨论】:
-
对于初学者来说,您的缓冲区对于大多数图像来说都太小了。然后,你可以使用developer.android.com/reference/java/net/…它在URLConnection中,但是可以用于任何InputStream。
标签: android io bluetooth android-bluetooth iobluetooth