android 中文 api (72) —— BluetoothSocket[蓝牙]
2010-12-22 08:55
453 查看
前言
本章内容是android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译。蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过inputstream和outputstream与远端程序进行通讯。版本为Android2.3r1,翻译来自中山大学的"AndroidClubSYSU",欢迎访问他们的WIKI:http://www.android-wiki.net,再次感谢"AndroidClubSYSU"!期待你一起参与AndroidAPI的中文翻译,联系我over140@gmail.com。
声明
欢迎转载,但请保留文章原始出处:)
AndroidClubSYSU:http://www.android-wiki.net
博客园:http://www.cnblogs.com/
Android中文翻译组:http://code.taobao.org/project/view/404/
正文
一、结构
publicfinalclassBluetoothSocketextendsObjectimplementsParcelable
java.lang.Object
android.bluetooth.BluetoothSocket
二、概述
已连接或连接到蓝牙套接字(socket)。
蓝牙端口监听接口和TCP端口类似:Socket和ServerSocket类。在服务器端,使用BluetoothServerSocket类来创建一个监听服务端口。当一个连接被BluetoothServerSocket所接受,它会返回一个新的BluetoothSocket来管理该连接。在客户端,使用一个单独的BluetoothSocket类去初始化一个外接连接和管理该连接。
最通常使用的蓝牙端口是RFCOMM,它是被AndroidAPI支持的类型。RFCOMM是一个面向连接,通过蓝牙模块进行的数据流传输方式,它也被称为串行端口规范(SerialPortProfile,SPP)。
为了创建一个BluetoothSocket去连接到一个已知设备,使用方法BluetoothDevice.createRfcommSocketToServiceRecord()。然后调用connect()方法去尝试一个面向远程设备的连接。这个调用将被阻塞指导一个连接已经建立或者该链接失效。
为了创建一个BluetoothSocket作为服务端(或者“主机”),查看BluetoothServerSocket文档。
每当该端口连接成功,无论它初始化为客户端,或者被接受作为服务器端,通过getInputStream()和getOutputStream()来打开IO流,从而获得各自的InputStream和OutputStream对象
BluetoothSocket类线程安全。特别的,close()方法总会马上放弃外界操作并关闭服务器端口。
注意:需要
参见
三、公共方法
publicvoidclose()
马上关闭该端口并且释放所有相关的资源。
在其它线程的该端口中引起阻塞,从而使系统马上抛出一个IO异常。
异常
IOException
publicvoidconnect()
尝试连接到远程设备。
该方法将阻塞,指导一个连接建立或者失效。如果该方法没有返回异常值,则该端口现在已经建立。
当设备查找正在进行的时候,创建对远程蓝牙设备的新连接不可被尝试。在蓝牙适配器上,设备查找是一个重量级过程,并且肯定会降低一个设备的连接。使用
异常
IOException一个错误,例如连接失败。
publicInputStreamgetInputStream()
通过连接的端口获得输入数据流
即使该端口未连接,该输入数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输入流
异常
IOException
publicOutputStreamgetOutputStream()
通过连接的端口获得输出数据流
即使该端口未连接,该输出数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输出流
异常
IOException
publicBluetoothDevicegetRemoteDevice()
获得该端口正在连接或者已经连接的远程设备。
返回值
远程设备
四、补充
文章精选
第四十一讲:Android蓝牙编程入门
Android蓝牙API之BluetoothSocket类 Android蓝牙API之BluetoothSocket类(2)
本章内容是android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译。蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过inputstream和outputstream与远端程序进行通讯。版本为Android2.3r1,翻译来自中山大学的"AndroidClubSYSU",欢迎访问他们的WIKI:
声明
欢迎转载,但请保留文章原始出处:)
AndroidClubSYSU:
博客园:
Android中文翻译组:
正文
一、结构
publicfinalclassBluetoothSocketextendsObjectimplementsParcelable
java.lang.Object
android.bluetooth.BluetoothSocket
二、概述
已连接或连接到蓝牙套接字(socket)。
蓝牙端口监听接口和TCP端口类似:Socket和ServerSocket类。在服务器端,使用BluetoothServerSocket类来创建一个监听服务端口。当一个连接被BluetoothServerSocket所接受,它会返回一个新的BluetoothSocket来管理该连接。在客户端,使用一个单独的BluetoothSocket类去初始化一个外接连接和管理该连接。
最通常使用的蓝牙端口是RFCOMM,它是被AndroidAPI支持的类型。RFCOMM是一个面向连接,通过蓝牙模块进行的数据流传输方式,它也被称为串行端口规范(SerialPortProfile,SPP)。
为了创建一个BluetoothSocket去连接到一个已知设备,使用方法BluetoothDevice.createRfcommSocketToServiceRecord()。然后调用connect()方法去尝试一个面向远程设备的连接。这个调用将被阻塞指导一个连接已经建立或者该链接失效。
为了创建一个BluetoothSocket作为服务端(或者“主机”),查看BluetoothServerSocket文档。
每当该端口连接成功,无论它初始化为客户端,或者被接受作为服务器端,通过getInputStream()和getOutputStream()来打开IO流,从而获得各自的InputStream和OutputStream对象
BluetoothSocket类线程安全。特别的,close()方法总会马上放弃外界操作并关闭服务器端口。
注意:需要
权限。BLUETOOTH
参见
BluetoothServerSocket
InputStream
OutputStream
三、公共方法
publicvoidclose()
马上关闭该端口并且释放所有相关的资源。
在其它线程的该端口中引起阻塞,从而使系统马上抛出一个IO异常。
异常
IOException
publicvoidconnect()
尝试连接到远程设备。
该方法将阻塞,指导一个连接建立或者失效。如果该方法没有返回异常值,则该端口现在已经建立。
当设备查找正在进行的时候,创建对远程蓝牙设备的新连接不可被尝试。在蓝牙适配器上,设备查找是一个重量级过程,并且肯定会降低一个设备的连接。使用
方法去取消一个外界的查询。查询并不由活动所管理,而作为一个系统服务来运行,所以即使它不能直接请求一个查询,应用程序也总会调用cancelDiscovery()
方法。cancelDiscovery()
方法可以用来放弃从另一线程而来的调用。close()
异常
IOException一个错误,例如连接失败。
publicInputStreamgetInputStream()
通过连接的端口获得输入数据流
即使该端口未连接,该输入数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输入流
异常
IOException
publicOutputStreamgetOutputStream()
通过连接的端口获得输出数据流
即使该端口未连接,该输出数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输出流
异常
IOException
publicBluetoothDevicegetRemoteDevice()
获得该端口正在连接或者已经连接的远程设备。
返回值
远程设备
四、补充
文章精选
相关文章推荐
- android 中文 api (72) —— BluetoothSocket[蓝牙]
- android 中文 api (72) —— BluetoothSocket[蓝牙]
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
- Android 中文API (65) —— BluetoothClass[蓝牙]
- Android 中文API (65) —— BluetoothClass[蓝牙]
- Android 中文API (65) —— BluetoothClass[蓝牙]
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
- Android 中文API (70) —— BluetoothDevice[蓝牙]
- Android 中文API (70) —— BluetoothDevice[蓝牙]
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
- Android蓝牙API之BluetoothSocket类(1)
- Android蓝牙API之BluetoothSocket类(2)
- Android 中文API (70) —— BluetoothDevice[蓝牙]
- 《Android 中文API (69) —— BluetoothAdapter[蓝牙]》
- Android 中文API (70) —— BluetoothDevice[蓝牙]
- Android蓝牙API之BluetoothAdapter类(1)
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址