【问题标题】:Bluetooth ASCII protocol蓝牙 ASCII 协议
【发布时间】:2011-04-30 02:24:06
【问题描述】:

我正在尝试与蓝牙设备通信。我在设备上的信息表明

“通信协议是 ASCII,用逗号分隔输出值。消息以回车和换行对终止。使用终端仿真器将这些结果保存为文件时,可以将这些结果读入 Excel 电子表格。”

如何从该设备发送和接收?我曾尝试使用 InputStreamReader 和 OutputStreamWriter,但我认为这不起作用。

编辑:

我正在尝试发送数据:

public void send(String s){
            try {
                writer.write(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

在哪里

try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            inStream = tmpIn;
            writer = new OutputStreamWriter(tmpOut);

您还可以看到我在哪里使用 inStream,它是一个简单的 InputStream。我也尝试过 InputStreamReader,但我只是得到了随机字符。使用 InputStream,无论我发送什么设备,我都只能读取 4 个字节,所以我不确定发送是否正常。

我应该使用什么?谢谢!

【问题讨论】:

  • 请详细说明。您尝试了哪些代码,遇到了哪些问题?包括任何结果或错误消息。
  • 我在我的问题中添加了更多细节,感谢您的调查!
  • 当有允许您指定编码的构造函数或方法时,切勿使用无编码构造函数或方法。如果您不指定编码,您将获得平台默认编码,这本质上是“随机编码”的委婉说法,导致代码依赖于平台。如果你的意思是 ASCII(只有 7 位),你应该指定它:new OutputStreamWriter(tmpOut, "ASCII"),但我怀疑它实际上是 ISO-8859-1 或其他一些 8 位编码。

标签: android bluetooth communication


【解决方案1】:

您应该查看有关 IO 流的 Java 文档以了解全部情况。

对于检索,我假设您使用的是InputStream.read() 方法,它一次读取一个字节。要一次检索多个字节,您应该使用byte[] 缓冲区。但这不是你的情况,仅供参考。

在您的情况下,您不需要使用InputStream 方法,而是使用InputStreamReader,因为 Reader 作用于字符,而不是字节。正如您在引用协议描述中所述,您有单独的 ASCII 行。在这种情况下BufferedReader 很方便,因为它有readLine() 方法。

所以你可以

    in = socket.getInputStream();
    InputStreamReader isr = new InputStreamReader(in);
    BufferedReader br = new BufferedReader(isr);

然后

    String line = br.readLine();

要发送数据,您应该使用 OutputStreamWriter。

记住:使用后请关闭流!!!在finaly{} 子句中

【讨论】:

    【解决方案2】:

    我正在跟进此事,以防其他人遇到同样的问题。我遇到的问题之一是我试图与之通信的设备期望 /n 和 /r 的特定顺序,如果不正确就会锁定,所以我不知道它是否在工作。

    这是我用于发送和接收的代码,我已经在几台设备上使用了它,它似乎运行良好。

        /**
     * This thread runs during a connection with a remote device.
     * It handles all incoming and outgoing transmissions.
     */
    private class ConnectedThread extends Thread {
        private final BluetoothSocket socket;
        private final InputStream inStream;
        private final OutputStream outStream;
        private final DataInputStream datIn;
    
        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            this.socket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
    
            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }
    
            inStream = tmpIn;
            outStream = tmpOut;
            datIn = new DataInputStream(inStream);
        }
    
        public void run() {
            Log.i(TAG, "BEGIN ConnectedThread");
            Bundle data = new Bundle();
    
            // Keep listening to the InputStream while connected
            while (true) {
                Log.i(TAG, "Reading...");
                try {
                    // Read from the InputStream
                    String results;
                    Log.i(TAG, "Recieved:");
                    results = datIn.readLine();
                    Log.i(TAG, results);
    
                 // Send the obtained bytes to the UI Activity
                    data.putString("results", results);
                    Message m = handler.obtainMessage(); // get a new message from the handler
                    m.setData(data); // add the data to the message
                    m.what = MESSAGE_READ;
                    handler.sendMessage(m);
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    handler.obtainMessage(MESSAGE_DISCONNECTED).sendToTarget();
                    setState(STATE_NONE);
                    // Start the service over to restart listening mode
                    break;
                }
            }
        }
    
        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                outStream.write(buffer);
                Log.i(TAG, "Sending: " + new String(buffer));
    
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }
    
        public void cancel() {
            try {
                socket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }
    
    /**
     * Write to the ConnectedThread in an unsynchronized manner
     * @param out The bytes to write
     * @see ConnectedThread#write(byte[])
     */
    public void send(byte[] out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (state != STATE_CONNECTED) return;
            r = connectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-06
      • 1970-01-01
      • 2014-01-19
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多