文章目录
先附上官方文档(中文版):https://developer.android.google.cn/guide/topics/connectivity/bluetooth?hl=zh_cn#DiscoverDevices
前言
蓝牙作为安卓应用中很重要的一块,可以用于数据传输
一、蓝牙声明权限
在清单文件声明权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- ACCESS_FINE_LOCATION:允许一个程序访问精确位置(如GPS) -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!-- ACCESS_COARSE_LOCATION:允许一个程序访问CellID或WiFi热点来获取大致的位置 -->
二、打开蓝牙和搜索已配对设备
1.获取bluetoothAdapter对象
//静态的获取蓝牙适配器
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
//不支持
}
2.打开蓝牙
/**
* 打开蓝牙方法
*/
public void openBlueTooth(BluetoothAdapter bluetoothAdapter) {
if(!bluetoothAdapter.isEnabled()){
Intent enableIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
}else{
AlertDialog alertDialog=new AlertDialog.Builder(context).setTitle("已开").show();
}
}
3.获取已配对设备
Set<BluetoothDevice> pairedDevices ;//已配对设备
/**
* 搜索已配对设备
* @param bluetoothAdapter
*/
public void searchAlreadyDevice(BluetoothAdapter bluetoothAdapter) {
//检测已经配对的设备
pairedDevices= bluetoothAdapter.getBondedDevices();
/**
* 如果不支持则返回null
*/
if (pairedDevices.size() > 0) {
mBluetoothAdapter.emptyAadpter();
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
deviceNames.add(deviceName);
String deviceHardwareAddress = device.getAddress(); // MAC address
deviceMacs.add(deviceHardwareAddress);
devices.add(device);
}
}
mBluetoothAdapter.updataList(deviceNames,deviceMacs);
}
三、将蓝牙设置为可被检测状态
/**
* 使自己设备300秒可见
* 注意:如果尚未在设备上启用蓝牙,则启用设备可检测性会自动启用蓝牙。
*/
public void canScan() {
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
四、搜索设备
1.定义一个广播,用来发现设备
/**
* 创建查找设备监听器
*/
public BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//找到设备
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
if (deviceName == null) {
} else {
deviceNames.add(deviceName);
deviceMacs.add(deviceHardwareAddress);
devices.add(device);
mBluetoothAdapter.updataList(deviceNames, deviceMacs);
}
}
/**
* 蓝牙开始搜索时进行初始化
*/
else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
mBluetoothAdapter.emptyAadpter();
mBluetoothAdapter.updataList(deviceNames, deviceMacs);
}
//结束搜索时调用
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mBluetoothAdapter.emptyAadpter();
mBluetoothAdapter.updataList(deviceNames, deviceMacs);
}
}
};
2.开始搜索
bluetoothAdapter.startDiscovery();
这里还不太清楚需不需要使用多线程,大家可以试一下。
文章蓝牙(二):