diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/.gitignore b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/app.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..82ba5bdf7e2a818246858c5854b029f866ee01e0 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "bundleName": "com.example.broadcastandscan", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/element/string.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0efcfcb22e2e47460a097725332c45d37114caf3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "BroadcastAndScan" + } + ] +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/media/app_icon.png b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/AppScope/resources/base/media/app_icon.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/README_zh.md b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..23c6ce6ec6ea466f6cbd3db1c16f324520f04886 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/README_zh.md @@ -0,0 +1,98 @@ +# 广播与扫描示例 + +### 介绍 + +广播与扫描,主要提供了蓝牙设备的开启广播、关闭广播、开启扫描、关闭扫描方法,通过广播和扫描发现对端蓝牙设备,实现低功耗的通信。 + + +### 场景介绍 + +主要场景有: + +1. 开启、关闭广播 + +2. 开启、关闭扫描 + +### 接口说明 + +完整的 JS API 说明以及实例代码请参考:[BLE接口](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-bluetooth-ble.md) + +具体接口说明如下表。 + +| 接口名 | 功能描述 | +|----------------|------------| +| startBLEScan() | 发起BLE扫描流程。 | +| stopBLEScan() | 停止BLE扫描流程。 | +|startAdvertising() |开始发送BLE广播。| +|disableAdvertising() |临时停止BLE广播。| +|enableAdvertising()| 临时启动BLE广播。| +|stopAdvertising()| 停止发送BLE广播。| +|on(type: 'advertisingStateChange') |订阅BLE广播状态。| +|off(type: 'advertisingStateChange') |取消订阅BLE广播状态。| +|on(type: 'BLEDeviceFind') |订阅BLE设备发现上报事件。| +|off(type: 'BLEDeviceFind')| 取消订阅BLE设备发现上报事件| + +### 主要场景开发步骤 + +#### 开启、关闭广播 + +1. import需要的ble模块。 +2. 开启设备的蓝牙。 +3. 需要SystemCapability.Communication.Bluetooth.Core系统能力。 +4. 开启广播,对端设备扫描该广播。 +5. 关闭广播。 +6. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md). + +#### 开启、关闭扫描 + +1. import需要的ble模块。 +2. 开启设备的蓝牙。 +3. 需要SystemCapability.Communication.Bluetooth.Core系统能力。 +4. 对端设备开启广播。 +5. 本端设备开启扫描,获取扫描结果。 +6. 关闭扫描。 +7. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md). + + + +### 工程目录 +``` +entry/src/main/ets/ +|---entryability +| |---BluetoothAdvertising.ets +| |---BluetoothScan.ets +| |---EntryAbility.ets +|---entrybackupability +| |---EntryBackupAbility.ets +|---pages +| |---Index.ets // 应用主页面 +| |---Broadcast.ets +``` + +### 相关权限 + +需要开启蓝牙权限。 + +### 依赖 + +不涉及。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行, 支持设备:RK3568。 + +2.本示例为Stage模型,支持API14版本SDK,版本号:5.0.2.57,镜像版本号:OpenHarmony_5.0.2.57。 + +3.本示例需要使用DevEco Studio NEXT Developer Preview2 (Build Version: 5.0.5.306, built on December 12, 2024)及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +```` +git init +git config core.sparsecheckout true +echo code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/ > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +```` \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/build-profile.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..cb47ca373192309ff61b3628aa16fa1eb6a4f904 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/build-profile.json5 @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "app": { + "signingConfigs": [ + { + "name": "default", + "material": { + "certpath": "C:/Users/yangyanshuo/.ohos/config/openharmony/default_BroadcastAndScan_zq9yv2FaKXYTcyExv7LBOwDMYpgNl62pBIaKub2sqhU=.cer", + "storePassword": "0000001A4FE5363C549E0F33FFE711F101B169C56AC65028469DCB7AF69E8780E71DFC540D57D1BA085C", + "keyAlias": "debugKey", + "keyPassword": "0000001AEDBCE163D40B08D7BB3BDDC1D6869547DCF80858D58F2634AAE615C20A427724C3B2B704EA6A", + "profile": "C:/Users/yangyanshuo/.ohos/config/openharmony/default_BroadcastAndScan_zq9yv2FaKXYTcyExv7LBOwDMYpgNl62pBIaKub2sqhU=.p7b", + "signAlg": "SHA256withECDSA", + "storeFile": "C:/Users/yangyanshuo/.ohos/config/openharmony/default_BroadcastAndScan_zq9yv2FaKXYTcyExv7LBOwDMYpgNl62pBIaKub2sqhU=.p12" + } + } + ], + "products": [ + { + "name": "default", + "signingConfig": "default", + "compileSdkVersion": 14, + "compatibleSdkVersion": 14, + "targetSdkVersion": 14, + "runtimeOS": "OpenHarmony", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/code-linter.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/code-linter.json5 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/.gitignore b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/build-profile.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/hvigorfile.ts b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/obfuscation-rules.txt b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/oh-package.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c9cb6c8174858277c9b0d465a51547dcab16d5ff --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothAdvertising.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothAdvertising.ets new file mode 100644 index 0000000000000000000000000000000000000000..026f45937170f67667accc7fabfa6dd6d5fdf147 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothAdvertising.ets @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ble } from '@kit.ConnectivityKit'; +import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; + +const TAG: string = 'BleAdvertisingManager'; + +export class BleAdvertisingManager { + private advHandle: number = 0xFF; // default invalid value + + // 1 订阅广播状态 + public onAdvertisingStateChange() { + try { + ble.on('advertisingStateChange', (data: ble.AdvertisingStateChangeInfo) => { + console.info(TAG, 'bluetooth advertising state = ' + JSON.stringify(data)); + AppStorage.setOrCreate('advertiserState', data.state); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 2 首次启动广播 + public async startAdvertising() { + // 2.1 设置广播发送的参数 + console.info('startAdvertising 2.1') + let setting: ble.AdvertiseSetting = { + interval: 160, + txPower: 0, + connectable: true + }; + // 2.2 构造广播数据 + console.info('startAdvertising 2.2') + let manufactureValueBuffer = new Uint8Array(4); + manufactureValueBuffer[0] = 1; + manufactureValueBuffer[1] = 2; + manufactureValueBuffer[2] = 3; + manufactureValueBuffer[3] = 4; + let serviceValueBuffer = new Uint8Array(4); + serviceValueBuffer[0] = 5; + serviceValueBuffer[1] = 6; + serviceValueBuffer[2] = 7; + serviceValueBuffer[3] = 8; + let manufactureDataUnit: ble.ManufactureData = { + manufactureId: 4567, + manufactureValue: manufactureValueBuffer.buffer + }; + let serviceDataUnit: ble.ServiceData = { + serviceUuid: '00001888-0000-1000-8000-00805f9b34fb', + serviceValue: serviceValueBuffer.buffer + }; + let advData: ble.AdvertiseData = { + serviceUuids: ['00001888-0000-1000-8000-00805f9b34fb'], + manufactureData: [manufactureDataUnit], + serviceData: [serviceDataUnit], + includeDeviceName: false // 表示是否携带设备名,可选参数。注意带上设备名时广播包长度不能超出31个字节。 + }; + let advResponse: ble.AdvertiseData = { + serviceUuids: ['00001888-0000-1000-8000-00805f9b34fb'], + manufactureData: [manufactureDataUnit], + serviceData: [serviceDataUnit] + }; + // 2.3 构造广播启动完整参数AdvertisingParams + console.info('startAdvertising 2.3') + let advertisingParams: ble.AdvertisingParams = { + advertisingSettings: setting, + advertisingData: advData, + advertisingResponse: advResponse, + duration: 0 // 可选参数,若大于0,则广播发送一段时间后,则会临时停止,可重新启动发送 + } + + // 2.4 首次启动广播,且获取所启动广播的标识ID + console.info('startAdvertising 2.4') + try { + this.onAdvertisingStateChange(); + this.advHandle = await ble.startAdvertising(advertisingParams); + console.info('startAdvertising succeed'); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 4 临时停止广播,该广播资源仍然存在 + public async disableAdvertising() { + // 4.1 构造临时停止广播参数 + console.info('disableAdvertising 4.1') + let advertisingDisableParams: ble.AdvertisingDisableParams = { + advertisingId: this.advHandle // 使用首次启动广播时获取到的广播标识ID + } + // 4.2 临时停止 + console.info('disableAdvertising 4.2') + try { + await ble.disableAdvertising(advertisingDisableParams); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 5 再次启动广播 + public async enableAdvertising(enableDuration: number) { + // 5.1 构造临时启动广播参数 + console.info('enableAdvertising 5.1') + let advertisingEnableParams: ble.AdvertisingEnableParams = { + advertisingId: this.advHandle, // 使用首次启动广播时获取到的广播标识ID + duration: enableDuration + } + // 5.2 再次启动 + console.info('enableAdvertising 5.2') + try { + await ble.enableAdvertising(advertisingEnableParams); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 6 完全关闭广播,释放广播资源 + public async stopAdvertising() { + try { + await ble.stopAdvertising(this.advHandle); + ble.off('advertisingStateChange', (data: ble.AdvertisingStateChangeInfo) => { + console.info(TAG, 'bluetooth advertising state = ' + JSON.stringify(data)); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } +} + +let bleAdvertisingManager = new BleAdvertisingManager(); +export default bleAdvertisingManager as BleAdvertisingManager; \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothScan.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothScan.ets new file mode 100644 index 0000000000000000000000000000000000000000..e4c93d86b1a03f0366e3f6cc117e5fe888d5821f --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/BluetoothScan.ets @@ -0,0 +1,232 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ble } from '@kit.ConnectivityKit'; +import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; + +const TAG: string = 'BleScanManager'; +const BLE_ADV_TYPE_FLAG = 0x01; +const BLE_ADV_TYPE_16_BIT_SERVICE_UUIDS_INCOMPLETE = 0x02; +const BLE_ADV_TYPE_16_BIT_SERVICE_UUIDS_COMPLETE = 0x03; +const BLE_ADV_TYPE_32_BIT_SERVICE_UUIDS_INCOMPLETE = 0x04; +const BLE_ADV_TYPE_32_BIT_SERVICE_UUIDS_COMPLETE = 0x05; +const BLE_ADV_TYPE_128_BIT_SERVICE_UUIDS_INCOMPLETE = 0x06; +const BLE_ADV_TYPE_128_BIT_SERVICE_UUIDS_COMPLETE = 0x07; +const BLE_ADV_TYPE_LOCAL_NAME_SHORT = 0x08; +const BLE_ADV_TYPE_LOCAL_NAME_COMPLETE = 0x09; +const BLE_ADV_TYPE_TX_POWER_LEVEL = 0x0A; +const BLE_ADV_TYPE_16_BIT_SERVICE_SOLICITATION_UUIDS = 0x14; +const BLE_ADV_TYPE_128_BIT_SERVICE_SOLICITATION_UUIDS = 0x15; +const BLE_ADV_TYPE_32_BIT_SERVICE_SOLICITATION_UUIDS = 0x1F; +const BLE_ADV_TYPE_16_BIT_SERVICE_DATA = 0x16; +const BLE_ADV_TYPE_32_BIT_SERVICE_DATA = 0x20; +const BLE_ADV_TYPE_128_BIT_SERVICE_DATA = 0x21; +const BLE_ADV_TYPE_MANUFACTURER_SPECIFIC_DATA = 0xFF; + +const BLUETOOTH_UUID_16_BIT_LENGTH = 2; +const BLUETOOTH_UUID_32_BIT_LENGTH = 4; +const BLUETOOTH_UUID_128_BIT_LENGTH = 16; + +const BLUETOOTH_MANUFACTURE_ID_LENGTH = 2; + +export class BleScanManager { + // 1 订阅扫描结果 + public onScanResult() { + ble.on('BLEDeviceFind', (data: Array) => { + if (data.length > 0) { + console.info(TAG, 'BLE scan result = ' + data[0].deviceId); + this.parseScanResult(data[0].data); + } + }); + } + + private parseScanResult(data: ArrayBuffer) { + let advData = new Uint8Array(data); + if (advData.byteLength == 0) { + console.warn(TAG, 'nothing, adv data length is 0'); + return; + } + console.info(TAG, 'advData: ' + JSON.stringify(advData)); + + let advFlags: number = -1; + let txPowerLevel: number = -1; + let localName: string = ''; + let serviceUuids: string[] = []; + let serviceSolicitationUuids: string[] = []; + let serviceDatas: Record = {}; + let manufactureSpecificDatas: Record = {}; + + let curPos = 0; + while (curPos < advData.byteLength) { + let length = advData[curPos++]; + if (length == 0) { + break; + } + let advDataLength = length - 1; + let advDataType = advData[curPos++]; + switch (advDataType) { + case BLE_ADV_TYPE_FLAG: + advFlags = advData[curPos]; + break; + case BLE_ADV_TYPE_LOCAL_NAME_SHORT: + case BLE_ADV_TYPE_LOCAL_NAME_COMPLETE: + localName = advData.slice(curPos, curPos + advDataLength).toString(); + break; + case BLE_ADV_TYPE_TX_POWER_LEVEL: + txPowerLevel = advData[curPos]; + break; + case BLE_ADV_TYPE_16_BIT_SERVICE_UUIDS_INCOMPLETE: + case BLE_ADV_TYPE_16_BIT_SERVICE_UUIDS_COMPLETE: + this.parseServiceUuid(BLUETOOTH_UUID_16_BIT_LENGTH, curPos, advDataLength, advData, serviceUuids); + break; + case BLE_ADV_TYPE_32_BIT_SERVICE_UUIDS_INCOMPLETE: + case BLE_ADV_TYPE_32_BIT_SERVICE_UUIDS_COMPLETE: + this.parseServiceUuid(BLUETOOTH_UUID_32_BIT_LENGTH, curPos, advDataLength, advData, serviceUuids); + break; + case BLE_ADV_TYPE_128_BIT_SERVICE_UUIDS_INCOMPLETE: + case BLE_ADV_TYPE_128_BIT_SERVICE_UUIDS_COMPLETE: + this.parseServiceUuid(BLUETOOTH_UUID_128_BIT_LENGTH, curPos, advDataLength, advData, serviceUuids); + break; + case BLE_ADV_TYPE_16_BIT_SERVICE_SOLICITATION_UUIDS: + this.parseServiceSolicitationUuid(BLUETOOTH_UUID_16_BIT_LENGTH, curPos, advDataLength, + advData, serviceSolicitationUuids); + break; + case BLE_ADV_TYPE_32_BIT_SERVICE_SOLICITATION_UUIDS: + this.parseServiceSolicitationUuid(BLUETOOTH_UUID_32_BIT_LENGTH, curPos, advDataLength, + advData, serviceSolicitationUuids); + break; + case BLE_ADV_TYPE_128_BIT_SERVICE_SOLICITATION_UUIDS: + this.parseServiceSolicitationUuid(BLUETOOTH_UUID_128_BIT_LENGTH, curPos, advDataLength, + advData, serviceSolicitationUuids); + break; + case BLE_ADV_TYPE_16_BIT_SERVICE_DATA: + this.parseServiceData(BLUETOOTH_UUID_16_BIT_LENGTH, curPos, advDataLength, advData, serviceDatas); + break; + case BLE_ADV_TYPE_32_BIT_SERVICE_DATA: + this.parseServiceData(BLUETOOTH_UUID_32_BIT_LENGTH, curPos, advDataLength, advData, serviceDatas); + break; + case BLE_ADV_TYPE_128_BIT_SERVICE_DATA: + this.parseServiceData(BLUETOOTH_UUID_128_BIT_LENGTH, curPos, advDataLength, advData, serviceDatas); + break; + case BLE_ADV_TYPE_MANUFACTURER_SPECIFIC_DATA: + this.parseManufactureData(curPos, advDataLength, advData, manufactureSpecificDatas); + break; + default: + break; + } + curPos += advDataLength; + } + } + + private parseServiceUuid(uuidLength: number, curPos: number, advDataLength: number, + advData: Uint8Array, serviceUuids: string[]) { + while (advDataLength > 0) { + let tmpData: Uint8Array = advData.slice(curPos, curPos + uuidLength); + serviceUuids.push(this.getUuidFromUint8Array(uuidLength, tmpData)); + advDataLength -= uuidLength; + curPos += uuidLength; + } + } + + private parseServiceSolicitationUuid(uuidLength: number, curPos: number, advDataLength: number, + advData: Uint8Array, serviceSolicitationUuids: string[]) { + while (advDataLength > 0) { + let tmpData: Uint8Array = advData.slice(curPos, curPos + uuidLength); + serviceSolicitationUuids.push(this.getUuidFromUint8Array(uuidLength, tmpData)); + advDataLength -= uuidLength; + curPos += uuidLength; + } + } + + private getUuidFromUint8Array(uuidLength: number, uuidData: Uint8Array): string { + let uuid = ''; + let temp: string = ''; + for (let i = uuidLength - 1; i > -1; i--) { + temp += uuidData[i].toString(16).padStart(2, '0'); + } + switch (uuidLength) { + case BLUETOOTH_UUID_16_BIT_LENGTH: + uuid = `0000${temp}-0000-1000-8000-00805F9B34FB`; + break; + case BLUETOOTH_UUID_32_BIT_LENGTH: + uuid = `${temp}-0000-1000-8000-00805F9B34FB`; + break; + case BLUETOOTH_UUID_128_BIT_LENGTH: + uuid = `${temp.substring(0, 8)}-${temp.substring(8, 12)}-${temp.substring(12, 16)}-${temp.substring(16, 20)}-${temp.substring(20, 32)}`; + break; + default: + break; + } + return uuid; + } + + private parseServiceData(uuidLength: number, curPos: number, advDataLength: number, + advData: Uint8Array, serviceDatas: Record) { + let tmpUuid: Uint8Array = advData.slice(curPos, curPos + uuidLength); + let tmpValue: Uint8Array = advData.slice(curPos + uuidLength, curPos + advDataLength); + serviceDatas[tmpUuid.toString()] = tmpValue; + } + + private parseManufactureData(curPos: number, advDataLength: number, + advData: Uint8Array, manufactureSpecificDatas: Record) { + let manufactureId: number = (advData[curPos + 1] << 8) + advData[curPos]; + let tmpValue: Uint8Array = advData.slice(curPos + BLUETOOTH_MANUFACTURE_ID_LENGTH, curPos + advDataLength); + manufactureSpecificDatas[manufactureId] = tmpValue; + } + + // 2 开启扫描 + public startScan() { + console.info(TAG, 'startBleScan success 1'); + // 2.1 构造扫描过滤器,需要能够匹配预期的广播包内容 + let manufactureId = 4567; + // let manufactureData: Uint8Array = new Uint8Array([0xFF,0xFF,0xFF]); + let manufactureDataMask: Uint8Array = new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF]); + let scanFilter: ble.ScanFilter = { // 根据业务实际情况定义过滤器 + manufactureId: manufactureId, + // manufactureData: manufactureData.buffer, + manufactureDataMask: manufactureDataMask.buffer + }; + + // 2.2 构造扫描参数 + let scanOptions: ble.ScanOptions = { + interval: 0, + dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER, + matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE + } + try { + this.onScanResult(); // 订阅扫描结果 + ble.startBLEScan([scanFilter], scanOptions); + console.info(TAG, 'startBleScan success'); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 3 关闭扫描 + public stopScan() { + try { + ble.off('BLEDeviceFind', (data: Array) => { // 取消订阅扫描结果 + console.info(TAG, 'off success'); + }); + ble.stopBLEScan(); + console.info(TAG, 'stopBleScan success'); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } +} + +let bleScanManager = new BleScanManager(); +export default bleScanManager as BleScanManager; diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/EntryAbility.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(0x0000, 'testTag', 'onBackup ok'); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(0x0000, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..638e26511ff344620a033ac682071afcac750bc0 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BleScanManager } from '../entryability/BluetoothScan' +import { BleAdvertisingManager } from '../entryability/BluetoothAdvertising' + +@Entry +@Component +struct Broadcast { + private buttonWidth: string | number = '50%' + private bleScanManager: BleScanManager = new BleScanManager(); + private bleAdvertisingManager: BleAdvertisingManager = new BleAdvertisingManager(); + + build() { + Column({ space: 12 }) { + + Button('open broadcast') + .id('openBroadcast') + .onClick(() => { + this.bleAdvertisingManager.startAdvertising(); + }) + + Button('stop broadcast') + .width(this.buttonWidth) + .onClick(() => { + console.info('disable Advertising'); + this.bleAdvertisingManager.disableAdvertising(); + console.info('Advertising over'); + }) + + Button('continue broadcast') + .width(this.buttonWidth) + .onClick(() => { + console.info('disable enableAdvertising'); + this.bleAdvertisingManager.enableAdvertising(0); + console.info('enableAdvertising over'); + }) + + Button('close broadcast') + .width(this.buttonWidth) + .onClick(() => { + console.info('disable stopAdvertising'); + this.bleAdvertisingManager.stopAdvertising(); + console.info('stopAdvertising over'); + }) + + Button('start scan') + .width(this.buttonWidth) + .onClick(() => { + this.bleScanManager.startScan(); + }) + + Button('stop scan') + .width(this.buttonWidth) + .onClick(() => { + this.bleScanManager.stopScan(); + }) + }.width('100%') + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/module.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4d001e6eeef8773e6ce3eaea955215a716a5e75 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/module.json5 @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.ACCESS_BLUETOOTH", + "reason": "$string:ACCESS_BLUETOOTH", + "usedScene": { + "when": "always" + } + }], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/color.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/string.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..fa5d8db024d50fa81182f5cdbea2de0b2fe6bc8e --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "BroadcastAndScan" + }, + { + "name": "ACCESS_BLUETOOTH", + "value": "user" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/background.png b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f939c9fa8cc8914832e602198745f592a0dfa34d Binary files /dev/null and b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/background.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/foreground.png b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..4483ddad1f079e1089d685bd204ee1cfe1d01902 Binary files /dev/null and b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/layered_image.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/startIcon.png b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..205ad8b5a8a42e8762fbe4899b8e5e31ce822b8b Binary files /dev/null and b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/backup_config.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/main_pages.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/dark/element/color.json b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/mock/mock-config.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..b9a78e201535765168a92d3543c690273ecdc019 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/mock/mock-config.json5 @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/Ability.test.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..0f8ce9a2c012f8fe36114cef65216ef0b6254f41 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function abilityTest() { + describe('ActsAbilityTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }) + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }) + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }) + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }) + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }) + }) +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/List.test.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1eac52fcebe8958e19a7b8fed2e8f39c520a3e42 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/module.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c3fd9dda3040d888d9d8b0b62bcb5d3b6fbeb614 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/ohosTest/module.json5 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "default", + "tablet" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/List.test.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f1186b1f53c3a70930921c5dbd1417332bec56c9 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/List.test.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/LocalUnit.test.ets b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fc57c77dbf76d8df08a2b802a55b948e3fcf968 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigor/hvigor-config.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d584c19c247db9a7caee4b606bb931aa9279c637 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigor/hvigor-config.json5 @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigorfile.ts b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins:[] /* Custom plugin to extend the functionality of Hvigor. */ +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/oh-package.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/BroadcastAndScan/oh-package.json5 @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{ + "modelVersion": "5.0.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.19", + "@ohos/hamock": "1.0.0" + } +}