登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

蓝牙血糖仪手机应用系统的设计与实现外文翻译资料

 2022-08-21 11:08  

Bluetooth

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Using the Bluetooth APIs, an Android application can perform the following:

  • Scan for other Bluetooth devices
  • Query the local Bluetooth adapter for paired Bluetooth devices
  • Establish RFCOMM channels
  • Connect to other devices through service discovery
  • Transfer data to and from other devices
  • Manage multiple connections

The Basics

This document describes how to use the Android Bluetooth APIs to accomplish the four major tasks necessary to communicate using Bluetooth: setting up Bluetooth, finding devices that are either paired or available in the local area, connecting devices, and transferring data between devices.

All of the Bluetooth APIs are available in the android.bluetooth package. Heres a summary of the classes and interfaces you will need to create Bluetooth connections:

BluetoothAdapter

Represents the local Bluetooth adapter (Bluetooth radio). The BluetoothAdapter is the entry-point for all Bluetooth interaction. Using this, you can discover other Bluetooth devices, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for communications from other devices.

BluetoothDevice

Represents a remote Bluetooth device. Use this to request a connection with a remote device through a BluetoothSocket or query information about the device such as its name, address, class, and bonding state.

BluetoothSocket

Represents the interface for a Bluetooth socket (similar to a TCP Socket). This is the connection point that allows an application to exchange data with another Bluetooth device via InputStream and OutputStream.

BluetoothServerSocket

Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket). In order to connect two Android devices, one device must open a server socket with this class. When a remote Bluetooth device makes a connection request to the this device, the BluetoothServerSocket will return a connected BluetoothSocket when the connection is accepted.

BluetoothClass

Describes the general characteristics and capabilities of a Bluetooth device. This is a read-only set of properties that define the devices major and minor device classes and its services. However, this does not reliably describe all Bluetooth profiles and services supported by the device, but is useful as a hint to the device type.

BluetoothProfile

An interface that represents a Bluetooth profile. A Bluetooth profile is a wireless interface specification for Bluetooth-based communication between devices. An example is the Hands-Free profile. For more discussion of profiles, see Working with Profiles

BluetoothHeadset

Provides support for Bluetooth headsets to be used with mobile phones. This includes both Bluetooth Headset and Hands-Free (v1.5) profiles.

BluetoothA2dp

Defines how high quality audio can be streamed from one device to another over a Bluetooth connection. 'A2DP' stands for Advanced Audio Distribution Profile.

BluetoothHealth

Represents a Health Device Profile proxy that controls the Bluetooth service.

BluetoothHealthCallback

An abstract class that you use to implement BluetoothHealth callbacks. You must extend this class and implement the callback methods to receive updates about changes in the applicationrsquo;s registration state and Bluetooth channel state.

BluetoothHealthAppConfiguration

Represents an application configuration that the Bluetooth Health third-party application registers to communicate with a remote Bluetooth health device.

BluetoothProfile.ServiceListener

An interface that notifies BluetoothProfile IPC clients when they have been connected to or disconnected from the service (that is, the internal service that runs a particular profile).

Bluetooth Permissions

In order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions: BLUETOOTH and BLUETOOTH_ADMIN.

You must request the BLUETOOTH permission in order to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.

You must request the BLUETOOTH_ADMIN permission in order to initiate device discovery or manipulate Bluetooth settings. Most applications need this permission solely for the ability to discover local Bluetooth devices. The other abilities granted by this permission should not be used, unless the application is a 'power manager' that will modify Bluetooth settings upon user request.

Note: If you use BLUETOOTH_ADMIN permission, then must also have the BLUETOOTH permission.

Declare the Bluetooth permission(s) in your application manifest file. For example:

lt;manifest ... gt;

lt;uses-permission android:name='android.permission.BLUETOOTH' /gt;

lt;/manifestgt;

See the lt;uses-permissiongt; reference for more information about declaring application permissions.

Setting Up Bluetooth

Figure 1: The enabling Bluetooth dialog.

Before your application can communicate over Bluetooth, you need to verify that Bluetooth is supported on the device, and if so, ensure that it is enabled.

If Bluetooth is not supported, then you should gracefully disable any Bluetooth

剩余内容已隐藏,支付完成后下载完整资料


蓝 牙

Android平台提供了对蓝牙网络协议栈的支持,它允许设备通过无线的方式与其它设备进行数据交换。应用程序框架通过Android Bluetooth APIs提供了对蓝牙功能的访问。这些API允许应用以无线的方式连接其它蓝牙设备,启用点对点或者多点对多点的无线功能。使用蓝牙的API,Android应用程序可以执行以下工作:

  • 扫描其它蓝牙设备
  • 查询本地蓝牙适配器以配对蓝牙设备
  • 建立RFCOMM通道
  • 通过服务搜索连接其它设备
  • 传输或者接收其它设备的数据
  • 管理多个连接

