前言

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

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

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

 参见

BluetoothServerSocket

     InputStream

     OutputStream

  三、公共方法

public void close ()

马上关闭该端口并且释放所有相关的资源。

在其它线程的该端口中引起阻塞,从而使系统马上抛出一个IO异常。

异常

IOException

public void connect ()

尝试连接到远程设备。

该方法将阻塞,指导一个连接建立或者失效。如果该方法没有返回异常值,则该端口现在已经建立。

当设备查找正在进行的时候,创建对远程蓝牙设备的新连接不可被尝试。在蓝牙适配器上,设备查找是一个重量级过程,并且肯定会降低一个设备的连接。使用cancelDiscovery()方法去取消一个外界的查询。查询并不由活动所管理,而作为一个系统服务来运行,所以即使它不能直接请求一个查询,应用 程序也总会调用cancelDiscovery()方法。

close()方法可以用来放弃从另一线程而来的调用。

异常

IOException     一个错误,例如连接失败。

public InputStream getInputStream ()

通过连接的端口获得输入数据流

即使该端口未连接,该输入数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。

返回值

输入流

异常

IOException

public OutputStream getOutputStream ()

通过连接的端口获得输出数据流

即使该端口未连接,该输出数据流也会返回。不过在该数据流上的操作将抛出异常,直到相关的连接已经建立。

返回值

输出流

异常

IOException

public BluetoothDevice getRemoteDevice ()

获得该端口正在连接或者已经连接的远程设备。

返回值

远程设备

  四、补充

  文章精选

       第四十一讲:Android蓝牙编程入门

      Android蓝牙API之BluetoothSocket类

      Android蓝牙API之BluetoothSocket类(2)

转载:http://www.cnblogs.com/over140/archive/2010/12/22/1913353.html

android 中文 api (72) —— BluetoothSocket[蓝牙]相关推荐

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

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

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

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

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

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

  4. Android 中文API (94) —— MediaController

    前言 本章内容是android.widget.MediaController,版本为Android 2.3 r1,翻译来自"唐明",再次感谢"唐明" !期待你一 ...

  5. Android 中文 API 文档 (45) —— AbsoluteLayout.LayoutParams

    前言 本章内容是 android.widget.AbsoluteLayout.LayoutParams,版本为Android 2.2 r1,翻译来自"绵白糖",再次感谢" ...

  6. Android 中文API (92) —— MenuInflater

    前言 本章内容是android.view.MenuInflater,版本为Android 2.3 r1,翻译来自"獨鍆躌踄",欢迎大家访问他的博客:http://www.cnblo ...

  7. Android 中文 API (90) —— WindowManager

    一.结构 public interface WindowManager extends android.view.ViewManager android.view.WindowManager 二.概述 ...

  8. Android中文API(127) —— MessageQueue

    结构 继承关系 public class MessageQueue extends Object java.lang.Object android.os.MessageQueue 类概述 保存消息列表 ...

  9. android 中文 API (41) —— RatingBar.OnRatingBarChangeListener

    前言 本章内容是 android.widget.RatingBar.OnRatingBarChangeListener,版本为Android 2.2 r1,翻译来自madgoat,欢迎大家访问他的博客 ...

  10. Android 中文API (91) —— GestureDetector

    前言 本章内容是android.view.GestureDetector,版本为Android 2.3 r1,翻译来自"Haiya 胡蝶",再次感谢"Haiya 胡蝶&q ...

最新文章

  1. flask secure_filename 不能识别中文名的解决办法
  2. ES transport client批量导入
  3. C语言遥控器程序,红外遥控
  4. wxWidgets:wxDirDialog类用法
  5. WinDbg配置与下载
  6. WebMagic爬取58同城租房数据
  7. python类中引用数据是通过_重载python类中的[]运算符以引用数字阵列数据内存
  8. iOS开发过程中常见错误问题及解决方案
  9. python创建txt文件_Mac怎么创建txt文件?教你设置新建txt的快捷键
  10. connection对象的参数
  11. 2021-08-24自然语言处理预训练模型的研究综述 - 知网论文
  12. 数据库——添加外键约束
  13. 剪辑师:入门级无水印无片头片尾免费视频剪辑工具
  14. AirTrest连接模拟器和安卓手机
  15. “VBE6EXT.OLB不能被加载”解决方法
  16. Spring Cloud 工具集
  17. Typora下载和使用
  18. 2019-07-12 函数递归、二分法、匿名函数、三元表达式、内置函数
  19. 【超级炫酷的旋转特效——html实现(附源代码)】
  20. QT读写Sqlite数据库的三种方式

热门文章

  1. python -lambda表达式的用法
  2. 【LeetCode】200. 岛屿的个数
  3. 版本控制--搭建 GitLab 服务器
  4. mysql-数据库操作
  5. jquery检测浏览器类型
  6. swift:高级运算符(位运算符、溢出运算符、优先级和结合性、运算符重载函数)...
  7. 实例变量和静态变量(或类变量static)
  8. rails开发随手记-0
  9. Table城市代码翻译
  10. 主流浏览器和内核及Web标准