Android监听蓝牙与设备连接状态、关闭和打开状态
作者:互联网
在Android设备与蓝牙设备进行数据交换时,需要实时监听蓝牙关闭和打开的状态、蓝牙与设备的连接状态,
进而对程序进行相应的逻辑处理,提供用户体验。本文实现了监听蓝牙的关闭和打开状态、蓝牙与设备的连接
状态,详细的实现代码如下:
注意:监听蓝牙状态时,请使用系统全局广播,本地广播无法实现蓝牙状态的监听
1、安卓权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2、XML代码(activity_bluetooth_monitor.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".BluetoothMonitorActivity"
android:orientation="vertical">
<Button
android:id="@+id/open_ble"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开蓝牙"/>
<Button
android:id="@+id/close_ble"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭蓝牙"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="蓝牙设备的连接状态,可以使用两部手机进行试验,先对两部手机进行配对,然后再
进行手动连接,就会显示连接状态!"/>
</LinearLayout>
3、Activity代码
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class BluetoothMonitorActivity extends AppCompatActivity {
private Button openBle = null;
private Button closeBle = null;
private BluetoothMonitorReceiver bleListenerReceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_monitor);
// 初始化广播
this.bleListenerReceiver = new BluetoothMonitorReceiver();
IntentFilter intentFilter = new IntentFilter();
// 监视蓝牙关闭和打开的状态
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
// 监视蓝牙设备与APP连接的状态
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
// 注册广播
registerReceiver(this.bleListenerReceiver, intentFilter);
// 初始化控件
this.closeBle = findViewById(R.id.close_ble);
this.openBle = findViewById(R.id.open_ble);
// 关闭蓝牙
this.closeBle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
// 跳转到蓝牙设置页面,关闭蓝牙,没有发现弹出对话框关闭蓝牙的
Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(intent,RESULT_OK);
*/
// 隐式关闭蓝牙
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.disable();
}
});
// 打开蓝牙
this.openBle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
// 弹出对话框,打开蓝牙
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,RESULT_OK);
*/
// 隐式打开蓝牙
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.enable();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(this.bleListenerReceiver);
}
}
4、广播代码
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BluetoothMonitorReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null){
switch (action) {
case BluetoothAdapter.ACTION_STATE_CHANGED:
int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
switch (blueState) {
case BluetoothAdapter.STATE_TURNING_ON:
Toast.makeText(context,"蓝牙正在打开中",Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context,"蓝牙已经打开",Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Toast.makeText(context,"蓝牙正在关闭中",Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_OFF:
Toast.makeText(context,"蓝牙已经关闭",Toast.LENGTH_SHORT).show();
break;
}
break;
case BluetoothDevice.ACTION_ACL_CONNECTED:
Toast.makeText(context,"蓝牙设备已链接",Toast.LENGTH_SHORT).show();
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
Toast.makeText(context,"蓝牙设备已断开",Toast.LENGTH_SHORT).show();
break;
}
}
}
}
标签:Toast,状态,layout,BluetoothAdapter,蓝牙,import,Android,android 来源: https://blog.csdn.net/make_progress/article/details/90605748