廢話不多說直接上效果圖
:與藍牙相關的最重要的兩個API
1:BuletoothAdapter
這個類的對象代表了本地的藍牙擴充卡,相當于藍牙工作流程圖中的手機裡的藍牙擴充卡,也就是說比如這個應用程式是運作在手機上,那麼手機上的藍牙擴充卡就是本地藍牙擴充卡。
2:BuletoothDevice
這個類的對象代表了遠端的藍牙裝置,相當于藍牙工作流程圖中的計算機裡的藍牙擴充卡,也就是說比如這個應用程式是運作在手機上,那麼BuletoothDevice代表了你要連接配接的遠端的那個裝置上面的藍牙擴充卡。
硬體準備
今天這個示例必須運作在具有安卓2.0SDK以上的手機上面,不能運作在模拟器上面,因為現在的模拟器是不能模拟藍牙的,是以必須有個安卓的手機,另外要有台具有藍牙擴充卡的電腦。手機和電腦來進行配對,隻能通過手動來進行,不可能通過代碼是實作配對,因為安全性的問題不能通過應用程式自動的來進行配對,一旦配對成功就可以進行檔案的傳輸了。如何配對在這裡就不講解相信大家都了解!
建立BlueToothActivity類
package com.weixin;
import java.util.List;
import java.util.Set;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BlueToothActivity extends Activity {
private Button btn_search;
private TextView tv_devices;
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_CODE=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
initView();
}
private void initView() {
// TODO Auto-generated method stub
btn_search = (Button) findViewById(R.id.btn_search);
tv_devices = (TextView) findViewById(R.id.tv_devices);
}
@SuppressLint("NewApi")
public void Search(View v) {
// 擷取本地藍牙
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 判斷手機是否支援藍牙
if (mBluetoothAdapter == null) {
Toast.makeText(this, "此裝置不支援藍牙", Toast.LENGTH_SHORT).show();
this.finish();
}
// 判斷是否打開藍牙
if (!mBluetoothAdapter.isEnabled()) {
// 彈出對話框提示使用者是後打開
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_CODE);
//不提示強行關閉
//mBluetoothAdapter.disable();
// 不做提示,強行打開
// mBluetoothAdapter.enable();
}
// 擷取已經配對的裝置清單
Set<BluetoothDevice> pList=mBluetoothAdapter.getBondedDevices();
if(pList!=null&&pList.size()>0){
for (BluetoothDevice bluetoothDevice : pList) {
tv_devices.append(bluetoothDevice.getName()+":"+bluetoothDevice.getAddress()+":"+bluetoothDevice.getBondState());
Log.i("已配對裝置", tv_devices.getText().toString());
}
}
// // 判斷是否有配對過的裝置
// if (pairedDevices.size() > 0) {
// for (BluetoothDevice device : pairedDevices) {
// // 周遊到清單中
// tv_devices.append(device.getName() + ":" + device.getAddress());
// Log.i("已配對裝置", tv_devices.getText().toString());
// }
// }
// 找到裝置的廣播
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// 注冊廣播
registerReceiver(receiver, filter);
// 搜尋完成的廣播
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// 注冊廣播
registerReceiver(receiver, filter);
setProgressBarIndeterminateVisibility(true);
setTitle("正在搜尋附近的藍牙裝置...");
// 判斷是否在搜尋,如果在搜尋,就取消搜尋
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 開始搜尋
mBluetoothAdapter.startDiscovery();
}
// 廣播接收器
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
// 發現裝置的廣播
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 從intent中擷取裝置
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 判斷是否配對過
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
// 将搜尋到的裝置添加到清單
tv_devices.append(device.getName() + ":"
+ device.getAddress() + "\n");
Log.i("tag", tv_devices.getText().toString());
}
// 搜尋完成
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
// 關閉進度條
setProgressBarIndeterminateVisibility(true);
//setTheme(R.drawable.gril);
setTitle("搜尋結束!");
}
}
};
}
添加兩個權限
<!-- 添加藍牙裝置權限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
布局檔案activity_bluetooth.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="Search"
android:text="搜尋藍牙裝置"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="顯示附近的藍牙裝置"
android:gravity="center"
android:textSize="20sp" />
</LinearLayout>
完工了!