本文档描述了如何使用传统蓝牙。传统蓝牙是耗电操作的理想选择,例如Android设备之间的数据流传输和通讯。为了适应低能耗蓝牙设备,Android 4.3(API Level 18)的API开始支持Bluetooth Low Energy(BLE)。更多信息,请参考低功耗蓝牙部分。

基础部分

本文档描述了如何使用Android Bluetooth APIs来完成四种需要使用蓝牙通信的任务:设置蓝牙,搜索已经配对或者附近区域允许访问的设备,连接设备,还有在设备之间传输数据。

所有的Bluetooth API都可以在android.bluetooth包中访问到。下面是你创建蓝牙连接需要使用到的类和接口的摘要:

BluetoothAdapter

代表Local Bluetooth Adapter(本地蓝牙适配器、蓝牙发送接收器)。BluetoothAdapter是所有蓝牙交互的入口点。使用这个对象,你可以搜索发现其他蓝牙设备,查询已配对配备列表,使用已知MAC地址实例化一个BluetoothDevice对象,创建 一个BluetoothServiceSocket来监听来自其它设备的通信。

BluetoothDevice

代表Remote Bluetooth Device(远程的蓝牙设备)。使用这个对象可以通过一个BluetoothSocket来请求一个远程设备的连接,或者查询这个远程设备的信息,例如名字、地址、类别、以及配对状态。

BluetoothSocket

代表Bluetooth socket(蓝牙套接字)的接口(类似于一个TCP socket)。这是一个连接点,它允许一个应用程序通过InputStream和OutputStream与另一个蓝牙设备交换数据。

BluetoothServerSocket

代表一个open server socket(开放的服务套接字),用于监听即将传入的请求(类似于一个TCP的ServerSocket)。为了能够连接两台Android设备,一台设备必须使用这个类开放一个server socket。当一个远程蓝牙设备发送一个连接请求给这台设备的时候,BluetoothServerSocket接受了这个请求就会返回一个已连接的BluetoothSocket。

BluetoothClass

这个类描述了一台蓝牙设备的一般特征和功能。这是一串只读的属性,定义了这台设备的主要和次要设备类型,以及它的服务。然而,它对这台设备所支持的全部Bluetooth Profile和蓝牙功能的描述并不可靠,但作为对设备类型的一种提示还是非常有用的。

BluetoothProfile

代表一个Bluetooth Profile的接口。Bluetooth Profile是一个针对设备之间基础蓝牙通讯的无线接口规范。例如Hands-Free profile(免提规范)。更多关于profile的讨论,请参考后面的Working with Profiles章节。

BluetoothHeadset

提供了对手机蓝牙耳机的支持。它包括蓝牙耳机和Hands-Free(v1.5)profiles。

BluetoothA2dp

定义了高质量音频流怎样通过蓝牙连接以数据流的方式从一台设备传输到另一个设备。“A2DP”代表Advanced Audio Distribution Profile(高级音频分发)。

BluetoothHealth

代表一个Health Device Profile(健康设备)的代理,用来控制蓝牙服务。

BluetoothHealthCallback

用于实现BluetoothHealth回调函数的虚拟类。你必须继承这个类并实现其中的回调函数,才能够接收到关于应用程序的注册状态和蓝牙通道状态的更新。

BluetoothHealthAppConfiguration

代表一个第三方蓝牙健康应用程序在注册后并与一个远程蓝牙健康设备通讯的应用配置。

BluetoothProfile.ServiceListener

一个用于通知BluetoothProfile IPC客户端连接或者断开服务的接口(这意味着有一个内部服务运行着一个指定的Profile)。

蓝牙权限

为了在你的应用程序上可以使用蓝牙功能,你必须声明蓝牙权限android.permission.BLUETOOTH。你需要这个权限才能执行一些蓝牙通讯,例如请求一个连接,接受一个连接,传输数据。

如果你的应用还要启动设备搜索或者管理蓝牙设置,那还需要声明android.permission.BLUETOOTH_ADMIN权限。大多数应用需要这个权限只是为了搜索附近的蓝牙设备。这个权限提供的其它功能不应该去使用,除非这个应用程序是一个类似“电池管理”的应用,用户期望通过这个应用去管理蓝牙设置。注意:如果你使用BLUETOOTH_ADMIN权限,那么你必须同样声明BLUETOOTH权限。

在你的应用manifest文件中声明蓝牙权限,例如:

lt;manifest ... gt;

lt;uses-permission android:name='android.permission.BLUETOOTH' /gt;

