参考博客http://blog.csdn.net/qinjuning/article/details/7726093
import android.app.Activity;
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.widget.TextView;
import android.view.View;
import android.widget.Button;
import android.bluetooth.BluetoothAdapter;
public class BTTest extends EmActivity {
TextView mtext1;
TextView mtext2;
View btn_pass;
private TextView mTextView;
Button mbutton;
boolean mOrgBtState=false;
BluetoothAdapter mBtAdapter=null;
boolean btstate=false;
String bt_status;
String status_off;
String status_on;
String address;
private int s=BluetoothAdapter.STATE_OFF;
Button scanbutton;
//定义当扫描到蓝牙设备或者蓝牙状态值发生变化时广播接收器中的操作
private final BroadcastReceiver mReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)){
//获取到发送的广播中蓝牙设备的信息
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//这里获取设备的绑定状态也需要权限android.permission.BLUTTOOTH.
//BluetoothDevice.BOND_BONDED表示设备已经绑定
if(device.getBondState()!=BluetoothDevice.BOND_BONDED){
//获取蓝牙设备的名称,需要权限
mTextView.append(device.getName()+"\n");
mTextView.invalidate();//刷新UI控件
if(btn_pass==null){
View dv=getWindow().getDecorView();
btn_pass=dv.findViewById(R.id.btn_succ);
}
btn_pass.setVisibility(View.VISIBLE);
}
}else{//如果是蓝牙状态信息改变
int state=intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.ERROR);
if(state!=BluetoothAdapter.ERROR){
//如果状态为BluetoothAdapter.STATE_OFF or BluetoothAdapter.STATE_ON就更新界面
updateScreen(state);
}
}
}
};
private void updateScreen(int mode){
String state="";
switch(mode){
case BluetoothAdapter.STATE_OFF:
state=status_off;
break;
case BluetoothAdapter.STATE_ON:
state=status_on;
address=mBtAdapter.getAddress();
if(mBtAdapter.isDiscovering()){
mBtAdapter.cancelDiscovery();
}
mBtAdapter.startDiscovery();
break;
default:
break;
}
mtext1.setText(bt_status+state);
mtext2.setText(address);
}
@Override
protected void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.em_bt);
address=getString(R.string.unknown);
bt_status=getString(R.string.bt_status);
status_off=getString(R.string.status_off);
status_on=getString(R.string.status_on);
mtext1=(TextView)findViewById(R.id.m0_t1);
mtext1.setText(bt_status+status_off);
mtext2=(TextView)findViewById(R.id.m0_t2);
mTextView=(TextView)findViewById(R.id.device_list);
mbutton=(Button)findViewById(R.id.m0_b);
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.m0_b:
{
finish();//结束活动
return;
}
default:
break;
}
}
});
mbutton.setText("OK");
scanbutton=(Button)findViewById(R.id.scan);
scanbutton.setText(R.string.btn_text_scan);
scanbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送启动系统蓝牙设置界面的广播
Intent intent=new Intent();
intent.setAction("android.settings.BLUETOOTH_SETTINGS");
startActivity(intent);
btnSucc.setEnabled(true);//btnSucc是父类的Button对象
}
});
scanbutton.setVisibility(View.GONE);
//功能:获得本设备的蓝牙适配器实例。
// 返回值:如果设备具备蓝牙功能,返回BluetoothAdapter 实例;否则,返回null对象。
mBtAdapter=BluetoothAdapter.getDefaultAdapter();
//获取当前蓝牙的使能状态,这里需要蓝牙权限android.permission.BLUETOOTH
mOrgBtState=mBtAdapter.isEnabled();
if(mOrgBtState){
mtext1.setText(bt_status+status_on);
}
btstate=mBtAdapter.isEnabled();
if(!btstate){//如果当前蓝牙的使能状态为false
mBtAdapter.enable();//使能蓝牙适配器
}else{
//如果当前蓝牙的使能状态为true,那么获取蓝牙设备的硬件地址(MAC地址)
//这里需要蓝牙权限android.permission.BLUETOOTH
address=mBtAdapter.getAddress();
//功能: 是否正在处于扫描过程中。 //注意: 如果蓝牙没有开启,该方法会返回false。
if(mBtAdapter.isDiscovering()){
//功能: 取消扫描过程。 //注意: 如果蓝牙没有开启,该方法会返回false。
mBtAdapter.cancelDiscovery();
}
//功能: 扫描蓝牙设备 注意: 如果蓝牙没有开启,该方法会返回false ,即不会开始扫描过程。
mBtAdapter.startDiscovery();
//如果不添加以上内容,如果已经在设置里面打开蓝牙开关,再进入测试界面,不会自动扫描设备
}
mtext2.setText(address);
}
@Override
protected void onResume() {
super.onResume();
if(btn_pass==null){
View dv=getWindow().getDecorView();
btn_pass=dv.findViewById(R.id.btn_succ);
btn_pass.setVisibility(View.GONE);
}
//注册监听蓝牙状态(on of off)的广播和扫描设备的广播
IntentFilter intentFilter=new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
//蓝牙扫描时,扫描到任一远程蓝牙设备时,会发送此广播。
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,intentFilter);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(!btstate){
/*功能:关闭蓝牙设备。
返回值:该函数会立即返回。
true 表示关闭操作成功
false 表示蓝牙操作失败 , ①、当前蓝牙已经关闭 ; ②、其他一些异常情况*/
mBtAdapter.disable(true);//需要权限
}
unregisterReceiver(mReceiver);
}
}