Android 蓝牙

来源:互联网 发布:淘宝店铺上传宝贝软件 编辑:程序博客网 时间:2024/05/01 12:29

参考文章:http://wear.techbrood.com/guide/topics/connectivity/bluetooth.html ,这是developer.android.com在中国的一个镜像,大家可以收藏。

蓝牙连接初识

下图是对Android蓝牙连接的一个通俗的认识,有雷同,实属巧合。
阿白和阿酷的相识相知
主要有下面几个步骤:

一、Setting Up Bluetooth

  1. 验证设备是否支持蓝牙
  2. 打开蓝牙

二、查找设备

  1. 请求已配对的设备
  2. 加入没有请求到,则发现新的设备

三、连接设备

  1. 作为client连接设备
  2. 作为server连接设备

四、管理连接

  1. 写数据(写数据到设备上)
  2. 读数据(从设备读取数据)

心得体会

  1. 发现新朋友是需要耗费很多资源,一旦发现了设备,在连接设备之前需要取消发现,否则会占用带宽 。
  2. 安卓也提供了专门用于连接健康可穿戴设备的profile,即Health Device Profile (HDP),用于封装和简化蓝牙连接。
    这是步骤:
    Creating an HDP Application

Here are the basic steps involved in creating an Android HDP application:

Get a reference to the BluetoothHealth proxy object.
Similar to regular headset and A2DP profile devices, you must call getProfileProxy() with a BluetoothProfile.ServiceListener and the HEALTH profile type to establish a connection with the profile proxy object.
Create a BluetoothHealthCallback and register an application configuration (BluetoothHealthAppConfiguration) that acts as a health sink.
Establish a connection to a health device. Some devices will initiate the connection. It is unnecessary to carry out this step for those devices.
When connected successfully to a health device, read/write to the health device using the file descriptor.
The received data needs to be interpreted using a health manager which implements the IEEE 11073-xxxxx specifications.
When done, close the health channel and unregister the application. The channel also closes when there is extended inactivity.

0 0