- 蓝牙开发权限
<!-- 允许程序进行发现和配对新的蓝牙设备-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- 允许程序连接配对过的蓝牙设备-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<!-- 在 Android 6.0 及以上,还需要打开位置权限。如果应用没有位置权限,蓝牙扫描功能不能使用(其它蓝牙操作例如连接蓝牙设备和写入数据不受影响)-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- 初始化操作
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
开启关闭蓝牙
第一种开启蓝牙的方法
bluetoothAdapter.enable();//这个方法没有提示窗口,直接开启蓝牙
第二种开启蓝牙的方法
Intent request = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(request,100);
这种开启方法有交互界面,用户可以选择是否开启蓝牙
关闭蓝牙
bluetoothAdapter.disable();
- 扫描蓝牙设备
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
}
- 注册蓝牙接收者处理蓝牙各种状态监听
/**
* 注册蓝牙广播接收者
*/
private void registBluetoothBroadcastReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
context.registerReceiver(mBroadcastReceiver, filter);
}
- 广播接受者
BluetoothDevice.ACTION_FOUND:每当扫描到蓝牙设备时,就会走这里
case BluetoothDevice.ACTION_FOUND:
//<editor-fold>
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int deviceClassType = device.getBluetoothClass().getDeviceClass();
//找到指定的蓝牙设备
if ((deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
|| deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES)
&& device.getName().equals(PubUtils.deviceName)) {
Log.i(TAG, "Found device:" + device.getName());
mBluetoothDevice = device;
//start bond,开始配对
startMarth();
} else {
Toast.makeText(context.getApplicationContext(), "找不到设备,请确保设备开启被搜寻模式", Toast.LENGTH_LONG).show();
}
if (dialog_match != null && dialog_match.isShowing()) {
dialog_match.dismiss();
}
//</editor-fold>
break;
BluetoothAdapter.ACTION_STATE_CHANGED:蓝牙状态:蓝牙开启关闭时会走这里
case BluetoothAdapter.ACTION_STATE_CHANGED:
//<editor-fold>
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
Log.i(TAG, "BluetoothAdapter is turning on.");
break;
case BluetoothAdapter.STATE_ON:
Log.i(TAG, "BluetoothAdapter is on.");
//蓝牙已打开,开始搜索并连接service
startDiscovery();
getBluetoothA2DP();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.i(TAG, "BluetoothAdapter is turning off.");
break;
case BluetoothAdapter.STATE_OFF:
Log.i(TAG, "BluetoothAdapter is off.");
break;
}
BluetoothDevice.ACTION_BOND_STATE_CHANGED:设备配对时各个状态
case BluetoothDevice.ACTION_BOND_STATE_CHANGED://配对状态
//<editor-fold>
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (bondState) {
case BluetoothDevice.BOND_BONDED: //配对成功
Log.i(TAG, "Device:" + device.getName() + " bonded.");
mBluetoothAdapter.cancelDiscovery(); //取消搜索
PubUtils.bluetoothDevices.setMatchStatus("已配对");
greendaoUtil.updateEntity(PubUtils.bluetoothDevices);
List<BluetoothDevices> arrays = greendaoUtil.queryAllBluetoothDevices();
adapter.setDate(arrays);
connect(); //连接蓝牙设备
break;
//正在配对
case BluetoothDevice.BOND_BONDING:
Log.i(TAG, "Device:" + device.getName() + " bonding.");
break;
//配对不成功
case BluetoothDevice.BOND_NONE:
Log.i(TAG, "Device:" + device.getName() + " not bonded.");
//不知道是蓝牙耳机的关系还是什么原因,经常配对不成功
//配对不成功的话,重新尝试配对
startMarth();
break;
default:
break;
}
- 蓝牙配对
/**
* 开始配对
*/
private void startMarth() {
Log.i(TAG, "createBond");
mBluetoothDevice.createBond();
}
- 查看已配对的蓝牙设备
Set<BluetoothDevice> localSet = bluetoothAdapter.getBondedDevices();
查看手机有没有连接蓝牙耳机
/**
* 查看手机有没有已经连接的蓝牙
* @return
*/
public boolean blootoothConnected() {
if (BluetoothProfile.STATE_CONNECTED == mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)){
return true;
}else{
return false;
}
}