安卓蓝牙中文翻译帮助文档.pdf_第1页
安卓蓝牙中文翻译帮助文档.pdf_第2页
安卓蓝牙中文翻译帮助文档.pdf_第3页
安卓蓝牙中文翻译帮助文档.pdf_第4页
安卓蓝牙中文翻译帮助文档.pdf_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

安卓蓝牙开发帮助文档 安卓平台包含了支持蓝牙网络的软件栈 这使得安卓设备可以和其他的蓝牙设备进行无线 数据交换 通过安卓蓝牙的API 函数 应用程序框架提供了访问蓝牙的功能 这些API 使得 应用程序可以无线连接到其他的蓝牙设备 并且支持点对点和点对多点 运用蓝牙API 一个安卓应用程序可以做下面的事 1 扫描其他蓝牙设备 2 查询局部 周围 区域的蓝牙适配器来进行蓝牙设备之间的配对 3 建立RFCOMM 通信通道 4 通过服务发现连接到其他设备 5 和其他设备之间互传数据 6 管理多个设备的连接 蓝牙权限 为了在你的应用程序里面使用蓝牙 你必须声明至少 2 条蓝牙权限中的一条 这 2条权限分 别是 BLUETOOTH 和BLUETOOTH ADMIN 为了执行任何的蓝牙通信动作 例如请求连接 接受连接 传送数据等 你必须声明 BLUETOOTH 这个权限 为了初始化蓝牙设备发现和管理蓝牙设置等 你必须声明 BLUETOOTH ADMIN 这个权限 如果只是需要发现局部区域的蓝牙设备 大多数应用程序只需声明这个权限就可以了 这个 权限的其他能力一般是不用的 除非这个应用程序是 power manger 电源管理的 它要根据 用户需求进行更改蓝牙设置 需要注意的是 如果你使用了 BLUETOOTH ADMIN 这个权限 那就必须还有使用BLUETOOTH 这个权限 建立蓝牙 在你的应用程序通过蓝牙通信之前 你应该先确认你的设备是支持蓝牙的 如果是这样还 要确保它是可用的 如果你的设备不支持蓝牙 很遗憾你不能使用任何蓝牙的功能 如果你的设备支持蓝牙 但 是不可用 这个时候在不离开你的应用程序的前提下你可以请求用户使能蓝牙 这个操作通 过BluetoothAdapter 只需要2步 1 获取BluetoothAdapter 蓝牙适配器 BluetoothAdapter mBluetoothAdapter BluetoothAdapter getDefaultAdapter if mBluetoothAdapter null Device does not support Bluetooth 2 使能蓝牙 if mBluetoothAdapter isEnabled Intent enableBtIntent new Intent BluetoothAdapter ACTION REQUEST ENABLE startActivityForResult enableBtIntent REQUEST ENABLE BT 查找设备 通过 BluetoothAdapter 使用设备发现能力或者查询已经配对 绑定 的蓝牙设备 你就可 以找到远程的蓝牙设备 周围的蓝牙设备 设备发现是一个扫描的过程 它包括搜索周围区域内使能了蓝牙的设备然后请求每个设备 的一些信息 这个有时候叫做 discovering inqureing scanning 而在这个周围区域的蓝牙设备如果它是使能了被发现 可见性 的 它将会回应这个发现 请求 通过分享一些信息 例如设备的名字 类别 和它唯一的物理地址 通过这些信息 启动发现过程 扫描设备 的设备然后可以选择初始化连接周围被发现的设备 一旦第一次和远程设备建立连接 一个配对请求将会自动陈现在用户面前 当设备配对了的 话 设备的一些基本信息 设备的名字 类别 和它唯一的物理地址 就被保存了并且可 以通过蓝牙API 读取出来 运用已知的远程设备的物理地址 可以在任何时候建立连接而无 须执行发现过程 扫描周围设备 假设设备在范围之内 记住已经配对和已经连接有一点不同 已经配对意味着 2个设备都已经知道了对方的存在 共同分享一个认证的链路钥匙 可以建立一个加密连接 已经连接意味着设备当前分享同一 个 RFCOMM 通信通道可以相互传送数据 现在的安卓蓝牙 API 需要设备在建立一个 RFCOMM 通信通道之前先进行配对 当你用蓝牙 API 初始化一个加密连接的时候配对是 自动进行的 下面的部分讲诉了如何通过设备发现能力来发现已经配对了设备和最新发现的设备 查询已配对设备 在执行设备发现之前 先查询下已经配对的设备 来看下我们想要的设备是不是已经是知 道的还是值得的 Set pairedDevices mBluetoothAdapter getBondedDevices If there are paired devices if pairedDevices size 0 Loop through paired devices for BluetoothDevice device pairedDevices Add the name and address to an array adapter to show in a ListView mArrayAdapter add device getName n device getAddress 发现 扫描过程 蓝牙设备 启动发现过程 只需要调用 startDiscovery 这是个异步过程 方法马上会返回一个布 尔类型 它表明发现过程启动是否成功 这个发现过程通常包含了 12S的扫描查询过程 接 下来就是包含了每个被发现设备的的页扫描来提取蓝牙设备的名字 Create a BroadcastReceiver for ACTION FOUND private final BroadcastReceiver mReceiver new BroadcastReceiver public void onReceive Context context Intent intent String action intent getAction When discovery finds a device if BluetoothDevice ACTION FOUND equals action Get the BluetoothDevice object from the Intent BluetoothDevice device intent getParcelableExtra BluetoothDevice EXTRA DEVICE Add the name and address to an array adapter to show in a ListView mArrayAdapter add device getName n device getAddress Register the BroadcastReceiver IntentFilter filter new IntentFilter BluetoothDevice ACTION FOUND registerReceiver mReceiver filter Don t forget to unregister during onDestroy 为了初始化连接 所有从BluetoothDevice 对象里面得到的就是物理地址 使能本地蓝牙设备的可发现 可见性 Intent discoverableIntent new Intent BluetoothAdapter ACTION REQUEST DISCOVERABLE discoverableIntent putExtra BluetoothAdapter EXTRA DISCOVERABLE DURATION 300 startActivity discoverableIntent 注意 如果蓝牙没有使能 打开 如果直接使能蓝牙可见性 则蓝牙也自动打开 如果你将要初始化一个连接到远程设备 你就没有必要打开可见性 使能可见性仅仅只是让 你的应用程序作为服务端Socket 它用来接受输入连接请求 因为远程设备在初始化连接之 前必须先发现这个设备 连接设备 为了在你的应用程序里面让2 个设备建立连接 你必须实行服务端和客户端连接机制 因为 一个设备必须打开一个 server socket 服务端套接字而另一个设备必须初始化这个连接 使 用 server 设备的物理地址来建立连接 当 server 和 client 他们之间在同一个 RFCOMM 通 信通道之间有一个连接好了的 BluetoothSocket 的时候 server 和 client 服务端和客户端就被 认为是已经建立好了连接 在这个点上 每个设备就可以获得输入输出流 数据传输也可以 开始 关于这点我们在管理连接那节介绍 我们这节只讲如何在 2个设备之间初始化连接 Server 设备和 client 设备各自获得 BluetoothSocket 要用不同的方式 Server 将会接收一个 BluetoothSocket 当server 接受一个输入连接请求的时候 Client 将会接收 BluetoothSocket 当 它打开一个和server 之间的 RFCOMM 通信通道的时候 Block 在这里是阻塞的意思 作为服务端连接 当你想连接 2 个设备 一个必须作为 server 服务端 通过保持打开 BluetoothServerSocket 这个 BluetoothServerSocket 的目的是为了监听输入连接请求并且当接受了请求之后 服务 端就得到了一个连接了的 BluetoothSocket 当 从 BluetoothServerSocket 这里得到了 BluetoothSocket 之后 BluetoothServerSocket 就应该被丢弃 除非你想接受更多的输入连接 请求 你就把它就留着 下面是建立一个server socket 和 accept a connection 接受一个连接请求的基本过程 1 通过调用listenUsingRfcommWithServiceRecord String UUID 得到一个 BluetoothServerSocket string 是你的 service 服务的名字 系统将会自动把它写入新服务发现 协议 SDP 数据库实例 这个名字是具体的 可以就是你的应用程序的名字 到设备上 而UUID 也是数据库实例的一部分并且是与 client 客户端连接的基本协议 也就是说 当客 户端想要和设备建立连接 它必须承载一个和它想要连接 server 服务端的唯一一个一样的 UUID 2 通过调用 accept 来启动监听连接请求 这个是阻塞调用方法 它会在连接被接受了或 者发生了一个异常时才返回 一个连接请求会被接受 只会在当一个远程设备发送的连接请 求的 UUID 匹配了注册的监听 server socket 服务套接字的时候 当成功的时候 accept 会返回一个连接了的BluetoothSocket 3 除非你想继续接受输入连接请求 否则调用 close 这个函数会释放 server socket 的 所有资源 但是不会关闭已经是之前 accept 返回的已经连接了的 BluetoothSocket 不像 TCP IP RFCOMM 只允许一次只能建立一个连接通道 Accept 函数不应该在主 activity 里面调用 因为它是一个阻塞调用将会阻止在应用程序 里面其他的交互 Example Here s a simplified thread for the server component that accepts incoming connections private class AcceptThread extends Thread private final BluetoothServerSocket mmServerSocket public AcceptThread Use a temporary object that is later assigned to mmServerSocket because mmServerSocket is final BluetoothServerSocket tmp null try MY UUID is the app s UUID string also used by the client code tmp mBluetoothAdapter listenUsingRfcommWithServiceRecord NAME MY UUID catch IOException e mmServerSocket tmp public void run BluetoothSocket socket null Keep listening until exception occurs or a socket is returned while true try socket mmServerSocket accept catch IOException e break If a connection was accepted if socket null Do work to manage the connection in a separate thread manageConnectedSocket socket mmServerSocket close break Will cancel the listening socket and cause the thread to finish public void cancel try mmServerSocket close catch IOException e 作为客户端连接 为了初始化连接一个远程设备 那个保持打开服务套接字的设备 server socket 你必须首 先获得代表了远程设备的 BluetoothDevice 的对象 获取一个 BluetoothDevice 在查找设备 那节讲诉了 然后你必须要用 BluetoothDevice 来得到 BluetoothSocket 和初始化连接 下面 是最基本的流程 1 用BluetoothDevice得到一个BluetoothSocket 通过调用 createRfcommSocketToServiceRecord UUID 这就初始化了将要连接到 BluetoothDevice 的 BluetoothSocket 蓝牙套接字 这 里 的 UUID 必 须 和 server 设 备 通 过 打 开 BluetoothServerSocket 调用 listenUsingRfcommWithServiceRecord String UUID 里面用 到的UUID 一样 使用一样的 UUID 仅仅只是硬编码字符串到你的应用程序里面 然后还要 在server 和 client 里面都引用它 2 调用connect 来初始化连接 通过这个方法 为了匹配UUID 系统将会在远程设备上执行 SDP查找 如果成功找到的话 远程设备将会接受这个连接 而且在连接中 它也将分享这个 RFCOMM 通信信道同时 connect 返回 这个方法也是个阻塞方法 如果因为某些原因连接失败或者connect 超 时了 大概 12S 这个方法将抛出一个异常 因为 connect 是个阻塞调用方法 所以 连接程序要放在主activity程序之外的一个线程里面 Example Here is a basic example of a thread that initiates a Bluetooth connection private class ConnectThread extends Thread private final BluetoothSocket mmSocket private final BluetoothDevice mmDevice public ConnectThread BluetoothDevice device Use a temporary object that is later assigned to mmSocket because mmSocket is final BluetoothSocket tmp null mmDevice device Get a BluetoothSocket to connect with the given BluetoothDevice try MY UUID is the app s UUID string also used by the server code tmp device createRfcommSocketToServiceRecord MY UUID catch IOException e mmSocket tmp public void run Cancel discovery because it will slow down the connection mBluetoothAdapter cancelDiscovery try Connect the device through the socket This will block until it succeeds or throws an exception mmSocket connect catch IOException connectException Unable to connect close the socket and get out try mmSocket close catch IOException closeException return Do work to manage the connection in a separate thread manageConnectedSocket mmSocket Will cancel an in progress connection and close the socket public void cancel try mmSocket close catch IOException e 管理一个连接 当你已经成功连接好了 2 个设备或者更多的时候 每个设备将会有一个连接好了的 BluetoothSocket 蓝牙套接字 这就是蓝牙功能开始的时候因为你可以在设置之前分享数据了 使用BluetoothSocket 蓝牙套接字 分享数据的一般流程是很简单的 1 凭 借 getInputStream 和 getOutputStream 来 得 到 socket 的 处 理 流 数 据 的 对 象 InputStream 和 OutputStream 2 读写数据到流里面通过 read byte 和 write byte 你应该使用一个专门的线程来处理输入输出流 那是非常重要的 因为 read byte 和 write byte 是阻塞调用方法 read byte 将会阻塞直到流里面有东西可以读出来 write byte 不是通常会阻塞 但是也会因为流控而阻塞 如果远程设备没有迅速调用 read byte 或 者中间缓存满了 所以 你这个线程的主循环应该专门从 InputStream 输入流读取数据 并且 在这个线程里面应该设置一个分离的 public 公共方法来来初始化写数据到输出流 OutputStream Example Here s an example of how this might look private class ConnectedThread extends Thread private final BluetoothSocket mmSocket private final InputStream mmInStream private final OutputStream mmOutStream public ConnectedThread BluetoothSocket socket mmSocket socket InputStream tmpIn null OutputStream tmpOut null Get the input a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论