前言

  本章内容是  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

    博客园:http://www.cnblogs.com/

    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权限。

参见

BluetoothSocket

  三、公共方法    

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[蓝牙]的更多相关文章

  1. Android 中文API (69) —— BluetoothAdapter[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...

  2. Android 中文API (70) —— BluetoothDevice[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android  2.3 ...

  3. Android 中文API (65) —— BluetoothClass[蓝牙]

    前言 本章内容是android.bluetooth.BluetoothClass,为Android蓝牙部分的章节翻译.用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法 ...

  4. Android 中文API (68) —— BluetoothClass.Service

    前言 本章内容是 android.bluetooth.BluetoothClass.Service,为Android蓝牙部分的章节翻译,版本为 Android 2.3   r1,翻译来自中山大学的&q ...

  5. Android 中文API (67) —— BluetoothClass.Device.Major

    前言 本章内容是android.bluetooth.BluetoothClass.Device.Major,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学 ...

  6. Android 中文API (66) —— BluetoothClass.Device

    前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学的" ...

  7. Android 中文 API (40) —— RatingBar

    Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...

  8. android中文api(79)——Gallery

    前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www. ...

  9. Android中文API

    Android中文API http://www.android-doc.com/index.html

随机推荐

  1. String字符串需要掌握的几个问题

    一.两种定义方式的区别: String str = "hello";      先在堆内存中查找是否已经有"hello",若有,将str指向已存在的它即可:若堆 ...

  2. 页面的拼装配置Appache SSI

    O(∩_∩)O哈哈哈~今天又遇到了一个好东西,记录一下下哈~ 一个网站,有些页面的头部跟尾部是相同的,还在为总是复制,为错了一个小小点而需要改好多个页面烦恼吗?现在我知道怎么来偷懒了! 1.首先找到c ...

  3. C#学习日志 day 3 ------ 基本语句示例

    写c#首先需要知道的就是数据类型,这里是所有c#中的所有数据类型以及说明.

  4. fiddler--手机https

    1.访问fiddler的主讲加端口,比如我的是:http://192.168.1.103:8080 在点击fiddlerroot  certificate 下载安装证书即可

  5. 批量添加target属性

    <script> addTarget(); function addTarget(){ var oa=document.getElementsByTagName('a'); for(var ...

  6. 阿里云ECS每天一件事D7:安装tomcat8.0

    这一D,跨越了几个月啊,人是越来越懒,集中写一些,就懒得再记录了.也是因为测试需要,搭建个jsp的服务环境,只是测试,考虑用tomcat就够了. 在Tomcat官网下载最新Core版本,下载之后,将文 ...

  7. 阿里云ECS每天一件事D5:安装php5.4.34

    原本是想把php和nginx合在一起来说的,不过考虑后,还是分开来做吧,已熟悉的更透彻一些. 1.准备类库 yum install autoconf automake libtool re2c fle ...

  8. SSD常见问题的技术分析

    AHCI对性能的影响 AHCI,全称Advanced Host Controller Interface,即高级主机控制器接口,是一种相比老旧的“IDE虚拟模式”更适合新一代SATA存储设备通信的协议 ...

  9. xsoup,Jsoup

    Xsoup 0.2.0 Xsoup 的详细介绍:请点这里 Xsoup 的下载地址:请点这里 https://github.com/code4craft/xsoup http://www.oschina ...

  10. 自己定义标签中tagsupport的一些方法

    TagSupport生命周期 TagSupport类分别实现了Tag与IterationTag界面,其预设的回传值是:doStartTag()回传 SKIP_BODY.EVAL_BODY_INCLUD ...