【问题标题】:Check what kind of content represent received bytes检查什么样的内容代表接收到的字节
【发布时间】: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 只是一个例子。

【问题讨论】:

标签: android io bluetooth android-bluetooth iobluetooth


【解决方案1】:

两种方式(无限的方式)

One:第一个字节是类型。无论您发送什么,都是一种标题。

二:用类型字段序列化一个对象。甚至,也许,一个层次结构或......更好......当你想发送文本时写字符串

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2014-05-07
    • 2013-10-21
    • 2014-07-05
    相关资源
    最近更新 更多