android 中文 api (72) —— BluetoothSocket[蓝牙]
前言
本章内容是 android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译。蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过inputstream和outputstream与远端程序进行通讯。版本为 Android 2.3 r1,翻译来自中山大学的"Android Club SYSU",欢迎访问他们的WIKI:http://www.android-wiki.net,再次感谢"Android Club SYSU"!期待你一起参与Android API 的中文翻译,联系我 over140@gmail.com。
声明
欢迎转载,但请保留文章原始出处:)
Android Club SYSU:http://www.android-wiki.net
Android中文翻译组:http://code.taobao.org/project/view/404/
正文
一、结构
public final class BluetoothSocket extends Object implements Parcelable
java.lang.Object
android.bluetooth.BluetoothSocket
二、概述
已连接或连接到蓝牙套接字(socket)。
蓝牙端口监听接口和TCP端口类似:Socket和ServerSocket类。在服务器端,使用BluetoothServerSocket类来创建一个 监听服务端口。当一个连接被BluetoothServerSocket所接受,它会返回一个新的BluetoothSocket来管理该连接。在客户 端,使用一个单独的BluetoothSocket类去初始化一个外接连接和管理该连接。
最通常使用的蓝牙端口是RFCOMM,它是被Android API支持的类型。RFCOMM是一个面向连接,通过蓝牙模块进行的数据流传输方式,它也被称为串行端口规范(Serial Port Profile,SPP)。
为了创建一个BluetoothSocket去连接到一个已知设备,使用方法 BluetoothDevice.createRfcommSocketToServiceRecord()。然后调用connect()方法去尝试一个 面向远程设备的连接。这个调用将被阻塞指导一个连接已经建立或者该链接失效。
为了创建一个BluetoothSocket作为服务端(或者“主机”),查看BluetoothServerSocket文档。
每当该端口连接成功,无论它初始化为客户端,或者被接受作为服务器端,通过getInputStream()和getOutputStream()来打开IO流,从而获得各自的InputStream和OutputStream对象
BluetoothSocket类线程安全。特别的,close()方法总会马上放弃外界操作并关闭服务器端口。
注意:需要BLUETOOTH
权限。
参见
三、公共方法
public void close ()
马上关闭该端口并且释放所有相关的资源。
在其它线程的该端口中引起阻塞,从而使系统马上抛出一个IO异常。
异常
IOException
public void connect ()
尝试连接到远程设备。
该方法将阻塞,指导一个连接建立或者失效。如果该方法没有返回异常值,则该端口现在已经建立。
当设备查找正在进行的时候,创建对远程蓝牙设备的新连接不可被尝试。在蓝牙适配器上,设备查找是一个重量级过程,并且肯定会降低一个设备的连接。使用 cancelDiscovery()
方法去取消一个外界的查询。查询并不由活动所管理,而作为一个系统服务来运行,所以即使它不能直接请求一个查询,应用 程序也总会调用cancelDiscovery()
方法。
close()
方法可以用来放弃从另一线程而来的调用。
异常
IOException 一个错误,例如连接失败。
public InputStream getInputStream ()
通过连接的端口获得输入数据流
即使该端口未连接,该输入数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输入流
异常
IOException
public OutputStream getOutputStream ()
通过连接的端口获得输出数据流
即使该端口未连接,该输出数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。
返回值
输出流
异常
IOException
public BluetoothDevice getRemoteDevice ()
获得该端口正在连接或者已经连接的远程设备。
返回值
远程设备
四、补充
文章精选
Android蓝牙API之BluetoothSocket类(2)
android 中文 api (72) —— BluetoothSocket[蓝牙]的更多相关文章
- Android 中文API (70) —— BluetoothDevice[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android 2.3 ...
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...
- Android 中文API (65) —— BluetoothClass[蓝牙]
前言 本章内容是android.bluetooth.BluetoothClass,为Android蓝牙部分的章节翻译.用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法 ...
- Android 中文API (68) —— BluetoothClass.Service
前言 本章内容是 android.bluetooth.BluetoothClass.Service,为Android蓝牙部分的章节翻译,版本为 Android 2.3 r1,翻译来自中山大学的&q ...
- Android 中文API (67) —— BluetoothClass.Device.Major
前言 本章内容是android.bluetooth.BluetoothClass.Device.Major,为Android蓝牙部分的章节翻译,版本为Android 2.3 r1,翻译来自中山大学 ...
- Android 中文API (66) —— BluetoothClass.Device
前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3 r1,翻译来自中山大学的" ...
- Android 中文 API (40) —— RatingBar
Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...
- android中文api(79)——Gallery
前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www. ...
- Android中文API
Android中文API http://www.android-doc.com/index.html
随机推荐
- 《JavaScript+DOM编程艺术》的摘要(三)---图片库实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- Python 标识符
Python 标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以 ...
- java 构造方法 constructor demo笔记
demo 地址 http://pan.baidu.com/s/1bo2FG1T package com.ws.study; /** * @author Administrator * */ publi ...
- php基础知识总结
PHP 代表 PHP: Hypertext Preprocessor PHP 文件可包含文本.HTML.JavaScript代码和 PHP 代码 PHP 代码在服务器上执行,结果以纯 HTML 形式返 ...
- 【Perl学习笔记】1.perl的ref 函数
perl有引用的概念:一组数据实际上是另一组数据的引用.这些引用称为指针,第一组数据中存放的是第二组数据的头地址.引用的方式被用得相当普遍,特别是在面向对象的模块.函数的参数传递等常见.但perl对每 ...
- 向前辈致敬 strspn
把8位的CHAR型数据分解为:前5位和后3位,这样2^5 = 32个CHAR型数+值就可表示所有的CHAR型数据 这样做的好处:在给出子串后,不用比较256次,最多比较32次即可判断出是否一个数在子串 ...
- HUD 4473 Exam
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4473 题目意思 定义f(x) = 满足(a * b)|x的有序对(a,b)的个数. 然后输入一个n, ...
- JAVA学习第一课(初识JAVA)
PS:退ACM集训队了,自己也疯玩了两天,后天就开学了,就正式大二了,该收收心好好学习啦 ...
- Android二维码开源项目zxing用例简化和生成二维码、条形码
上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...
- Objective-C中NSArray和NSMutableArray的基本用法
/*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...