一种基于Android系统的蓝牙音频设备自适应连接方法转让专利
申请号 : CN201910811608.5
文献号 : CN110519735A
文献日 : 2019-11-29
发明人 : 鲁霖 , 蒋少彬
申请人 : 厦门市思芯微科技有限公司
摘要 :
权利要求 :
1.一种基于Android系统的蓝牙音频设备自适应连接方法, 其特征在于包括以下步骤:步骤一(S101)、蓝牙自连接APP注册BroadcastReceiver;
步骤二(S102)、注册成功后,蓝牙自连接APP初始化获取蓝牙音频设备适配器BluetoothAdapter和BluetoothA2dp;
步骤三(S103)、蓝牙自连接APP开启移动数据终端蓝牙连接模块搜索功能,并启动搜索超时定时器;
步骤四(S104)、蓝牙自连接APP对已搜索到的蓝牙音频设备与预设设备名称进行匹配;
若匹配,则停止搜索;若未匹配,则继续搜索直至超时取消搜索;
步骤五(S105)、蓝牙自连接APP检测蓝牙音频设备绑定状态;
步骤六(S106)、蓝牙自连接APP若检测到蓝牙音频设备未绑定,则执行绑定操作;
步骤七(S107)、蓝牙自连接APP监听已绑定的蓝牙音频设备状态,开启连接超时倒计时;
步骤八(S108)、在倒计时结束之前,蓝牙自连接APP若无法连接上蓝牙音频设备,则开启反射机制连接蓝牙音频设备。
2.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤一(S101)为:蓝牙自连接APP注册BroadcastReceiver,其中含有的广播:BluetoothDevice.ACTION_FOUND(发现蓝牙音频设备广播)BluetoothDevice.ACTION_BOND_STATE_CHANGED(蓝牙音频设备绑定状态变化)BluetoothDevice.ACTION_PAIRING_REQUEST(蓝牙音频设备配对码请求)BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED(蓝牙音频设备连接状态变化)。
3.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤二(S102)为:初始化获取BluetoothAdapter对象:mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();获取BluetoothA2dp对象:
new BluetoothProfile.ServiceListener() { public void onServiceConnected(int profile, BluetoothProfile proxy) { mBluetoothA2dp = (BluetoothA2dp) proxy; }
public void onServiceDisconnected(int profile) { }
}。
4.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤三(S103)为:开启搜索功能的方位:
mBluetoothA2dp. startDiscovery();
超时取消搜索的方法:
Observable.timer(5, TimeUnit.SECONDS).subscribe(aLong -> { mBluetoothAdapter.cancelDiscovery(); })。
5.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤四(S104)为:获取搜索到的蓝牙音频设备方法:
蓝牙自连接APP通过监听BluetoothDevice.ACTION_FOUND上报;
if (BluetoothDevice.ACTION_FOUND.equals(action)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if(device.getName == name){ mDevice = device;}
}。
6.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤五(S105)为:检测绑定状态的方法:
通过mBluetoothAdapter获取所有已绑定蓝牙音频设备:public boolean isBond(String mac) { Set
}
return false; }。
7.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤六(S106)为:绑定操作的方法:
bluetoothDevice.createBond()。
8.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤七(S107)为:蓝牙自连接APP监听BluetoothDevice.ACTION_BOND_STATE_CHANGED广播;在收到BluetoothDevice.ACTION_BOND_STATE_CHANGED广播后,通过intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);获取state为BluetoothDevice.BOND_BONDED之后开启倒计时;若在倒计时结束前无法接收到BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED广播且state为state == BluetoothA2dp.STATE_CONNECTING,则开始步骤八(S108);
倒计时方法:
Message message = new Message(); message.what = MSG_WHAT_A2DP_CONNECT; Bundle bundle = new Bundle(); bundle.putParcelable("device", device); message.setData(bundle); handler.sendMessageDelayed(message, 2000)。
9.根据权利要求1所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于步骤八(S108)为:采用反射机制再次进行连接:
先从BluetoothA2dp中获取它的类:Class
Class
Method connect = bluetoothA2dpClass.getMethod("connect", BluetoothDevice.class); connect.invoke(mBluetoothA2dp, device);} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace();}
try {
Method setPriority = bluetoothA2dpClass.getMethod("setPriority", BluetoothDevice.class, int.class); setPriority.invoke(mBluetoothA2dp, device, 1000);} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace();}。
10.根据权利要求1至9任一项所述的一种基于Android系统的蓝牙音频设备自适应连接方法,其特征在于:所述移动数据终端为台式电脑、智能手机、平板电脑、笔记本电脑、智能穿戴设备。
说明书 :
一种基于Android系统的蓝牙音频设备自适应连接方法
技术领域
背景技术
发明内容
步骤一(S101)、蓝牙自连接APP注册BroadcastReceiver;
步骤二(S102)、注册成功后,蓝牙自连接APP初始化获取蓝牙音频设备适配器BluetoothAdapter和BluetoothA2dp;
步骤三(S103)、蓝牙自连接APP开启移动数据终端蓝牙连接模块搜索功能,并启动搜索超时定时器;
步骤四(S104)、蓝牙自连接APP对已搜索到的蓝牙音频设备与预设设备名称进行匹配;
若匹配,则停止搜索;若未匹配,则继续搜索直至超时取消搜索;
步骤五(S105)、蓝牙自连接APP检测蓝牙音频设备绑定状态;
步骤六(S106)、蓝牙自连接APP若检测到蓝牙音频设备未绑定,则执行绑定操作;
步骤七(S107)、蓝牙自连接APP监听已绑定的蓝牙音频设备状态,开启连接超时倒计时;
步骤八(S108)、在倒计时结束之前,蓝牙自连接APP若无法连接上蓝牙音频设备,则开启反射机制连接蓝牙音频设备。
BluetoothDevice.ACTION_BOND_STATE_CHANGED(蓝牙音频设备绑定状态变化)BluetoothDevice.ACTION_PAIRING_REQUEST(蓝牙音频设备配对码请求)BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED(蓝牙音频设备连接状态变化)。
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
获取BluetoothA2dp对象:
new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
}
public void onServiceDisconnected(int profile) {
}
}
mBluetoothA2dp. startDiscovery();
超时取消搜索的方法:
Observable.timer(5, TimeUnit.SECONDS).subscribe(aLong -> {
mBluetoothAdapter.cancelDiscovery();
})
蓝牙自连接APP通过监听BluetoothDevice.ACTION_FOUND上报;
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getName == name){
mDevice = device;
}
}
通过mBluetoothAdapter获取所有已绑定蓝牙音频设备:
public boolean isBond(String mac) {
Set
for (BluetoothDevice device : bondedDevices) {
if (mac.equals(device.getAddress())) {
return true;
}
}
return false;
}
bluetoothDevice.createBond()
倒计时方法:
Message message = new Message();
message.what = MSG_WHAT_A2DP_CONNECT;
Bundle bundle = new Bundle();
bundle.putParcelable("device", device);
message.setData(bundle);
handler.sendMessageDelayed(message, 2000)
先从BluetoothA2dp中获取它的类:
Class
然后从类中获取方法“connect”和“setPriority”进行连接;
Class
try {
Method connect = bluetoothA2dpClass.getMethod("connect",
BluetoothDevice.class);
connect.invoke(mBluetoothA2dp, device);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
}
try {
Method setPriority = bluetoothA2dpClass.getMethod("setPriority", BluetoothDevice.class, int.class);
setPriority.invoke(mBluetoothA2dp, device, 1000);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
}
附图说明
具体实施方式
BluetoothDevice.ACTION_FOUND(发现蓝牙音频设备广播)
BluetoothDevice.ACTION_BOND_STATE_CHANGED(蓝牙音频设备绑定状态变化)BluetoothDevice.ACTION_PAIRING_REQUEST(蓝牙音频设备配对码请求)BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED(蓝牙音频设备连接状态变化)。
获取BluetoothA2dp对象:
new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
}
public void onServiceDisconnected(int profile) {
}
}
超时取消搜索的方法:
Observable.timer(5, TimeUnit.SECONDS).subscribe(aLong -> {
mBluetoothAdapter.cancelDiscovery();
})
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getName == name){
mDevice = device;
}
}
public boolean isBond(String mac) {
Set
for (BluetoothDevice device : bondedDevices) {
if (mac.equals(device.getAddress())) {
return true;
}
}
return false;
}
bluetoothDevice.createBond()
Message message = new Message();
message.what = MSG_WHAT_A2DP_CONNECT;
Bundle bundle = new Bundle();
bundle.putParcelable("device", device);
message.setData(bundle);
handler.sendMessageDelayed(message, 2000)
Class
然后从类中获取方法“connect”和“setPriority”进行连接;
Class
try {
Method connect = bluetoothA2dpClass.getMethod("connect",
BluetoothDevice.class);
connect.invoke(mBluetoothA2dp, device);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
}
try {
Method setPriority = bluetoothA2dpClass.getMethod("setPriority", BluetoothDevice.class, int.class);
setPriority.invoke(mBluetoothA2dp, device, 1000);
} catch (NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
e.printStackTrace();
}