安卓藍牙API(6)

管理連接:
當你成功連接了兩個或多個設備的時候, 每個設備都將有一個連接的BluetoothSocket。這是有趣的開始,因爲你可以再設備之間分享數據了。使用BluetoothSocket,傳輸任意數據的大體步驟是簡單的:
1.得到 InputStream和 OutputStream ( handle transmissions through the socket,)相應地通過getInputStream() 和 getOutputStream()方法。
2. 讀寫數據: read(byte[]) and write(byte[]) .
注意的地方(就不翻譯了)
There are, of course, implementation details to consider. First and foremost, you should use a dedicated thread for all
stream reading and writing. This is important because both read(byte[]) and write(byte[]) methods are blocking
calls. read(byte[]) will block until there is something to read from the stream. write(byte[]) does not usually block,
but can block for flow control if the remote device is not calling read(byte[]) quickly enough and the intermediate
buffers are full. So, your main loop in the thread should be dedicated to reading from the InputStream. A separate
public method in the thread can be used to initiate writes to the OutputStream.

private class ConnectedThread extends Thread {
 private final BluetoothSocket  mmSocket;
 private final InputStream mmInStream;
 private final OutputStream mmOutStream;
 public ConnectedThread(BluetoothSocket  socket) {
 mmSocket = socket;
 InputStream tmpIn = null ;
 OutputStream tmpOut = null ;
 // Get the input and output streams, using temp objects because
 // member streams are final
 try {
 tmpIn = socket. getInputStream();
 tmpOut = socket. getOutputStream();
 } catch (IOException e) { }
 mmInStream = tmpIn;
 mmOutStream = tmpOut;
 }
 public void run() {
 byte[]  buffer = new byte[1024];  // buffer store for the stream
 int  bytes; // bytes returned from read()
 // Keep listening to the InputStream until an exception occurs
 while (true) {
 try {
 // Read from the InputStream
 bytes = mmInStream. read(buffer);
 // Send the obtained bytes to the UI activity
 mHandler. obtainMessage(MESSAGE_READ,  bytes, -1,  buffer)
 . sendToTarget();
 } catch (IOException e) {
 break;
 }
 }
 }
 /* Call this from the main activity to send data to the remote device */
 public void write(byte[]  bytes) {
 try {
 mmOutStream. write(bytes);
 } catch (IOException e) { }
 }
 /* Call this from the main activity to shutdown the connection */
 public void cancel() {
 try {
 mmSocket. close();
 } catch (IOException e) { }
 }
}

The constructor acquires the necessary streams and once executed, the thread will wait for data to come through the
InputStream. When read(byte[]) returns with bytes from the stream, the data is sent to the main activity using a
member Handler from the parent class. Then it goes back and waits for more bytes from the stream.
Sending outgoing data is as simple as calling the thread’s write() method from the main activity and passing in the
bytes to be sent. This method then simply calls write(byte[]) to send the data to the remote device.
The thread’s cancel() method is important so that the connection can be terminated at any time by closing the
BluetoothSocket. This should always be called when you’re done using the Bluetooth connection.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章