Android Bluetooth(藍牙)實例
在很多方面,藍牙是一種能夠發送或接受兩個不同的設備之間傳輸的數據。 Android平臺包含了藍牙框架,使設備以無線方式與其他藍牙設備進行數據交換的支持。
Android提供藍牙API來執行這些不同的操作。
掃描其他藍牙設備
獲取配對設備列表
連接到通過服務發現其他設備
Android提供BluetoothAdapter類藍牙通信。通過調用創建的對象的靜態方法getDefaultAdapter()。其語法如下給出。
private BluetoothAdapter BA; BA = BluetoothAdapter.getDefaultAdapter();
爲了使用設備的藍牙,調用下列藍牙ACTION_REQUEST_ENABLE的意圖。其語法如下:
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0);
除了這個常量,有提供其它的API,支持不同任務的其他常數。它們在下面列出。
Sr.No
常數說明
1
ACTION_REQUEST_DISCOVERABLE
此常數用於開啓藍牙的發現
2
ACTION_STATE_CHANGED
此常量將通知藍牙狀態已經改變
3
ACTION_FOUND
此常數用於接收關於所發現的每個設備的信息
啓用了藍牙功能之後,可以通過調用 getBondedDevices()方法來獲取配對設備列表。它返回一組的藍牙設備。其語法如下:
private Set<BluetoothDevice>pairedDevices; pairedDevices = BA.getBondedDevices();
除了配對的設備,還有API,讓更多藍牙控制權等方法。它們在下面列出。
Sr.No
方法及說明
1
enable()
這種方法使適配器,如果未啓用
2
isEnabled()
如果適配器已啓用此方法返回true
3
disable()
該方法禁用適配器
4
getName()
此方法返回的藍牙適配器的名稱
5
setName(String name)
此方法更改藍牙名稱
6
getState()
此方法返回藍牙適配器的當前狀態
7
startDiscovery()
此方法開始藍牙120秒的發現過程。
示例
這個例子提供了示範BluetoothAdapter類操縱藍牙,並顯示通過藍牙配對設備列表。
爲了試驗這個例子,需要在實際設備上運行此程序
步驟
描述
1
使用Android Studio創建Android應用程序,並將其命名爲Bluetooth,創建這個項目,確保目標SDK編譯在Android SDK的最新版本或使用更高級別的API。
2
修改 src/MainActivity.java 文件中添加代碼
3
如果修改所需的佈局XML文件 res/layout/activity_main.xml 添加GUI組件
4
修改 res/values/string.xml 文件,並添加必要的字符串常量組件
5
修改 AndroidManifest.xml 添加必要的權限。
6
運行應用程序並選擇運行Android的設備,並在其上安裝的應用和驗證結果。
以下是 src/com.yiibai.bluetooth/MainActivity.java 文件的內容:
package com.example.bluetooth; import java.util.ArrayList; import java.util.List; import java.util.Set; import android.os.Bundle; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private Button On,Off,Visible,list; private BluetoothAdapter BA; private Set<BluetoothDevice>pairedDevices; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); On = (Button)findViewById(R.id.button1); Off = (Button)findViewById(R.id.button2); Visible = (Button)findViewById(R.id.button3); list = (Button)findViewById(R.id.button4); lv = (ListView)findViewById(R.id.listView1); BA = BluetoothAdapter.getDefaultAdapter(); } public void on(View view){ if (!BA.isEnabled()) { Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0); Toast.makeText(getApplicationContext(),"Turned on" ,Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(),"Already on", Toast.LENGTH_LONG).show(); } } public void list(View view){ pairedDevices = BA.getBondedDevices(); ArrayList
  list
  =
new
ArrayList
();
   for
   (
   BluetoothDevice
   bt
   :
    pairedDevices
   )
    list
   .
   add
   (
   bt
   .
   getName
   ());
   Toast
   .
   makeText
   (
   getApplicationContext
   (),
   "Showing Paired Devices"
   ,
   Toast
   .
   LENGTH_SHORT
   ).
   show
   ();
final
ArrayAdapter
adapter 
\=
new
ArrayAdapter
 (
 this
 ,
 android
 .
 R
 .
 layout
 .
 simple\_list\_item\_1
 ,
  list
 );
  lv
 .
 setAdapter
 (
 adapter
 );
 }
 public
 void
  off
 (
 View
  view
 ){
  BA
 .
 disable
 ();
 Toast
 .
 makeText
 (
 getApplicationContext
 (),
 "Turned off"
 ,
 Toast
 .
 LENGTH\_LONG
 ).
 show
 ();
 }
 public
 void
  visible
 (
 View
  view
 ){
 Intent
 getVisible 
 \=
 new
 Intent
 (
 BluetoothAdapter
 .
  ACTION\_REQUEST\_DISCOVERABLE
 );
  startActivityForResult
 (
 getVisible
 ,
 0
 );
 }
 @Override
 public
 boolean
  onCreateOptionsMenu
 (
 Menu
  menu
 )
 {
 // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater
 ().
 inflate
 (
 R
 .
 menu
 .
 main
 ,
  menu
 );
 return
 true
 ;
 }
 }
這裏是 activity_main.xml 文件的內容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="on" android:text="@string/on" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="visible" android:text="@string/Visible" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="list" android:text="@string/List" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="off" android:text="@string/off" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" >
這裏是 Strings.xml 文件的內容:
 
這裏是 AndroidManifest.xml 文件的內容:
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yiibai.bluetooth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yiibai.bluetooth.MainActivity" android:label="@string/app_name" > 
讓我們試着運行AndroidCapture應用程序。假設你已經連接實際的Android移動設備到計算機。啓動應用程序之前,Eclipse會顯示如下窗口,選擇要運行的Android應用程序的選項。
   
選擇移動設備作爲一個選項,然後檢查移動設備將顯示如下界面:
   
現在選擇打開開啓藍牙。但是當選擇它,藍牙將不會被打開。事實上它會詢問許可,以啓用藍牙。
   
現在,只需要選擇設置可見按鈕來打開視圖。下面的屏幕會出現要求許可才能打開發現120秒。
   
現在,只要選擇列表中的設備選項。它會列出倒在列表視圖中的配對設備。就我而言,只有一個配對設備。它如下所示。
   
現在,只需選擇關閉按鈕來關閉藍牙。當關掉藍牙指示成功切換關閉藍牙會出現以下消息。
   
以下代碼下載: http://pan.baidu.com/s/1o69hnaa