6.0的蓝牙已经开发完毕,因为可以得到系统6.0的jar包,so也就开发的快些,更好些。4.4因为代码部分在setting模块里,部分在系统中,so不方便打成jar包。所以就通过广播接受改变状态之类的。也算鼓捣出来一款。
![](https://i-blog.csdnimg.cn/blog_migrate/d2982d14249f622e4a0326b91be67560.png)
开发的思路:
1.开关蓝牙
2.扫描到蓝牙列表和得到配对列表
下面关于连接蓝牙:
A2dp:这个是蓝牙音频传输协议
AVRCP:输入设备控制协议
首先要弄清当前蓝牙的功能属于那种协议,具体代码判断协议的方法不清楚。(本项目用不到)
3.连接蓝牙
4.断开蓝牙与取消配对
1.开关蓝牙
/**
* 打开蓝牙or关闭蓝牙
* @param flag true:open
*/
public void openOrCloseBluetooth(boolean flag) {
if (flag) {
mBluetoothAdapter.enable();
} else {
mBluetoothAdapter.disable();
}
}
2.扫描到蓝牙列表和得到配对列表
扫描蓝牙
//开始扫描
public void startScan() {
if (mBluetoothAdapter != null) {
// scanResultList.clear();
if (mBluetoothAdapter.isDiscovering()) {
Log.d(TAG, "startScan: cancelDiscovery");
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
Log.d(TAG, "startScan: 开始扫描周围蓝牙");
} else {
Log.d(TAG, "startScan: 扫描周围蓝牙异常");
}
}
扫描之后如何获取扫描结果主要通过广播获取
private void registerBluetoothBroadcastReceiver(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙状态改变的广播
filter.addAction(BluetoothDevice.ACTION_FOUND);//找到设备的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//搜索完成的广播
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始扫描的广播
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
if (mReceiver == null) {
mReceiver = new BluetoothBroadCasteReceiver();
}
Log.d(TAG, "registerBluetoothBroadcastReceiver: 注册蓝牙广播" + mBluetoothAdapter.isEnabled());
context.registerReceiver(mReceiver, filter);
}
接受器中主要处理这三个状态
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Log.d(TAG, "onReceive: 开始扫描。");
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.d(TAG, "onReceive: 扫描结束。");
if (callBack != null) {
//callBack.scan_result(scanResultList, getHasConnectBlueDevices());
callBack.scanEnd();
}
break;
case BluetoothDevice.ACTION_CLASS_CHANGED:
Log.d(TAG, "onReceive: 一个已经改变的远程设备的蓝牙类。");
break;
case BluetoothDevice.ACTION_FOUND:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothDeviceBean bluetoothDeviceBean = new BluetoothDeviceBean(device, Bluetooth_State.State_BOND_NONE);
if (device != null) {
//发现的设备统一给一个未配对的状态
if (callBack != null) {
callBack.foundDevice(bluetoothDeviceBean, getHasConnectBlueDevices());
}
Log.d(TAG, "onReceive: 远程设备发现。" + device.getName() + "??" + device.getAddress() + "???" + device.getBondState());
}
break;
tips:
1.扫描蓝牙的时候,扫描结束的广播等待时间较长,为了页面体验就在发现设备的广播中,做数据的收集,但注意的是同一个设备会多次被发现,所以数据要做一些扁平化处理
2.扫描的结果也包含了已经配对的,so数据应该做一些处理
获得已经配对的列表
/**
* 获得已经配对的蓝牙列表
* @return
*/
public Set<BluetoothDeviceBean> getHasConnectBlueDevices() {
Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
Set<BluetoothDeviceBean> bluetoothDeviceBeans = new LinkedHashSet<>();
if (bondedDevices != null) {
for (BluetoothDevice de :
bondedDevices) {
bluetoothDeviceBeans.add(new BluetoothDeviceBean(de, Bluetooth_State.State_BOND_BONDED));
}
Log.e(TAG, "getHasConnectBlueDevices: 已经配对的蓝牙设备" + bluetoothDeviceBeans.toString());
return bluetoothDeviceBeans;
} else {
Log.e(TAG, "没有已经配对的蓝牙设备");
return bluetoothDeviceBeans;
}
}
tips:已经配对的列表中及时当前的设备没有连接也会显示。
3.连接蓝牙
连接蓝牙主要分为两部分,一部分是配对,配对完成后再连接
配对:
/**
* 蓝牙配对
* @param btClass
* @param btDevice
*/
public void createBond(Class btClass, BluetoothDevice btDevice) {
Method createBondMethod = null;
try {
createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
} catch (NoSuchMethodException e) {
e.printStackTrace();
Log.d(TAG, "createBond: XXXX");
} catch (IllegalAccessException e) {
e.printStackTrace();
Log.d(TAG, "createBond: XXXX1");
} catch (InvocationTargetException e) {
Log.d(TAG, "createBond: XXXX2");
e.printStackTrace();
}
}
在广播中得到配对的状态:
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
switch (state) {
case BluetoothDevice.BOND_NONE:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "BOND_NONE 删除配对" + device.getName());
if (callBack != null) {
callBack.update_state(new BluetoothDeviceBean(device, Bluetooth_State.State_BOND_NONE));
}
break;
case BluetoothDevice.BOND_BONDING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "BOND_BONDING 正在配对" + device.getName());
if (callBack != null) {
callBack.update_state(new BluetoothDeviceBean(device, Bluetooth_State.State_BOND_BONDING));
}
break;
case BluetoothDevice.BOND_BONDED:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "BOND_BONDED 配对成功" + device.getName());
if (callBack != null) {
callBack.update_state(new BluetoothDeviceBean(device, Bluetooth_State.State_BOND_BONDED));
}
//连接操作
conectBluetoothDevice(device);
break;
}
break;
收到配对成功的广播进行连接:
A2dp连接:
//连接蓝牙
public void conectBluetoothDevice(BluetoothDevice device) {
if (mA2dpService != null) {
try {
Log.d(TAG, "createBond: 配对成功开始连接");
mA2dpService.connect(device);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
输入控制设备的连接:
/**
*
* @param device
* @param isConnect true:连接 false:断开
*/
private void connectOrDisConnectHidBT(BluetoothDevice device, boolean isConnect) {
if (mHidService != null) {
//连接
try {
String flag = "";
if (isConnect) {
flag = "connect";
} else {
flag = "disconnect";
}
if (mHidService.getProfile() == getInputDeviceHiddenConstant()) {
if (device != null) {
//得到BluetoothInputDevice然后反射connect连接设备
Method method = mHidService.getProxy().getClass().getMethod(flag,
new Class[]{BluetoothDevice.class});
method.invoke(mHidService.getProxy(), device);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
}
/**
* 获取BluetoothProfile中hid的profile,"INPUT_DEVICE"类型隐藏,需反射获取
* @return
*/
@SuppressLint("NewApi")
public static int getInputDeviceHiddenConstant() {
Class<BluetoothProfile> clazz = BluetoothProfile.class;
for (Field f : clazz.getFields()) {
int mod = f.getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod)
&& Modifier.isFinal(mod)) {
try {
if (f.getName().equals("INPUT_DEVICE")) {
return f.getInt(null);
}
} catch (Exception e) {
}
}
}
return -1;
}
tips:连接蓝牙的时候,需要开启一个服务,通过广播接受来判定是否连接成功
A2dp的服务:
private void initA2dpService() {
Intent i = new Intent(IBluetoothA2dp.class.getName());
context.bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}
IBluetoothA2dp mA2dpService;
public ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
try {
mA2dpService = IBluetoothA2dp.Stub.asInterface(service);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
switch (intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)) {
case BluetoothA2dp.STATE_CONNECTING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "BluetoothA2dp.STATE_CONNECTING: " + device.getName() + " connecting");
if (callBack != null) {
callBack.update_state(new BluetoothDeviceBean(device, Bluetooth_State.STATE_CONNECTING