Managing a Connection
When you have successfully connected two (or more) devices, each one will have a connectedBluetoothSocket
.
This is where the fun begins because you can share data between devices. Using theBluetoothSocket
,
the general procedure to transfer arbitrary data is simple:
- Get the
InputStream
andOutputStream
that
handle transmissions through the socket, viagetInputStream()
andgetOutputStream()
,
respectively.
- Read and write data to the streams with
read(byte[])
andwrite(byte[])
.
That's it.
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 bothread(byte[])
andwrite(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 callingread(byte[])
quickly enough and the intermediate
buffers are full. So, your main loop in the thread should be dedicated to reading from theInputStream
.
A separate public method in the thread can be used to initiate writes to theOutputStream
.
Example
Here's an example of how this might look:
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. Whenread(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'swrite()
method from the main Activity and passing in the bytes to be sent. This method then simply callswrite(byte[])
to
send the data to the remote device.
The thread'scancel()
method is important so that the connection can be terminated at any time by closing theBluetoothSocket
.
This should always be called when you're done using the Bluetooth connection.
分享到:
相关推荐
delphi-android-bluetooth-printer-sample-master
Android蓝牙开发框架! !
Android-5.0-Bluetooth-HCI-Reqs
Android Bluetooth Simulator What is it It's a tcp-based implementation of part of the android bluetooth API. As for now, you can communicate between different emulators using the RFComm protocol, you ...
支持RCSP协议的固件,支持以下系列芯片 | AC693X, AC697X, AC695X等等 |最后列新... |- 杰理OTA外接库(Android)开发文档 --- 在线开发文档 |- 杰理之家SDK(Android)开发文档 --- 在线开发文档 |- libs -- 核心库<br
1.深入了解Android蓝牙Bluetooth——《基础篇》 2.深入了解Android蓝牙Bluetooth——《进级篇》 2.深入了解Android蓝牙Bluetooth——《总结篇》 欢迎入群 QQ交流群 :232203809 微信公众号:终端研发部。加入我们...
Introducing Bluetooth LE Audio book form Bluetooth SIG
详见http://blog.csdn.net/huaxun66/article/details/53516916
android_Bluetooth_ssp-BLE,一个可以选择spp或者BLE连接的数据交换app
基于Android的BlueTooth开发手机蓝牙和蓝牙模块通讯,
FAQ_Bluetooth-----5737343740.pdf MTK FAQ 文档 讲诉 蓝牙的一些 问题 修改
[14本经典Android开发教程]-3-Android SDK 中文开发文档 什么是 Android? Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta版的 Android SDK 提供了在Android 平台上...
Android-Simple-Bluetooth-Example Simple Android Bluetooth example to turn on/off radio and to view and connect with devices. Has associated code to connect to an Arduino. For a complete tutorial write...
PCB parts for systems constructuion MCP1802T
BlueTooth-AutoPair-自动匹配.zip
android bluetooth框架知识整理
Arduino-android-bluetooth-serial.zip,android的一个库,用于简化通过蓝牙的基本串行通信,例如在与arduinos通信时。android-bluetooth-serial,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,...
这是本人在研究安卓蓝牙模块的时候,自己整理的bluetooth框架相关知识点,android顶起。
Bluetooth-UART-Board使用说明,主要介绍了这一型号的各个参数及使用
Bluetooth在android中的实现和开发