• Home
Name Date Size #Lines LOC

..--

AppScope/22-Oct-2025-3532

entry/22-Oct-2025-2,8292,611

hvigor/22-Oct-2025-3836

screenshots/22-Oct-2025-

.gitignoreD22-Oct-2025133 1212

README.mdD22-Oct-20256.9 KiB151113

build-profile.json5D22-Oct-20251.4 KiB5857

code-linter.json5D22-Oct-2025958 3534

hvigorfile.tsD22-Oct-2025234 75

oh-package.json5D22-Oct-2025809 2624

ohosTest.mdD22-Oct-20256 KiB3330

README.md

1# 蓝牙示例代码工程化
2
3### 介绍
4
5本示例为开发指南中[蓝牙服务开发](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/connectivity/bluetooth/Readme-CN.md)章节中**蓝牙设置**,**广播与扫描**,**通用属性协议**示例代码的完整工程,该工程中展示的代码详细描述可查如下链接:
6
7[蓝牙设置](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/connectivity/bluetooth/br-development-guide.md)
8[广播与扫描](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/connectivity/bluetooth/ble-development-guide.md)
9[通用属性协议](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/connectivity/bluetooth/gatt-development-guide.md)
10
11### 效果预览
12|Scan界面| Bonded界面 | Adveriser界面| Switch界面|
13|-------|---------|--------|----------|
14|<img src="./screenshots/SCAN.jpeg" width="360;" /> |<img src="./screenshots/BONDED.jpeg" width="360;" /> |<img src="./screenshots/ADVERISER.jpeg" width="360;" /> |<img src="./screenshots/SWITCH.jpeg" width="360;" /> |
15
16### 使用说明
17
181. 启动应用,蓝牙权限申请点击同意。
192. 切换到SWITCH页面,单击开启蓝牙,等待上方蓝牙状态变为BlueTooth State: Connected。
203. 在SCAN页面单击设置过滤参数,可以设置扫描的过滤参数。
214. 单击开始扫描,设备开始扫描设备,扫描到的设备ID会以弹窗形式展示,单击刷新,可以更新下方的设备列表。
225. 单击停止扫描,停止扫描设备。
236. 单击设备列表里的connect,连接设备,设备信息会在上方循环播放展示。
247. 切换GATT页面,填写读写特征值和描述符的过滤参数,上方输入框为写特征值和描述符的过滤参数。
258. 单击发现服务,可以获取已连接设备的服务列表,获得的内容在上方显示。
269. 单击写特征值或写描述符,根据上方过滤参数写入特征值和描述符。
2710. 单击读特征值或描述符,读取特征值和描述符,读取到的内容以弹窗形式显示。
2811. 单击断开连接,断开与当前设备的连接。
2912. 在Adveriser界面,单击注册服务,可以注册服务,单击注销服务,可以注销服务。
3013. 单击订阅读写特征值可以订阅读写特征值的操作。
3114. 单击设置广播内容,在打开的弹窗中设置广播的内容和参数。
3215. 单击开始广播,设备开始进行广播操作。
3316. 单击暂停广播,设备暂时停止广播。
3417. 单击继续广播,设备继续进行广播。
3518. 单击关闭广播,设备停止广播操作,关闭广播服务。
3619. 切换到SWITCH界面,单击关闭蓝牙,关闭设备的蓝牙服务。
37
38### 工程目录
39```
40+--- src
41|   +--- main
42|   |   +--- ets
43|   |   |   +--- bluetoothService
44|   |   |   |   +--- BluetoothAdvertising.ets
45|   |   |   |   +--- BluetoothScan.ets
46|   |   |   |   +--- BluetoothSwitch.ets
47|   |   |   |   +--- GattClientManager.ets
48|   |   |   |   +--- GattServerManager.ets
49|   |   |   +--- common
50|   |   |   |   +--- AdvertisingDialog.ets
51|   |   |   |   +--- ScanData.ets
52|   |   |   |   +--- ScanDialog.ets
53|   |   |   |   +--- ToastReport.ets
54|   |   |   +--- entryability
55|   |   |   |   +--- EntryAbility.ets
56|   |   |   +--- entrybackupability
57|   |   |   |   +--- EntryBackupAbility.ets
58|   |   |   +--- pages
59|   |   |   |   +--- Index.ets
60
61```
62
63### 具体实现
64
65#### 蓝牙设置
66
671. import需要的access模块。
68
692. 需要SystemCapability.Communication.Bluetooth.Core系统能力。
70
713. 开启蓝牙。
72
734. 关闭蓝牙。
74
755. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md)76
776. 如何验证: 执行开启蓝牙代码,记录日志“bluetooth statues: STATE_ON”,则表示开启蓝牙成功。执行关闭蓝牙代码,记录日志“bluetooth statues: STATE_OFF”,则表示蓝牙关闭成功。
78
79#### 广播与扫描开发指导
80
81##### 开启/关闭广播
82
831. import需要的ble模块。
842. 开启设备的蓝牙。
853. 需要SystemCapability.Communication.Bluetooth.Core系统能力。
864. 开启广播,对端设备扫描该广播。
875. 关闭广播。
886. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md)89
90##### 开启/关闭扫描
91
921. import需要的ble模块。
932. 开启设备的蓝牙。
943. 需要SystemCapability.Communication.Bluetooth.Core系统能力。
954. 对端设备开启广播。
965. 本端设备开启扫描,获取扫描结果。
976. 关闭扫描。
987. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md)99
100#### 通用属性协议开发指导
101
102##### 连接server端读取和写入信息
103
1041. import需要的ble模块。
1052. 创建gattClient实例对象。
1063. 连接gattServer。
1074. 读取gattServer的特征值和描述符。
1085. 向gattServer写入特征值和描述符。
1096. 断开连接,销毁gattClient实例。
1107. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md)111
112##### server端操作services和通知客户端信息
113
1141. import需要的ble模块。
1152. 创建gattServer实例对象。
1163. 添加services信息。
1174. 当向gattServer写入特征值通知gattClient。
1185. 移除services信息。
1196. 注销gattServer实例。
1207. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md)121
122### 相关权限
123
124[ohos.permission.USE_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionuse_bluetooth)
125
126[ohos.permission.DISCOVER_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissiondiscover_bluetooth)
127
128[ohos.permission.ACCESS_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all-user.md#ohospermissionaccess_bluetooth)
129### 依赖
130
131不涉及
132
133### 约束与限制
134
1351.本示例仅支持标准系统上运行, 支持设备:RK3568。
136
1372.本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.57,镜像版本号:OpenHarmony_5.0.2.57。
138
1393.本示例需要使用DevEco Studio NEXT Developer Preview2 (Build Version: 5.0.5.306, built on December 12, 2024)及以上版本才可编译运行。
140
141### 下载
142
143如需单独下载本工程,执行如下命令:
144
145````
146git init
147git config core.sparsecheckout true
148echo code/DocsSample/bluetoothSample/ > .git/info/sparse-checkout
149git remote add origin https://gitee.com/openharmony/applications_app_samples.git
150git pull origin master
151````