android 中文 api (71) —— BluetoothServerSocket[蓝牙]
前言
本章内容是 android.bluetooth.BluetoothServerSocket,为Android蓝牙部分的章节翻译。服务器通讯套接字,与TCP ServerSocket类似。版本为 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 BluetoothServerSocket extends Object implements Closeable
java.lang.Object
android.bluetooth.BluetoothServerSocket
二、概述
一个蓝牙监听端口。
蓝牙端口监听接口和TCP端口类似:Socket和ServerSocket类。在服务器端,使用BluetoothServerSocket类来创建一个 监听服务端口。当一个连接被BluetoothServerSocket所接受,它会返回一个新的BluetoothSocket来管理该连接。在客户 端,使用一个单独的BluetoothSocket类去初始化一个外接连接和管理该连接。
最通常使用的蓝牙端口是RFCOMM,它是被Android API支持的类型。RFCOMM是一个面向连接,通过蓝牙模块进行的数据流传输方式,它也被称为串行端口规范(Serial Port Profile,SPP)。
为了创建一个对准备好的新来的连接去进行监听BluetoothServerSocket
类,使用 BluetoothAdapter.listenUsingRfcommWithServiceRecord()
方法。然后调用accept()
方法去监 听该链接的请求。在连接建立之前,该调用会被阻断,也就是说,它将返回一个BluetoothSocket
类去管理该连接。每次获得该类之后,如果不再需 要接受连接,最好调用在BluetoothServerSocket
类下的close()
方法。关闭BluetoothServerSocket
类不会关 闭这个已经返回的BluetoothSocket
类。
BluetoothSocket
类线程安全。特别的,close()
方法总会马上放弃外界操作并关闭服务器端口。
注意:需要BLUETOOTH
权限。
参见
三、公共方法
public BluetoothSocketaccept (int timeout)
阻塞直到超时时间内的连接建立。
在一个成功建立的连接上返回一个已连接的BluetoothSocket类。
每当该调用返回的时候,它可以在此调用去接收以后新来的连接。
close()
方法可以用来放弃从另一线程来的调用。
参数
timeout (译者注:阻塞超时时间)
返回值
已连接的 BluetoothSocket
异常
IOException 出现错误,比如该调用被放弃,或者超时。
public BluetoothSocket accept ()
阻塞直到一个连接已经建立。(译者注:默认超时时间设置为-1,见源码)
在一个成功建立的连接上返回一个已连接的BluetoothSocket类。
每当该调用返回的时候,它可以在此调用去接收以后新来的连接。
close()
方法可以用来放弃从另一线程来的调用。
返回值
已连接的 BluetoothSocket
异常
IOException 出现错误,比如该调用被放弃,或者超时。
public void close ()
马上关闭端口,并释放所有相关的资源。
在其他线程的该端口中引起阻塞,从而使系统马上抛出一个IO异常。
关闭BluetoothServerSocket不会关闭接受自accept()的任意BluetoothSocket。
异常
IOException
android 中文 api (71) —— BluetoothServerSocket[蓝牙]的更多相关文章
- Android 中文API (69) —— BluetoothAdapter[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...
- Android 中文API (70) —— BluetoothDevice[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android 2.3 ...
- 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
随机推荐
- 安卓里面JSON处理和JAVA SE里面的JSON包
今天编译安卓项目遇到这个问题 com.android.dex.DexException: Multiple dex files define的解决办法 大致意思就是引用了 相同的包 在JAVA SE里 ...
- windows下搭建python+selenium环境
1.安装python https://www.python.org/ 2.安装setuptools(python的基础包工具) 下载地址:https://pypi.python.org/pypi/se ...
- Android TextWatcher应用实例
(1)使用TextWathcer限制输入字符个数布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中 ...
- VM 映像
让我们一起欢呼吧!随着最近Microsoft Azure运行时的发布,我们非常高兴地宣布发布 OS映像的继承性产品:新 VM映像.等一下-有些人可能会觉得这听起来有点耳熟.没错,一个月前在旧金山 ...
- nefu 462 fib组合
nefu 462 fib组合 (斐波那契数列的通项公式以及推倒过程) 分类: 数学2014-05-21 10:27 190人阅读 评论(0) 收藏 举报 题目链接:http://acm.nefu.ed ...
- android UI进阶之用【转】
android UI进阶之用ViewPager实现欢迎引导页面 摘要: ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ...
- 反射,得到Type引用的三种方式
1.使用System.Object.GetType()得到Type引用 使用一个SportsCar实例得到类型信息 SportsCar sc=new SportsCar(); Type t=sc.G ...
- SQL Server索引进阶:第五级,包含列
原文地址: Stairway to SQL Server Indexes: Level 5, Included Columns 本文是SQL Server索引进阶系列(Stairway to SQL ...
- HTML元素的ID和Name属性的区别
HTML元素的ID和Name属性的区别今天突然兴致来了,想深究下这两属性的具体区别最classical的答案:ID就像是一个人的身份证号码,而Name就像是他的名字,ID显然是唯一的,而Name是可以 ...
- ##DAY1 UI、frame、center、bounds、UIVIew
##DAY1 UI.frame.center.bounds.UIVIew #pragma mark ———————UI——————————— UI的本意是用户界面,是英文User和 Interface ...