前言

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

android 中文 api (72) —— BluetoothSocket[蓝牙]的更多相关文章

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

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

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

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

  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. leetcode Climbing Stairs python

    class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...

  2. Canvas 雾玻璃

    Demo http://lumixraku.github.io/demos/Fog/Fog.html Canvas tutorial 给大家安利一个学习Canvas的站点 http://www.htm ...

  3. grunt api 文档

    Grunt docs Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. 安装 grunt-cli npm install grunt-cli -g 注 ...

  4. ONFI闪存数据通道接口标准

    早期的闪存产品每个厂家的设计标准各有不同,会碰到各种各样的问题,特别是到了06年之后,闪存产业市场需求开始发力,造成了迫切需要一个统一的标准来改变这个问题. 2007年1月,以英特尔,镁光,海力士,意 ...

  5. android 背景透明度渐变动画

    button.setVisibility(View.VISIBLE); // 背景透明度渐变动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(butto ...

  6. Javascript 思维导图

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  7. VS2010/MFC对话框:颜色对话框

    颜色对话框 在上一节中为大家讲解了字体对话框的使用方法,熟悉了字体对话框,本节继续讲另一种通用对话框--颜色对话框. 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来 ...

  8. Android常用控件之ExpandableList的使用

    先来看下什么是ExpandableListView 跟列表有点像,这种是可以折叠的列表,下面来看下是如何在代码中实现 一.在布局文件中声明一个ExpandableListView <Linear ...

  9. HDU1695-GCD(数论-欧拉函数-容斥)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. CreateFileMapping使用方法

    CreateFileMapping的MSDN翻译和使用心得   測试创建和打开文件映射的时候老是得到"句柄无效"的错误, 细致看了MSDN以后才发觉是函数认识不透, 这里把相关的解 ...