前言

  本章内容是   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. WebApi服务

    WCF 它利用TCP.HTTP.MSMQ等传输协议构建“契约先行”的服务.WCF最初为基于SOAP的服务而设计[xml],繁琐.冗余.慢.沉重 WebApi 基于http协议,轻量级的,支持URL路由 ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. Linux例行工作crontab

    第一步编辑要定时执行的脚本: myScript.sh myScript.sh的内容为:touch /root/`date +%F' '%T`.txt 为myScript.sh增加可执行权限:chmod ...

  4. 只有小于65535端口编程可以用,查看哪些端口被打开netstat -anp,nc命令,nmap命令

    1024以下是系统保留的,从1024-65535是用户使用的 个人写的应用程序,尽量不要使用0到1024之间的端口号. 1024到65535我们编程可以用.这个不是Linux规定的,是socket规定 ...

  5. 一个关于Integer的秘密

    先让大家看一段简单的代码: public static voidmain(String[] args) { Integera = 1; Integerb = 1; Integerc = 222; In ...

  6. 通过浏览器直接打开Android应用程序

    需求 通过手机浏览器直接打开Android应用程序.假设本地已经安装了指定Android应用,就直接打开它:假设没有安装,则直接下载该应用的安装文件(也能够跳转到下载页面). 实现效果 假设手机上已经 ...

  7. python邮件发送脚本

    转自:http://phinecos.cnblogs.com/ #!/usr/bin/python #coding=utf-8 #@author:dengyike #@date:2010-09-28 ...

  8. C# 关于数据类型转换

    1.强制转换; 需要注意的一个是char不能强制转化成int,如果使用强制转化,得到的是原整数的ASCII码值.该方式对于浮点数会做无条件舍去,失去精确度. 2.利用方法: 类型.Parse(stri ...

  9. 优化器的使用oracle ---explain plan

    如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...

  10. css版hover现边框

    需要注意的是  hover中要给盒子加:position:relative; <style type="text/css"> *{margin:0;padding:0; ...