分类:google android应用开发2010-08-11
23:171486人阅读收藏举报
当你成功地连接了两台(或多台)设备时,每个设备都有一个已连接的BluetoothSocket。这时你可以在设备之间共享数据,乐趣才刚开始。 使用BluetoothSocket,传输二进制数据的过程是简单的:
- 分别通过getInputStream()和getOutputStream()获得管理数据传输的InputStream和OutputStream。
- 通过read(byte[])和write(byte[])从流中读取或写入数据。
首先,你必须使用一个线程专门用于数据的读或写。这是非常重要的,因为read(byte[])和write(byte[])方法都是阻塞调用。read(byte[])将会阻塞到流中有数据可读。write(byte[])一般不会阻塞,但当远程设备的中间缓冲区已满而对方没有及时地调用read(byte[])时将会一直阻塞。所以,你的线程中的主循环将一直用于从InputStream中读取数据。 A separate public method in the thread can be used to initiate
writes to theOutputStream
.
Example
-
privateclassConnectedThreadextendsThread{
-
privatefinalBluetoothSocketmmSocket;
-
privatefinalInputStreammmInStream;
-
privatefinalOutputStreammmOutStream;
-
publicConnectedThread(BluetoothSocketsocket){
-
mmSocket=socket;
-
InputStreamtmpIn=null;
-
OutputStreamtmpOut=null;
-
-
-
try{
-
tmpIn=socket.getInputStream();
-
tmpOut=socket.getOutputStream();
-
}catch(IOExceptione){}
-
mmInStream=tmpIn;
-
mmOutStream=tmpOut;
-
}
-
publicvoidrun(){
-
byte[]buffer=newbyte[1024];
-
intbytes;
-
-
while(true){
-
try{
-
-
bytes=mmInStream.read(buffer);
-
-
mHandler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
-
}catch(IOExceptione){
-
break;
-
}
-
}
-
}
-
-
publicvoidwrite(byte[]bytes){
-
try{
-
mmOutStream.write(bytes);
-
}catch(IOExceptione){}
-
}
-
-
publicvoidcancel(){
-
try{
-
mmSocket.close();
-
}catch(IOExceptione){}
-
}}
当你成功地连接了两台(或多台)设备时,每个设备都有一个已连接的BluetoothSocket。这时你可以在设备之间共享数据,乐趣才刚开始。 使用BluetoothSocket,传输二进制数据的过程是简单的:
- 分别通过getInputStream()和getOutputStream()获得管理数据传输的InputStream和OutputStream。
- 通过read(byte[])和write(byte[])从流中读取或写入数据。
首先,你必须使用一个线程专门用于数据的读或写。这是非常重要的,因为read(byte[])和write(byte[])方法都是阻塞调用。read(byte[])将会阻塞到流中有数据可读。write(byte[])一般不会阻塞,但当远程设备的中间缓冲区已满而对方没有及时地调用read(byte[])时将会一直阻塞。所以,你的线程中的主循环将一直用于从InputStream中读取数据。 A separate public method in the thread can be used to initiate
writes to theOutputStream
.
Example
-
privateclassConnectedThreadextendsThread{
-
privatefinalBluetoothSocketmmSocket;
-
privatefinalInputStreammmInStream;
-
privatefinalOutputStreammmOutStream;
-
publicConnectedThread(BluetoothSocketsocket){
-
mmSocket=socket;
-
InputStreamtmpIn=null;
-
OutputStreamtmpOut=null;
-
-
-
try{
-
tmpIn=socket.getInputStream();
-
tmpOut=socket.getOutputStream();
-
}catch(IOExceptione){}
-
mmInStream=tmpIn;
-
mmOutStream=tmpOut;
-
}
-
publicvoidrun(){
-
byte[]buffer=newbyte[1024];
-
intbytes;
-
-
while(true){
-
try{
-
-
bytes=mmInStream.read(buffer);
-
-
mHandler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
-
}catch(IOExceptione){
-
break;
-
}
-
}
-
}
-
-
publicvoidwrite(byte[]bytes){
-
try{
-
mmOutStream.write(bytes);
-
}catch(IOExceptione){}
-
}
-
-
publicvoidcancel(){
-
try{
-
mmSocket.close();
-
}catch(IOExceptione){}
-
}}
分享到:
相关推荐
Android蓝牙开发框架! !
基于Android的BlueTooth开发手机蓝牙和蓝牙模块通讯,
该用例是AndroidBLE蓝牙连接小设备的搜索,连接以及读取的相关用例。 如果还不对BLEandroid蓝牙的认识以及API有相关的认识:,那么请访问我的博客: http://blog.csdn.net/androidstarjack/article/details/60468468...
delphi-android-bluetooth-printer-sample-master
Android-5.0-Bluetooth-HCI-Reqs
可直接在HBuilderX 上运行,亲测可用。实现蓝牙状态实时监控,数据的接收和发送。外加Android原生蓝牙实现源码 + 串口调试工具
支持RCSP协议的固件,支持以下系列芯片 | AC693X, AC697X, AC695X等等 |最后列新... |- 杰理OTA外接库(Android)开发文档 --- 在线开发文档 |- 杰理之家SDK(Android)开发文档 --- 在线开发文档 |- libs -- 核心库<br
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 ...
node-bluetooth-obd, 用于与蓝牙OBD连接器通信的node 软件包 蓝牙 obd Bluetooth的蓝牙通信 for 。这个 node 模块允许你通过蓝牙串行端口与带有 Node.js.的obd ELM327连接器通信限制仅在ELM327设备上测试。还没有...
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...
安卓Android源码——Bluetooth.rar
该库允许您轻松地为具有一台服务器和一台客户端的两台 Android 设备创建套接字蓝牙连接。 该库与 Android SDK 2.3 到 5.1 兼容。 有关文档和其他信息,请参阅。 对于多个蓝牙连接,您可以看到这个库。 下载 下载...
android_Bluetooth_ssp-BLE,一个可以选择spp或者BLE连接的数据交换app
[14本经典Android开发教程]-3-Android SDK 中文开发文档 什么是 Android? Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta版的 Android SDK 提供了在Android 平台上...
Introducing Bluetooth LE Audio book form Bluetooth SIG
精品--本科毕业设计:蓝牙防丢器Bluetooth anti-lost App ——可自行设定报警dBm&报警距离
S60平台Bluetooth API开发伙伴指南——蓝牙的结构概述.doc
3.2 Android 与蓝牙协议栈的Bluetooth 之HCI层分析 .4.1HCI层与基带的通信方 4.2包的分析及研究 4.3通信过程的研究与分析 五Bluetooth之编程实现 5.1 HCI昙编程 5.2 L2CAP层编程 5.3 SDP层编程 六Bluetooth 之启动...
android-multiple-bluetooth-connections Android手机可以使用Bluetooth Low Engery或Bluetooth Classic同时与多个蓝牙设备进行通信。 蓝牙经典此代码旨在使用三种服务与三种蓝牙经典设备进行通信。 MyService1....