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.

Android平臺包含技持藍牙網路,它允許一個設備與另一個設備進無線資料傳輸。這個程式框架提供了API訪問藍牙的函數。讓這些程式無線去連接藍牙設備,應用與點對點和多個點無線特徵.

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 建立RFCOMM通道

•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.

這個文檔用於描述如何去使用Android 藍牙API去完成自四個主要的任務。1、設置藍牙 2、發現本地可用設備或已配對設備 3、連接設備 4、傳輸資料



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.

這個類主要是代表本機的藍牙配接器。藍牙配接器是所有藍牙設備資料交換的入口。使用這個類你可以發現其它藍牙設備,查詢已配對設備清單,已知MAC(硬體位址)具現化一個設備物件,創建一個BluetoothServerSocket 物件去監聽來自其它藍牙設備的連接。


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.

代表一個遠端藍牙設備。用這個類通過一個 BlueSocket(相當於TCP/IP裡面的Socket) 可去請求一個與遠端設備的連接,或者可以查詢以下資訊:設備名稱、硬體位址、類及連接狀態。

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.

代表BluetoothSocket的介面(類似于TCPSocket).這是一個連接點,它允許程式通過輸入輸出流進行資料交換


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.

代表一個開放的服務Socket去監聽進來的請求(類似于一個TCP ServerSocket).為了連接兩個Android設備,一個設備必須打開一個ServerSocket的服務。當一個遠端的藍牙設備發一個連接請求與這個打開ServerSocket設備,如果接受請求將返回 BluetoothServerSocket 將返回一個 BluetoothSocke t的實例。


BluetoothClass
Describes the general characteristics and capabilities of a Bluetooth device. This is a read-only set of properties that define the device's 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.


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).

in order to use Bluetooth features in your application, you need to declare at least one of two Bluetooth permissions: BLUETOOTH and BLUETOOTH_ADMIN .
為了在你的程式裡能使用藍牙特性,你需要聲明至少一個或兩個藍牙Permissions-------------->BLUETHOOTH and BLUETOOTH_ADMIN

You must request the BLUETOOTH_ADMIN permission in order to initiate device discovery or manipulate Bluetooth settings. 為了具現化一個device 搜索 或者操控藍牙設置,就得有BLUETHOOTH_ADMIN 這個 Permission.

You must request the BLUETOOTH permission in order to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data. 為了執行藍牙通信比如:請求一個連接、接受一個連接及傳輸資料,就得有 BLUETOOTH 這個 Permission.

例如 :
<manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> ...</manifest>

Setting Up Bluetooth

If Bluetooth is not supported, then you should gracefully disable any Bluetooth features. If Bluetooth is supported, but disabled, then you can request that the user enable Bluetooth without leaving your application. This setup is accomplished in two steps, using the BluetoothAdapter.
如果你的機器不支援藍牙,這個時候就不用再考慮藍牙這個問題了。如果你的機器支援藍牙但是不能用,這個時候你可請求使用藍牙。你無需離開程式,通過使用BluetoothAdapter這個類兩步就搞定了。

1、Get the BluetoothAdapter 獲得藍牙配接器

The BluetoothAdapter is required for any and all Bluetooth activity. To get the BluetoothAdapter, call the static getDefaultAdapter() method. This returns a BluetoothAdapter that represents the device's own Bluetooth adapter (the Bluetooth radio). There's one Bluetooth adapter for the entire system, and your application can interact with it using this object. If getDefaultAdapter() returns null, then the device does not support Bluetooth and your story ends here.


對於任務Bluetooth activity都是必須的。通過靜態方法 getDefaultAdapter() 方法就可以得到一個你自己設備的藍牙配接器。整個系統有一個藍牙配接器就可以用這個物件進行溝通。

For example:

// Get local Bluetooth adapter 得到本機的藍牙配接器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// If the adapter is null, then Bluetooth is not supported 如果為空,就是不支援藍牙設備
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}



2、Enable Bluetooth 使用藍牙

Next, you need to ensure that Bluetooth is enabled. Call isEnabled() to check whether Bluetooth is currently enable. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() with the ACTION_REQUEST_ENABLE action Intent. This will issue a request to enable Bluetooth through the system settings (without stopping your application).

接下來,你需要確定藍牙是可以用的。使用isEnabled() 方法去檢查當前藍牙設備是否可用,如果這個方法返回 false 藍牙不可用。去請求開啟藍牙設備。To request that Bluetooth be enabled.通過系統發出一個請求去開啟藍牙(不用停止你的的應用程式)

for example:
//開始顯示在屏目上,可以onStop()重新回到onStart() 可以參看Activity的生命週期
@Override
protected void onStart() {

super.onStart();
if(D) Log.e(TAG, "++ 開始藍牙設備 (ON START) ++");

if(!mBluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
}else {
if(mChatService == null) setupChat();
}
}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()