有时需要用到在上层的工程模式中测试蓝牙功能的正常与否,可以用个简单的activiy实现。搜到BT device设备就表示成功。
(1)用到的import类及工程权限
import android.os.Bundle;
import android.os.Handler;
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.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
(2)MainActivity用到的变量
private TextView mStatus; //BT工作状态的显示
private int mCount=0; //扫得BT设备的数量
private ListView newDevicesListView;
private ArrayAdapter<String> mNewDevicesArrayAdapter;
private BluetoothAdapter mBluetoothAdapter = null; //BT适配器
private boolean mTestResult; //测试结果
页面的onCreate的内容
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTestResult = false; //默认扫描失败
mStatus = (TextView) findViewById(R.id.status_textview);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.BT_not_avalible, Toast.LENGTH_LONG).show();
Intent mIntent = new Intent();
setResult(-1, mIntent);
finish();
return; //获得BT适配器失败的话,直接退出页面
}
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); //默认layout
newDevicesListView = (ListView) findViewById(R.id.bt_device_listview);
newDevicesListView.setAdapter(mNewDevicesArrayAdapter); //绑定listview与adapter
页面onStart的内容
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //接受特定Intent
this.registerReceiver(mReceiver, filter);
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable(); //如果BT没打开,使能它
mStatus.setText(R.string.BT_Enableing);
timeout_handler.removeCallbacks(checkbtstate);
timeout_handler.postDelayed(checkbtstate, 8000); //8秒钟后再检测BT是否打开
} else {
mStatus.setText(R.string.BT_Enabled);
timeout_handler.removeCallbacks(checkbtstate);
doDiscovery(); //如果BT已打开,不再检测BT使能状态,开始扫描
}
超时检测BT页面的的线程
private Handler timeout_handler = new Handler();
private Runnable checkbtstate = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
timeout_handler.removeCallbacks(checkbtstate);
if (!mBluetoothAdapter.isEnabled()) {
mStatus.setText(R.string.bt_enable_error); //如果BT使能还是失败,提示ERROR
}
}
};
页面退出函数onDestroy的实现
super.onDestroy();
timeout_handler.removeCallbacks(checkbtstate);
timeout_handler = null;
if(mReceiver != null){
this.unregisterReceiver(mReceiver);
mReceiver = null;
}
if(mBluetoothAdapter != null){
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
}
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
mBluetoothAdapter = null;
}
扫描函数doDiscovery
private void doDiscovery(){
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
}
响应BT intent的receiver实现
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) ==
BluetoothAdapter.STATE_ON) { //BT适配器已打开,此时receiver早已打开
mStatus.setText(R.string.BT_Enabled);
timeout_handler.removeCallbacks(checkbtstate); //不再检测BT使能状态,扫描
doDiscovery();
}else if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) ==
BluetoothAdapter.STATE_OFF) { //BT适配器还是关闭,3秒后重新试着打开
mBluetoothAdapter.enable();
mStatus.setText(R.string.BT_Enableing);
timeout_handler.removeCallbacks(checkbtstate);
timeout_handler.postDelayed(checkbtstate, 3000);
}
}else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
mCount = mNewDevicesArrayAdapter.getCount();//找到BT设备,显示设备的名字和MAC地址
if(mCount > 0){ //如果列表数目大于1,说明至少找到一个设备
mTestResult = true;
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if(mCount <= 0){ //一个也没有找到过
mTestResult = false;
mStatus.setTextColor(0xffff0033);
mStatus.setText(getString(R.string.BT_scan_over)+ "\n"+ getString(R.string.btdevicenum) + mCount + "\n" + getString(R.string.testagain));
}else{
mTestResult = true; //提示找到的设备数目
mStatus.setText(getString(R.string.BT_scan_over)+ "\n"+ getString(R.string.btdevicenum) + mCount);
}
}
}
};
以上仅实现基本功能,无绑定配对。