算是做了n年的智能穿戴BLE开发了, 首先对国内的安卓开发者提醒下 , BLE开发是真的很坑, 特别是安卓, ios端也坑, 但没安卓坑 因为国产有很多手机 各种奇葩兼容都有,
其实这些方案我很早就写到云笔记里了,一直没公开, 这里的解决方案大部分都是网上搜不到 或者网上搜到类似的问题, 但回复基本上是回答[无法解决] 或 [重启手机解决]等 没意义的解决办法,让我很无语…
以下内容可能涉及到各种系统类源码
你可以通过这里阅读 https://www.androidos.net.cn/
废话不多说, 希望对你们有用
1.刷新蓝牙app的状态
#####问题描述:
某些手机用久了会出现扫描不到任何设备的bug,
此时是因为手机误认为本app不是[ble类] app , f**k!!! 还有这种操作???
但值得注意的是, 这只是一种原因,[ 扫描不到任何设备的bug] 有很多种原因, 详情请看第3点
#####解决方案:
[目前网上没有与我类似的解决办法, 所以具体副作用自测]
参考 IBluetoothManager.aidl 系统源码
出现该问题时于是通过查看系统源码找到isBleAppPresent 方法 ,反射调用其后居然返回false ,
换了一台能正常使用的手机 调用该方法 返回true 因此证实了这个问题,
然后发现系统有私有的updateBleAppCount方法, 可以刷新ble类app的状态,反射调用之…
因此解决了 [偶尔ble设备扫描不出来]的bug),
通过传入你的app包名 以 刷新 蓝牙app的错误状态
public static void refreshBleAppFromSystem(Context context, String packageName) {
//6.0以上才有该功能,不是6.0以上就算了
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
return;
}
if (!adapter.isEnabled()) {
return;
}
try {
Object mIBluetoothManager = getIBluetoothManager(adapter);
Method isBleAppPresentM = mIBluetoothManager.getClass().getDeclaredMethod("isBleAppPresent");
isBleAppPresentM.setAccessible(true);
boolean isBleAppPresent = (Boolean) isBleAppPresentM.invoke(mIBluetoothManager);
if (isBleAppPresent) {
return;
}
Field mIBinder = BluetoothAdapter.class.getDeclaredField("mToken");
mIBinder.setAccessible(true);
Object mToken = mIBinder.get(adapter);
//刷新偶尔系统无故把app视为非 BLE应用 的错误标识 导致无法扫描设备
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//8.0+ (部分手机是7.1.2 也是如此)
Method updateBleAppCount = mIBluetoothManager.getClass().getDeclaredMethod("updateBleAppCount", IBinder.class, boolean.class, String.class);
updateBleAppCount.setAccessible(true);
//关一下 再开
updateBleAppCount.invoke(mIBluetoothManager, mToken, false, packageName);
updateBleAppCount.invoke(mIBluetoothManager, mToken, true, packageName);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
//6.0~7.1.1
Method updateBleAppCount = mIBluetoothManager.getClass().getDeclaredMethod("updateBleAppCount", IBinder.class, boolean.class);
updateBleAppCount.setAccessible(true);
//关一下 再开
updateBleAppCount.invoke(mIBluetoothManager, mToken, false);
updateBleAppCount.invoke(mIBluetoothManager, mToken, true);
} catch (NoSuchMethodException e) {
//8.0+ (部分手机是7.1.2 也是如此)
try {
Method updateBleAppCount = mIBluetoothManager.getClass().getDeclaredMethod("updateBleAppCount", IBinder.class, boolean.class, String.class);
updateBleAppCount.setAccessible(true);
//关一下 再开
updateBleAppCount.invoke(mIBluetoothManager, mToken, false, packageName);
updateBleAppCount.invoke(mIBluetoothManager, mToken, true, packageName);
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
}
}
}
} catch (Throwable e)