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
随机推荐
- Hibernate学习之hibernate执行顺序
Hibernate 执行的顺序如下: (1) 生成一个事务的对象,并标记当前的 Session 处于事务状态(注:此时并未启动数据库级事务). (2) 应用使用 s.save 保存对象,这个时候 ...
- VC++学习之GDI概述
VC++学习之GDI概述 图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏 ...
- leetcode 31. Next Permutation(字典序的下一个)
描述: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- PhpStorm 10.0 激活方式
随着 JetBrains 新版本的发布,注册机已然不行了.然而,道高一尺,魔高一丈.IntelliJ IDEA开源社区 提供了如下通用激活方法:注册时选择License server填写http:// ...
- 字符串-06. IP地址转换
/* * Main.c * D6-字符串-06. IP地址转换 * Created on: 2014年8月19日 *******测试通过******** *转载:http://blog.csdn.ne ...
- SSE && WebSockets
SSE && WebSockets 参考 http://www.bitscn.com/school/HTMLCSS/201402/194940.html WebSockets 定义了一 ...
- android和Vitamio使用比较
在开始接触udp组播的时候先使用的Vitamio,播放时候声音卡顿 画面也会出现卡顿,后来又使用了VLC,画面挺好,,但是声音卡顿.最后不断测试发现是由于设备底层驱动处理视频部分有问题,导致程序播出的 ...
- 假设给Contact的List加一个用字母排序的导航
效果图: 这样写Layout: <? xml version="1.0" encoding="utf-8"? > <LinearLayout ...
- 新浪微博开放平台开发-android客户端(1)【转】
http://www.cnblogs.com/virusswb/archive/2011/08/05/2128941.html 最近不是太忙,花了一些时间学习android的应用开发.经过两个星期的学 ...
- EF 如何code first
首先配置连接数据.sql server <connectionStrings> <add name="Model1" connectionString=" ...