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
随机推荐
- Windows上部署Redis
http://www.cnblogs.com/gaobing/p/5026136.html
- IOS自学笔记1——学前准备
函数的声明和定义: 在标准的C编译器中,定义的函数只能调用前面已经定义的函数.若在main()函数里要调用其他方法,这时得首先在main()上面声明要调用的函数,即函数的声明. C语言中,函数的声明和 ...
- java之适配器模式
interface Window { public void open(); public void close(); public void activated(); ...
- yii2.0 控制器方法 视图表单 Form表单处理
假设我们在ArticleController.php下面的actionForm方法中来处理提交的表单 新建立一个 views/Article/article-form.php文件用来作为输入表单 &l ...
- 为过程或函数sp_Adduser指定了过多的参数
前些天写用户注册模块,用存储过程添加用户,一开始就报“为过程或函数sp_Adduser指定了过多的参数”.仔细检查数据层的用户添加函数,结果在为存储过程添加sqlparameter参数的时候,数组给写 ...
- 《windows程序设计》学习_2.2:初识消息,双键的使用
/* 双键的使用 */ #include <windows.h> LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int WINAPI ...
- When Is Cheryl's Birthday
大早上起来逛微博,看见@西瓜大丸子汤Po的一个逻辑题,遂点开看之... 原文链接:http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.i ...
- tree(简单并差集)
tree Accepts: 156 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- poj 1961 Period(KMP训练指南例题)
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 11356 Accepted: 5279 Descripti ...
- javascript数字验证输入
javascript数字验证功能: <html> <body> <p>请输入数字.如果输入值不是数字,浏览器会弹出提示框.</p> <input ...