lt;/manifestgt;

lt;manifest ... gt;

lt;uses-permission android:name='android.permission.BLUETOOTH' /gt;

lt;/manifestgt;

参考lt;uses-permissiongt;获取更多关于声明应用权限的信息。

设置蓝牙

在你的应用程序通过蓝牙通讯之前,你需要确认设备是否支持蓝牙。如果支持,还要再确认是否有启用蓝牙。如果不支持蓝牙,那么你应该友好的禁用一些蓝牙功能。如果支持蓝牙,但是没有启用,你应该在不离开你的应用程序情况下请求用户启用蓝牙。这种设置需要使用BluetoothAdapter,通过两个步骤完成:

1. 获取BluetoothAdapter

几乎所有使用蓝牙的Activity都要求有BluetoothAdapter。可以通过调用静态的getDefaultAdapter()方法获取BluetoothAdapter。这个方法会返回一个代表设备自身蓝牙适配器的BluetoothAdapter。这个BluetoothAdapter适用于整个系统,你的应用程序可以通过这个对象与系统交互。如果getDefaultAdapter()返回null,可能是设备不支持蓝牙,你的操作到此为止。例如:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) { // 设备不支持蓝牙 }

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) {// 设备不支持蓝牙}

2. 启用蓝牙

下一步,你需要确认蓝牙已经启用。调用isEnable()方法检查当前蓝牙是否已经启用。如果这个方法返回false,表示蓝牙已经禁用。请求启用蓝牙,要调用startActivityForResult()和使用action为ACTION_REQUEST_ENABLE的Intent。这里将会通过系统设置弹出一个启用蓝牙的请求(不会停止你的应用程序)。例如:

if (!mBluetoothAdapter.isEnabled()) {

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }

if (!mBluetoothAdapter.isEnabled()) {

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

}

将会出现一个对话框请求用户授权启用蓝牙,如图。如果用户点击“Yes”,系统将会启用蓝牙,焦点也会在程序完成(或者失败)之后返回到你的应用程序。

传递到startActivityForRequest()的REQUEST_ENABLE_BT常量是自己定义的一个整数(必须大于0),系统会将其作为requestCode参数传递到你的onActivityResult()方法中。

如果启用蓝牙成功,你的activity就会在onActivityResult()中接收到一个RESULT_OK的result code。如果蓝牙由于一个错误(或者是用户点击了“No”)而启用失败,那么result code将会是RESULT_CANCELED。

另外,你的应用程序同样可以使用广播监听ACTION_STATE_CHANGED这种类型的Intent,因为系统在蓝牙状态发生改变的时候会发送广播。这种广播包含extra数据EXTRA_STATE和EXTRA_PREVIOUS_STATE,分别表示新的和旧的的蓝牙状态。这些extra数据可能值包括STATE_TURNING_ON、STATE_ON、STATE_TURNING_OFF和STATE_OFF。在你的应用程序运行的时候监听这个广播对于检测蓝牙状态引起的变化非常有用。

提示:启用设备可被发现模式同样会自动启用蓝牙。如果你计划在进入蓝牙功能的Activity之前始终启用设备可被发现,你可以跳过上面步骤2。阅读关于下面启用可被发现模式部分。

查找设备

你可以使用BluetoothAdapter通过设备搜索或者查询已配对设备列表来发现远程设备。

设备搜索是一个扫描程序,它会搜索本地附近启用了蓝牙的设备,并从这些设备上获取一些信息(这个过程有时简称为“发现中”、“查询中”或者“扫描中”)。然而,在本地区域范围内的蓝牙设备只有在它是允许被发现的模式下才会响应其它设备扫描发现的请求。如果一个设备是可被发现的状态模式,它会通过共享一些信息来响应搜索发现的请求,例如设备名字,设备类型和它唯一的MAC地址。使用这些信息,执行扫描的设备才能创建一个连接连接上被发现的设备。

如果是第一次与远程设备建立连接,(本地设备)将会自动提示用户一个配对的请求。当设备配对成功之后,远程设备的基本信息(例如设备名字,类型和MAC地址)将会被保存,并且可以使用Bluetooth APIs读取。使用已知的远程设备的MAC地址,可以在任何时候建立与远程设备的连接,而不需要预先执行扫描(即使远程设备现在不在附近范围内)。

注意设备配对和设备连接两者之间有点不同。设备配对意味着两个设备彼此之间都知道对方的存在,并且共享一个连接key可以进行互相验证和创建一个加密的连接。设备连接意味着设备之间当前共享一个RFCOMM通道,能够互相传输数据。当前的Android Bluetooth API要求在创建一个RFCOMM

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[498735],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图