目录
一、蓝牙设置权限
需要在AndroidManifest.xml配置获得蓝牙权限。
按照SDK说明需要下面两个权限就可以进行蓝牙的开发。
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
在android6.0之后为了更好的保护用户的数据安全,所有需要访问硬件唯一标识符的地方都需要申请位置权限,也就是需要申请以下权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
二、蓝牙搜索
1.首先需要获得蓝牙适配器。
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
2.判断蓝牙是否打开,然后请求打开蓝牙
private void openBlueTooth() {
if (!mBluetoothAdapter.isEnabled()) {
//通过这个方法来请求打开我们的蓝牙设备
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent,BLUETOOTH_RESPONSE);
}
}
通过这个方法可以判断是否允许打开蓝牙,然后进行下面相应的操作
case BLUETOOTH_RESPONSE:
if(resultCode == RESULT_OK){
//获得蓝牙权限成功
//需要再写一次,oncreate里的下面的方法无法执行
duringDialog("正在连接蓝牙!");
//搜索蓝牙
searchBlueTooth();
}else if(resultCode == RESULT_CANCELED){
//获得蓝牙权限失败
toast(ControlParkingActivity.this,"蓝牙权限获取失败,请打开蓝牙!");
}
break;
3.搜索蓝牙
如果已经打开蓝牙上面的搜索蓝牙不会执行,所以还需在onCreate方法中写搜索方法
//请求判断蓝牙是否提前打开
//如果没有提前打开则不会执行这句,执行openBluetooth的响应结果
//如果提前打开则执行下面
if(mBluetoothAdapter.isEnabled()){
duringDialog("正在连接蓝牙!");
searchBlueTooth();
}
搜索蓝牙方法如下:
//搜索蓝牙设备
private void searchBlueTooth() {
//判断蓝牙是否已经绑定
if(isBond()){
Log.d(TAG,"蓝牙已经绑定");
//已经绑定,直接连接蓝牙
Log.d(TAG,device.getName() + "1");
}else{
Log.d(TAG,"搜索蓝牙中");
//搜索蓝牙设备
mBluetoothAdapter.startDiscovery();
}
}
三、蓝牙配对
蓝牙配对是通过广播接收器,搜索附近蓝牙设备,通过判断设备的address和name来判断是否为目标设备。之后利用ClsUtils工具类实现自动匹配蓝牙设备的功能。
1.动态注册广播接收器
mBluetoothReceiver = new BluetoothReceiver();
// 动态注册注册广播接收器。接收蓝牙发现讯息
IntentFilter btFilter = new IntentFilter();
btFilter.setPriority(1000);
btFilter.addAction(BluetoothDevice.ACTION_FOUND);
btFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
this.registerReceiver(mBluetoothReceiver,btFilter);
//设置广播信息接口监听器
mBluetoothReceiver.setReceiverMessageListener(this);
setPriority(1000):设置优先级,如果优先级比较低可能仍然会弹出输入pin的对话框
BluetoothDevice.ACTION_FOUND:发现设备时的action
BluetoothDevice.ACTION_PAIRING_REQUEST:当调用createBond()后就会发起BluetoothDevice.ACTION_PAIRING_REQUEST的广播
2.判断蓝牙设备是否为目标设备,并且创建绑定
//获得action
String action=intent.getAction();
//获取设备
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//获取搜索到的设备的名称和地址
String name = device.getName();
String address = device.getAddress();
if(action.equals(BluetoothDevice.ACTION_FOUND))
{
Log.d(TAG, "发现设备:" + name);
if ((name != null && btName.equals(name)) || btAddress.equals(address)) {
//判断远程设备是否已经被绑定
if(device.getBondState() != BluetoothDevice.BOND_BONDED){
Log.d(TAG, "发现目标设备,开始配对!");
try {
// 调用配对的方法,此方法是异步的,系统会触发BluetoothDevice.ACTION_PAIRING_REQUEST的广播
// 收到此广播后,设置配对的密码
ClsUtils.createBond(BluetoothDevice.class, device);
} catch (Exception e) {
e.printStackTrace();
}
}else{
//远程设备已经被绑定,取消搜索,返回信息
}
}
3.利用ClsUtils和事先获得的pin进行配对
else if(action.equals("android.bluetooth.device.action.PAIRING_REQUEST")){
//createBond后再次得到action,就会等于PAIRING_REQUEST
Log.d(TAG,"action2 = " + action);
Log.d(TAG, "发现设备:" + device.getAddress() + " " + device.getName());
if ((name != null && name.equals(btName)) || address.equals(btAddress)) {
Log.d(TAG, "发现目标设备,开始配对!"