2011-04-30 60 views
0

我正在尝试与蓝牙设备进行通信。我在设备上的信息表明:蓝牙ASCII协议

“通信协议是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个字节,所以我不确定发送是否正常。

我应该使用什么?谢谢!

+0

请详细说明。你尝试过哪些代码,以及遇到什么问题?包含任何结果或错误消息。 – 2011-04-30 02:26:05

+0

我在我的问题中增加了更多细节,感谢您的关注! – Matt 2011-04-30 02:40:12

+0

如果存在允许指定编码的编码,则不要使用无编码构造函数或方法。如果你没有指定编码,你将得到平台默认编码,这实质上是“随机编码”的委婉说法,导致依赖平台的代码。如果你确实是指ASCII码(只有7位),你应该指定它:'新的OutputStreamWriter(tmpOut,“ASCII”)',但我怀疑它实际上是'ISO-8859-1'或其他一些8位编码。 – 2011-05-03 15:33:06

回答

0

我正在跟进此情况,以防其他人遇到同样的问题。我遇到的其中一个问题是,我试图与之通信的设备期望特定顺序的/ 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); 
} 
0

您应该查看IO Streams上的Java文档以制作完整的照片。

对于检索,我假设您正在使用InputStream.read()方法,该方法一次读取一个字节。要一次检索几个字节,您应该使用byte[]缓冲区。但那不是你的情况,只是FYI。

你的情况,你不需要使用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{}子句