Android开启支持蓝牙ble
一、Android7.1打开蓝牙ble
1.system/bt目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | diff --git a/include/bt_target.h b/include/bt_target.h old mode 100644 new mode 100755 index b0cf856..d4d1556 --- a/include/bt_target.h +++ b/include/bt_target.h @@ -688,14 +688,14 @@ * resolution, local address rotation etc. */ #ifndef BLE_PRIVACY_SPT -#define BLE_PRIVACY_SPT FALSE +#define BLE_PRIVACY_SPT TRUE #endif /* * Enables or disables support for local privacy (ex. address rotation) */ #ifndef BLE_LOCAL_PRIVACY_ENABLED -#define BLE_LOCAL_PRIVACY_ENABLED TRUE +#define BLE_LOCAL_PRIVACY_ENABLED FALSE #endif /* @@ -703,7 +703,7 @@ * feature discovery, multi-adv etc. */ #ifndef BLE_VND_INCLUDED -#define BLE_VND_INCLUDED FALSE +#define BLE_VND_INCLUDED TRUE #endif |
不加以上这些修改,可以搜到ble设备,但是会连接不成功。
2.farmeworks/base目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -909,13 +909,16 @@ public final class BluetoothAdapter { * @ return true if Multiple Advertisement feature is supported */ public boolean isMultipleAdvertisementSupported() { - if (getState() != STATE_ON) return false ; + /* + if (getState() != STATE_ON) return false; try { return mService.isMultiAdvertisementSupported(); } catch (RemoteException e) { Log.e(TAG, "failed to get isMultipleAdvertisementSupported, error: ", e); } return false; + */ + return true ; } |
1 2 3 4 5 6 7 8 9 10 11 | --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1102,7 +1102,7 @@ < bool name= "config_bluetooth_address_validation" > false </ bool > <!-- Boolean indicating if current platform supports BLE peripheral mode --> - < bool name= "config_bluetooth_le_peripheral_mode_supported" > false </ bool > + < bool name= "config_bluetooth_le_peripheral_mode_supported" > true </ bool > <!-- Max number of scan filters supported by blutooth controller. 0 if the device does not support hardware scan filters--> |
加上些,就可以正常搜索ble设备并连接成功了。
二、Android11打开蓝牙ble
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 29a98faf5cd1..8d46b3415fe0 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -1929,7 +1929,7 @@ public final class BluetoothAdapter { * @return true if Multiple Advertisement feature is supported */ public boolean isMultipleAdvertisementSupported() { - if (getState() != STATE_ON) { + /* if (getState() != STATE_ON) { return false; } try { @@ -1943,6 +1943,8 @@ public final class BluetoothAdapter { mServiceLock.readLock().unlock(); } return false; + */ + return true; } /** diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index c1e3026ee45c..be64201f9cf9 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1683,7 +1683,7 @@ <bool name="config_bluetooth_address_validation">false</bool> <!-- Boolean indicating if current platform supports BLE peripheral mode --> - <bool name="config_bluetooth_le_peripheral_mode_supported">false</bool> + <bool name="config_bluetooth_le_peripheral_mode_supported">true</bool> <!-- Boolean indicating if current platform supports HFP inband ringing --> <bool name="config_bluetooth_hfp_inband_ringing_support">false</bool>
注意点:手机端搜索蓝牙ble的时候,手机相当于是客户端,需要有一个服务端创建出服务,手机这边才能搜索到蓝牙设备。所以一般都会有两个app,主板app(服务端),手机app(客户端)。这个地方如果不清楚,只在手机app端有安装,是不会搜索ble蓝牙服务的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!