Android蓝牙设备的检测
最近,在项目工作中,遇到一个与蓝牙设备相关的问题,需要在特定情况下,检测手机是否有接入其他蓝牙设备。搜了很多资料,都不满足我的功能要求。最后,还是在Android developer官网上找到了解决方案。当时在官网搜索Bluetooth关键字,发现了BluetoothDevice类,通过阅读SDK文档,了解此类的用途,类方法的功能,终于解决了蓝牙设备的检测问题。
1. BluetoothDevice和BluetoothAdapter
BluetoothDevice代表一个远程的蓝牙设备,可以通过它与设备建立连接,获取相关信息,包括设备名称、设备地址和绑定状态。
BluetoothAdapter则表示一个本地的蓝牙适配器,可以通过它实现一些基本的蓝牙相关任务:初始化蓝牙检测,查找绑定设备,初始化蓝牙设备对象等。例如,使用BluetoothAdapter.getRemoteDevice(String address)来获取指定硬件地址的BluetoothDevice对象。
2. 检测蓝牙设备
项目遇到的功能要求是,检测当前的蓝牙设备连接情况,下面是简单的测试代码。
package com.dale.bluetoothtest;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.blue