权限
<!-- 蓝牙权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
广播BluetoothrReceiver
public class BluetoothReceiver extends BroadcastReceiver {
private static final String TAG = "BluetoothReceiver";//此处应不超过23个字符
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null) {
switch (action) {
//手机的蓝牙状态
case BluetoothAdapter.ACTION_STATE_CHANGED:
int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
switch(bluetoothState) {
case BluetoothAdapter.STATE_TURNING_ON:
Log.e(TAG, "onReceive---------蓝牙正在打开中");
break;
case BluetoothAdapter.STATE_ON:
Log.e(TAG, "onReceive---------蓝牙已打开");
Toast.makeText(context, "蓝牙已打开", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.e(TAG, "onReceive---------蓝牙正在关闭中");
break;
case BluetoothAdapter.STATE_OFF:
Log.e(TAG, "onReceive---------蓝牙已关闭");
Toast.makeText(context, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
break;
}
break;
//蓝牙设备已连接
case BluetoothDevice.ACTION_ACL_CONNECTED:
Log.e(TAG, "onReceive---------蓝牙设备已连接");
Toast.makeText(context,"蓝牙设备已连接", Toast.LENGTH_SHORT).show();
break;
//蓝牙设备断开连接
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
Log.e(TAG, "onReceive---------蓝牙设备已断开");
Toast.makeText(context,"蓝牙设备已断开",Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Activity
private BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothListenerReceiver bluetoothListenerReceiver;//蓝牙状态广播
private void BlueToothON() {
if(!adapter.isEnabled()) {//如果手机支持蓝牙功能
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//请求开启蓝牙设备
startActivity(intent);
}
//获得已配对的远程蓝牙设备的集合
Set<BluetoothDevice> devices = adapter.getBondedDevices();
if(devices.size() > 0) {
for(Iterator<BluetoothDevice> it = devices.iterator(); it.hasNext();) {
BluetoothDevice device = it.next();
Toast.makeText(MainActivity.this, device.getName(), Toast.LENGTH_SHORT).show();
}
}
}
private void BlueToothOFF() {
if(adapter.isEnabled()) {
//关闭蓝牙设备
adapter.disable();
}
}
private IntentFilter BluetoothStateFilter() {
IntentFilter intentFilter = new IntentFilter();
// 监听手机蓝牙状态
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//监听蓝牙设备与手机连接的状态
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
return intentFilter;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothListenerReceiver = new BluetoothListenerReceiver();
//注册广播
this.registerReceiver(bluetoothListenerReceiver, BluetoothStateFilter());
}
@Override
protected void onDestroy() {
super.onDestroy();
//注销广播
this.unregisterReceiver(bluetoothListenerReceiver);
}
执行结果
BlueToothAdapter
代表手机的蓝牙适配器,可以用来进行比较基础的蓝牙操作,如开关手机蓝牙,扫描外部蓝牙设备,以及获外部蓝牙设备的名称、Mac地址等信息。
string类型
ACTION_DISCOVERY_FINISHED
手机蓝牙适配器已完成外部蓝牙设备的扫描
ACTION_DISCOVERY_STARTED
手机本地蓝牙适配器已开启对外部蓝牙设备的扫描
ACTION_STATE_CHANGED
手机本地蓝牙适配器的状态已发生改变
int类型
STATE_TURNING_ON
手机蓝牙适配器正在打开中
STATE_ON
手机蓝牙适配器已打开
STATE_TURNING_OFF
手机蓝牙适配器正在关闭中
STATE_OFF
手机蓝牙适配器已关闭
BlueToothDevice
代表与手机连接的外部蓝牙设备
string类型
ACTION_ACL_CONNECTED
手机与一个外部蓝牙设备建立低级别(ACL)连接成功
ACTION_ACL_DISCONNECTED
手机与一个外部蓝备设备建立起的低级别(ACL)连接断开
ACTION_ACL_DISCONNECT_REQUESTED
已请求外部蓝牙设备进行低级别(ACL)断开连接,并且将很快断开连接
ACTION_BOND_STATE_CHANGED
外部蓝色设备与手机的绑定状态发生改变
ACTION_CLASS_CHANGED
外部蓝牙设备的类别已更改
ACTION_FOUND
发现外部蓝牙设备
ACTION_NAME_CHANGED
外部蓝牙设备的名称是第一次被检测到,或者自上次检测以来已更改
整型
BOND_BONDED
已绑定(配对)外部蓝牙设备
BOND_BONDING
正在与外部蓝牙设备进行绑定(配对)
BOND_NONE
外部蓝牙设备未绑定(配对)
DEVICE_TYPE_CLASSIC
蓝牙设备类型,经典-BR / EDR设备
DEVICE_TYPE_DUAL
蓝牙设备类型,双模式-BR / EDR / LE
DEVICE_TYPE_UNKNOWN
蓝牙设备类型,未知