diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/.gitignore b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/.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/GATT/AppScope/app.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c2e288cc0500056e81021d5278a45837aacf81ae --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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.gatt", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name" + } +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/element/string.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4e5dd36a690a55fccb45084717163dfb57a0b54b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "GATT" + } + ] +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/media/app_icon.png b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a39445dc87828b76fed6d2ec470dd455c45319e3 Binary files /dev/null and b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/AppScope/resources/base/media/app_icon.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/README.md b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/README.md new file mode 100644 index 0000000000000000000000000000000000000000..46df639d6ee41c63e1527d225534dc9bbc124739 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/README.md @@ -0,0 +1,124 @@ +# 通用属性协议开发指导示例 + +### 介绍 + +通用属性协议是GATT(Generic Attribute)的缩写,它是一种用于在蓝牙低功耗设备之间传输数据的协议,定义了一套通用的属性和服务框架。通过GATT协议,蓝牙设备可以向其他设备提供服务,也可以从其他设备获取服务。 + +### 场景介绍 + +主要场景有: + +1. 连接server端读取和写入信息 + +2. server端操作services和通知客户端信息。 + +### 接口说明 + +完整的 JS API 说明以及实例代码请参考:[GATT接口](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-bluetooth-ble.md) + +具体接口说明如下表。 + +| 接口名 | 功能描述 | +|----------------|------------| +|connect()| client端发起连接远端蓝牙低功耗设备。| +|disconnect() |client端断开与远端蓝牙低功耗设备的连接。| +|close()| 关闭客户端功能,注销client在协议栈的注册,调用该接口后GattClientDevice实例将不能再使用。| +|getDeviceName()| client获取远端蓝牙低功耗设备名。| +|getServices() |client端获取蓝牙低功耗设备的所有服务,即服务发现 。| +|readCharacteristicValue()| client端读取蓝牙低功耗设备特定服务的特征值。| +|readDescriptorValue() |client端读取蓝牙低功耗设备特定的特征包含的描述符。| +|writeCharacteristicValue() |client端向低功耗蓝牙设备写入特定的特征值。| +|writeDescriptorValue()| client端向低功耗蓝牙设备特定的描述符写入二进制数据。| +|getRssiValue() |client获取远端蓝牙低功耗设备的信号强度 (Received Signal Strength Indication, RSSI),调用connect接口连接成功后才能使用。| +|setBLEMtuSize()| client协商远端蓝牙低功耗设备的最大传输单元(Maximum Transmission Unit, MTU),调用connect接口连接成功后才能使用。| +|setCharacteristicChangeNotification()| 向服务端发送设置通知此特征值请求。| +|setCharacteristicChangeIndication()| 向服务端发送设置通知此特征值请求。| +|on(type: 'BLECharacteristicChange')| 订阅蓝牙低功耗设备的特征值变化事件。需要先调用setNotifyCharacteristicChanged接口才能接收server端的通知。| +|off(type: 'BLECharacteristicChange')| 取消订阅蓝牙低功耗设备的特征值变化事件。| +|on(type: 'BLEConnectionStateChange') |client端订阅蓝牙低功耗设备的连接状态变化事件。| +|off(type: 'BLEConnectionStateChange') |取消订阅蓝牙低功耗设备的连接状态变化事件。| +|on(type: 'BLEMtuChange') |client端订阅MTU状态变化事件。| +|off(type: 'BLEMtuChange') |client端取消订阅MTU状态变化事件。| +|addService() |server端添加服务。| +|removeService() |删除已添加的服务。| +|close() |关闭服务端功能,去注销server在协议栈的注册,调用该接口后GattServer实例将不能再使用。| +|notifyCharacteristicChanged()| server端特征值发生变化时,主动通知已连接的client设备。| +|sendResponse() |server端回复client端的读写请求。| +|on(type: 'characteristicRead') |server端订阅特征值读请求事件。| +|off(type: 'characteristicRead')| server端取消订阅特征值读请求事件。| +|on(type: 'characteristicWrite')| server端订阅特征值写请求事件。| +|off(type: 'characteristicWrite')| server端取消订阅特征值写请求事件。| +|on(type: 'descriptorRead') |server端订阅描述符读请求事件。| +|off(type: 'descriptorRead') |server端取消订阅描述符读请求事件。| +|on(type: 'descriptorWrite') |server端订阅描述符写请求事件。| +|off(type: 'descriptorWrite') |server端取消订阅描述符写请求事件。| +|on(type: 'connectionStateChange')| server端订阅BLE连接状态变化事件。| +|off(type: 'connectionStateChange') |server端取消订阅BLE连接状态变化事件。| +|on(type: 'BLEMtuChange') |server端订阅MTU状态变化事件。| +|off(type: 'BLEMtuChange')| server端取消订阅MTU状态变化事件。| + + +### 主要场景开发步骤 + +#### 连接server端读取和写入信息 + +1. import需要的ble模块。 +2. 开启设备的蓝牙。 +3. 创建gattClient实例对象。 +4. 连接gattServer。 +5. 读取gattServer的特征值和描述符。 +6. 向gattServer写入特征值和描述符。 +7. 断开连接,销毁gattClient实例。 +8. 错误码请参见[蓝牙服务子系统错误码](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-connectivity-kit/errorcode-bluetoothManager.md). + +#### server端操作services和通知客户端信息 + +1. import需要的ble模块。 +2. 创建gattServer实例对象。 +3. 添加services信息。 +4. 移除services信息。 +5. 注销gattServer实例。 +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 +| |---gattClientManager.ets +| |---gattServerManager.ets +| |---EntryAbility.ets +|---entrybackupability +| |---EntryBackupAbility.ets +|---pages +| |---Index.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/GATT/ > .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/GATT/build-profile.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..9a067264a40737cf79d1d8ae7c574b8d2cc87ce0 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/build-profile.json5 @@ -0,0 +1,58 @@ +/* + * 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": [], + "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/GATT/code-linter.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..28586467ee7a761c737d8654a73aed6fddbc3c71 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/.gitignore b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/build-profile.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e7569e3056e27af38e9991b7ea73ec10f3ba8a05 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/hvigorfile.ts b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..c6edcd90486dd5a853cf7d34c8647f08414ca7a3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/obfuscation-rules.txt b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/oh-package.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..10cda399b0aec3099b257299a57d284393e4e55a --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/oh-package.json5 @@ -0,0 +1,24 @@ +/* + * 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/GATT/entry/src/main/ets/entryability/EntryAbility.ets b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..edc2839f203ba057c186e19b0cbbbf80c8faa8b3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/ets/entryability/gattClientManager.ets b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/gattClientManager.ets new file mode 100644 index 0000000000000000000000000000000000000000..6903256c73885b6228bb339ced0decd2138397e2 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/gattClientManager.ets @@ -0,0 +1,299 @@ +/* + * 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 { constant } from '@kit.ConnectivityKit'; +import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; + +const TAG: string = 'GattClientManager'; + +export class GattClientManager { + device: string | undefined= undefined; + gattClient: ble.GattClientDevice | undefined = undefined; + connectState: ble.ProfileConnectionState = constant.ProfileConnectionState.STATE_DISCONNECTED; + myServiceUuid: string = '00001810-0000-1000-8000-00805F9B34FB'; + myCharacteristicUuid: string = '00001820-0000-1000-8000-00805F9B34FB'; + myFirstDescriptorUuid: string = '00002902-0000-1000-8000-00805F9B34FB'; // 2902一般用于notification或者indication + mySecondDescriptorUuid: string = '00002903-0000-1000-8000-00805F9B34FB'; + found: boolean = false; + + // 构造BLEDescriptor + private initDescriptor(des: string, value: ArrayBuffer): ble.BLEDescriptor { + let descriptor: ble.BLEDescriptor = { + serviceUuid: this.myServiceUuid, + characteristicUuid: this.myCharacteristicUuid, + descriptorUuid: des, + descriptorValue: value + }; + return descriptor; + } + + // 构造BLECharacteristic + private initCharacteristic(): ble.BLECharacteristic { + let descriptors: Array = []; + let descBuffer = new ArrayBuffer(2); + let descValue = new Uint8Array(descBuffer); + descValue[0] = 11; + descValue[1] = 12; + descriptors[0] = this.initDescriptor(this.myFirstDescriptorUuid, new ArrayBuffer(2)); + descriptors[1] = this.initDescriptor(this.mySecondDescriptorUuid, descBuffer); + let charBuffer = new ArrayBuffer(2); + let charValue = new Uint8Array(charBuffer); + charValue[0] = 1; + charValue[1] = 2; + let characteristic: ble.BLECharacteristic = { + serviceUuid: this.myServiceUuid, + characteristicUuid: this.myCharacteristicUuid, + characteristicValue: charBuffer, + descriptors: descriptors + }; + return characteristic; + } + + private logCharacteristic(char: ble.BLECharacteristic) { + let message = 'logCharacteristic uuid:' + char.characteristicUuid + '\n'; + let value = new Uint8Array(char.characteristicValue); + message += 'logCharacteristic value: '; + for (let i = 0; i < char.characteristicValue.byteLength; i++) { + message += value[i] + ' '; + } + console.info(TAG, message); + } + + private logDescriptor(des: ble.BLEDescriptor) { + let message = 'logDescriptor uuid:' + des.descriptorUuid + '\n'; + let value = new Uint8Array(des.descriptorValue); + message += 'logDescriptor value: '; + for (let i = 0; i < des.descriptorValue.byteLength; i++) { + message += value[i] + ' '; + } + console.info(TAG, message); + } + + private checkService(services: Array): boolean { + for (let i = 0; i < services.length; i++) { + if (services[i].serviceUuid != this.myServiceUuid) { + continue; + } + for (let j = 0; j < services[i].characteristics.length; j++) { + if (services[i].characteristics[j].characteristicUuid != this.myCharacteristicUuid) { + continue; + } + for (let k = 0; k < services[i].characteristics[j].descriptors.length; k++) { + if (services[i].characteristics[j].descriptors[k].descriptorUuid == this.myFirstDescriptorUuid) { + console.info(TAG, 'find expected service from server'); + return true; + } + } + } + } + console.error(TAG, 'no expected service from server'); + return false; + } + + // 1. 订阅连接状态变化事件 + public onGattClientStateChange() { + if (!this.gattClient) { + console.error(TAG, 'no gattClient'); + return; + } + try { + this.gattClient.on('BLEConnectionStateChange', (stateInfo: ble.BLEConnectionChangeState) => { + let state = ''; + switch (stateInfo.state) { + case 0: + state = 'DISCONNECTED'; + break; + case 1: + state = 'CONNECTING'; + break; + case 2: + state = 'CONNECTED'; + break; + case 3: + state = 'DISCONNECTING'; + break; + default: + state = 'undefined'; + break; + } + console.info(TAG, 'onGattClientStateChange: device=' + stateInfo.deviceId + ', state=' + state); + if (stateInfo.deviceId == this.device) { + this.connectState = stateInfo.state; + } + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 2. client端主动连接时调用 + public startConnect(peerDevice: string) { // 对端设备一般通过ble scan获取到 + if (this.connectState != constant.ProfileConnectionState.STATE_DISCONNECTED) { + console.error(TAG, 'startConnect failed'); + return; + } + console.info(TAG, 'startConnect ' + peerDevice); + this.device = peerDevice; + // 2.1 使用device构造gattClient,后续的交互都需要使用该实例 + this.gattClient = ble.createGattClientDevice(peerDevice); + try { + this.onGattClientStateChange(); // 2.2 订阅连接状态 + this.gattClient.connect(); // 2.3 发起连接 + console.info(TAG, 'startConnect success'); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 3. client端连接成功后,需要进行服务发现 + public discoverServices() { + if (!this.gattClient) { + console.info(TAG, 'no gattClient'); + return; + } + console.info(TAG, 'discoverServices'); + try { + this.gattClient.getServices().then((result: Array) => { + console.info(TAG, 'getServices success: ' + JSON.stringify(result)); + this.found = this.checkService(result); // 要确保server端的服务内容有业务所需要的服务 + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 4. 在确保拿到了server端的服务结果后,读取server端特定服务的特征值时调用 + public readCharacteristicValue() { + if (!this.gattClient || this.connectState != constant.ProfileConnectionState.STATE_CONNECTED) { + console.error(TAG, 'no gattClient or not connected'); + return; + } + if (!this.found) { // 要确保server端有对应的characteristic + console.error(TAG, 'no characteristic from server'); + return; + } + + let characteristic = this.initCharacteristic(); + console.info(TAG, 'readCharacteristicValue'); + try { + this.gattClient.readCharacteristicValue(characteristic).then((outData: ble.BLECharacteristic) => { + this.logCharacteristic(outData); + }) + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 5. 在确保拿到了server端的服务结果后,写入server端特定服务的特征值时调用 + public writeCharacteristicValue() { + if (!this.gattClient || this.connectState != constant.ProfileConnectionState.STATE_CONNECTED) { + console.error(TAG, 'no gattClient or not connected'); + return; + } + if (!this.found) { // 要确保server端有对应的characteristic + console.error(TAG, 'no characteristic from server'); + return; + } + + let characteristic = this.initCharacteristic(); + console.info(TAG, 'writeCharacteristicValue'); + try { + this.gattClient.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE, (err) => { + if (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + return; + } + console.info(TAG, 'writeCharacteristicValue success'); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 6. 在确保拿到了server端的服务结果后,读取server端特定服务的描述符时调用 + public readDescriptorValue() { + if (!this.gattClient || this.connectState != constant.ProfileConnectionState.STATE_CONNECTED) { + console.error(TAG, 'no gattClient or not connected'); + return; + } + if (!this.found) { // 要确保server端有对应的descriptor + console.error(TAG, 'no descriptor from server'); + return; + } + + let descBuffer = new ArrayBuffer(0); + let descriptor = this.initDescriptor(this.mySecondDescriptorUuid, descBuffer); + console.info(TAG, 'readDescriptorValue'); + try { + this.gattClient.readDescriptorValue(descriptor).then((outData: ble.BLEDescriptor) => { + this.logDescriptor(outData); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 7. 在确保拿到了server端的服务结果后,写入server端特定服务的描述符时调用 + public writeDescriptorValue() { + if (!this.gattClient || this.connectState != constant.ProfileConnectionState.STATE_CONNECTED) { + console.error(TAG, 'no gattClient or not connected'); + return; + } + if (!this.found) { // 要确保server端有对应的descriptor + console.error(TAG, 'no descriptor from server'); + return; + } + + let descBuffer = new ArrayBuffer(2); + let descValue = new Uint8Array(descBuffer); + descValue[0] = 11; + descValue[1] = 12; + let descriptor = this.initDescriptor(this.mySecondDescriptorUuid, descBuffer); + console.info(TAG, 'writeDescriptorValue'); + try { + this.gattClient.writeDescriptorValue(descriptor, (err) => { + if (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + return; + } + console.info(TAG, 'writeDescriptorValue success'); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 8.client端主动断开时调用 + public stopConnect() { + if (!this.gattClient || this.connectState != constant.ProfileConnectionState.STATE_CONNECTED) { + console.error(TAG, 'no gattClient or not connected'); + return; + } + + console.info(TAG, 'stopConnect ' + this.device); + try { + this.gattClient.disconnect(); // 8.1 断开连接 + this.gattClient.off('BLEConnectionStateChange', (stateInfo: ble.BLEConnectionChangeState) => { + }); + this.gattClient.close() // 8.2 如果不再使用此gattClient,则需要close + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } +} + +let gattClientManager = new GattClientManager(); +export default gattClientManager as GattClientManager; \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/gattServerManager.ets b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/gattServerManager.ets new file mode 100644 index 0000000000000000000000000000000000000000..d4a0e131f1cb45cb708c0bb087afebf8a9b40412 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entryability/gattServerManager.ets @@ -0,0 +1,290 @@ +/* + * 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 { constant } from '@kit.ConnectivityKit'; +import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; + +const TAG: string = 'GattServerManager'; + +export class GattServerManager { + gattServer: ble.GattServer = ble.createGattServer(); + connectState: ble.ProfileConnectionState = constant.ProfileConnectionState.STATE_DISCONNECTED; + myServiceUuid: string = '00001810-0000-1000-8000-00805F9B34FB'; + myCharacteristicUuid: string = '00001820-0000-1000-8000-00805F9B34FB'; + myFirstDescriptorUuid: string = '00002902-0000-1000-8000-00805F9B34FB'; // 2902一般用于notification或者indication + mySecondDescriptorUuid: string = '00002903-0000-1000-8000-00805F9B34FB'; + + // 构造BLEDescriptor + private initDescriptor(des: string, value: ArrayBuffer): ble.BLEDescriptor { + let descriptor: ble.BLEDescriptor = { + serviceUuid: this.myServiceUuid, + characteristicUuid: this.myCharacteristicUuid, + descriptorUuid: des, + descriptorValue: value + }; + return descriptor; + } + + // 构造BLECharacteristic + private initCharacteristic(): ble.BLECharacteristic { + let descriptors: Array = []; + let descBuffer = new ArrayBuffer(2); + let descValue = new Uint8Array(descBuffer); + descValue[0] = 31; + descValue[1] = 32; + descriptors[0] = this.initDescriptor(this.myFirstDescriptorUuid, new ArrayBuffer(2)); + descriptors[1] = this.initDescriptor(this.mySecondDescriptorUuid, descBuffer); + let charBuffer = new ArrayBuffer(2); + let charValue = new Uint8Array(charBuffer); + charValue[0] = 21; + charValue[1] = 22; + let characteristic: ble.BLECharacteristic = { + serviceUuid: this.myServiceUuid, + characteristicUuid: this.myCharacteristicUuid, + characteristicValue: charBuffer, + descriptors: descriptors + }; + return characteristic; + } + + // 1. 订阅连接状态变化事件 + public onGattServerStateChange() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + try { + this.gattServer.on('connectionStateChange', (stateInfo: ble.BLEConnectionChangeState) => { + let state = ''; + switch (stateInfo.state) { + case 0: + state = 'DISCONNECTED'; + break; + case 1: + state = 'CONNECTING'; + break; + case 2: + state = 'CONNECTED'; + break; + case 3: + state = 'DISCONNECTING'; + break; + default: + state = 'undefined'; + break; + } + console.info(TAG, 'onGattServerStateChange: device=' + stateInfo.deviceId + ', state=' + state); + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 2. server端注册服务时调用 + public registerServer() { + let characteristics: Array = []; + let characteristic = this.initCharacteristic(); + characteristics.push(characteristic); + let gattService: ble.GattService = { + serviceUuid: this.myServiceUuid, + isPrimary: true, + characteristics: characteristics + }; + + console.info(TAG, 'registerServer ' + this.myServiceUuid); + try { + // this.gattServer = ble.createGattServer(); 初始化时已经构造,去除报错// 2.1 构造gattServer,后续的交互都需要使用该实例 + this.onGattServerStateChange(); // 2.2 订阅连接状态 + this.gattServer.addService(gattService); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 3. 订阅来自gattClient的读取特征值请求时调用 + public onCharacteristicRead() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + + console.info(TAG, 'onCharacteristicRead'); + try { + this.gattServer.on('characteristicRead', (charReq: ble.CharacteristicReadRequest) => { + let deviceId: string = charReq.deviceId; + let transId: number = charReq.transId; + let offset: number = charReq.offset; + console.info(TAG, 'receive characteristicRead'); + let rspBuffer = new ArrayBuffer(2); + let rspValue = new Uint8Array(rspBuffer); + rspValue[0] = 21; + rspValue[1] = 22; + let serverResponse: ble.ServerResponse = { + deviceId: deviceId, + transId: transId, + status: 0, // 0表示成功 + offset: offset, + value: rspBuffer + }; + + try { + this.gattServer.sendResponse(serverResponse); + } catch (err) { + console.error(TAG, + 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 4. 订阅来自gattClient的写入特征值请求时调用 + public onCharacteristicWrite() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + + console.info(TAG, 'onCharacteristicWrite'); + try { + this.gattServer.on('characteristicWrite', (charReq: ble.CharacteristicWriteRequest) => { + let deviceId: string = charReq.deviceId; + let transId: number = charReq.transId; + let offset: number = charReq.offset; + console.info(TAG, 'receive characteristicWrite: needRsp=' + charReq.needRsp); + if (!charReq.needRsp) { + return; + } + let rspBuffer = new ArrayBuffer(0); + let serverResponse: ble.ServerResponse = { + deviceId: deviceId, + transId: transId, + status: 0, // 0表示成功 + offset: offset, + value: rspBuffer + }; + + try { + this.gattServer.sendResponse(serverResponse); + } catch (err) { + console.error(TAG, + 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 5. 订阅来自gattClient的读取描述符请求时调用 + public onDescriptorRead() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + + console.info(TAG, 'onDescriptorRead'); + try { + this.gattServer.on('descriptorRead', (desReq: ble.DescriptorReadRequest) => { + let deviceId: string = desReq.deviceId; + let transId: number = desReq.transId; + let offset: number = desReq.offset; + console.info(TAG, 'receive descriptorRead'); + let rspBuffer = new ArrayBuffer(2); + let rspValue = new Uint8Array(rspBuffer); + rspValue[0] = 31; + rspValue[1] = 32; + let serverResponse: ble.ServerResponse = { + deviceId: deviceId, + transId: transId, + status: 0, // 0表示成功 + offset: offset, + value: rspBuffer + }; + + try { + this.gattServer.sendResponse(serverResponse); + } catch (err) { + console.error(TAG, + 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 6. 订阅来自gattClient的写入描述符请求时调用 + public onDescriptorWrite() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + + console.info(TAG, 'onDescriptorWrite'); + try { + this.gattServer.on('descriptorWrite', (desReq: ble.DescriptorWriteRequest) => { + let deviceId: string = desReq.deviceId; + let transId: number = desReq.transId; + let offset: number = desReq.offset; + console.info(TAG, 'receive descriptorWrite: needRsp=' + desReq.needRsp); + if (!desReq.needRsp) { + return; + } + let rspBuffer = new ArrayBuffer(0); + let serverResponse: ble.ServerResponse = { + deviceId: deviceId, + transId: transId, + status: 0, // 0表示成功 + offset: offset, + value: rspBuffer + }; + + try { + this.gattServer.sendResponse(serverResponse); + } catch (err) { + console.error(TAG, + 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + }); + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } + + // 7. server端删除服务,不再使用时调用 + public unRegisterServer() { + if (!this.gattServer) { + console.error(TAG, 'no gattServer'); + return; + } + + console.info(TAG, 'unRegisterServer ' + this.myServiceUuid); + try { + this.gattServer.removeService(this.myServiceUuid); // 7.1 删除服务 + this.gattServer.off('connectionStateChange', (stateInfo: ble.BLEConnectionChangeState) => { // 7.2 取消订阅连接状态 + }); + this.gattServer.close() // 7.3 如果不再使用此gattServer,则需要close + } catch (err) { + console.error(TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); + } + } +} + +let gattServerManager = new GattServerManager(); + +export default gattServerManager as GattServerManager; \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1e212947256c5533c7b06285a597c94f840a6e3 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/ets/pages/Index.ets b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..1318b0c5cfcb1fc4446730f9dbcf57b992261e15 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,84 @@ +/* + * 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 { GattClientManager } from '../entryability/gattClientManager' +import { GattServerManager } from '../entryability/gattServerManager' + +@Entry +@Component +struct GATT { + private gattClientManager: GattClientManager = new GattClientManager(); + private gattServerManager: GattServerManager = new GattServerManager(); + @State deviceId: string = '12:32:56:78:12:52'; + + build() { + Scroll() { + Column({ space: 12 }) { + Text('GATT') + .fontSize(50) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Top }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + + Button('start connect') + .onClick(() => { + this.gattClientManager.startConnect(this.deviceId); + }) + + Button('discover services') + .onClick(() => { + this.gattClientManager.discoverServices(); + }) + + Button('read characteristic value') + .onClick(() => { + this.gattClientManager.readCharacteristicValue(); + }) + + Button('write characteristic value') + .onClick(() => { + this.gattClientManager.writeCharacteristicValue(); + }) + + Button('read descriptor value') + .onClick(() => { + this.gattClientManager.readDescriptorValue(); + }) + + Button('write descriptor value') + .onClick(() => { + this.gattClientManager.writeDescriptorValue(); + }) + + Button('stop connect') + .onClick(() => { + this.gattClientManager.stopConnect(); + }) + + Button('registerServer') + .onClick(() => { + this.gattServerManager.registerServer(); + }) + + Button('unRegisterServer') + .onClick(() => { + this.gattServerManager.unRegisterServer(); + }) + }.width('100%') + } + } +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/module.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..a4d001e6eeef8773e6ce3eaea955215a716a5e75 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/element/color.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/element/string.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..21ad1adeac61571bf0968ecfa23cfa94aa10f58b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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": "GATT" + }, + { + "name": "ACCESS_BLUETOOTH", + "value": "user" + } + ] +} \ No newline at end of file diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/media/background.png b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/media/background.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/media/foreground.png b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/media/foreground.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/media/layered_image.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/media/startIcon.png b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/media/startIcon.png differ diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/profile/backup_config.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/entry/src/main/resources/base/profile/main_pages.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/dark/element/color.json b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/hvigor/hvigor-config.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..1e473e424320d4e68b16737b289f5c851bb19d36 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/hvigor/hvigor-config.json5 @@ -0,0 +1,22 @@ +{ + "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/GATT/hvigorfile.ts b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3cb9f1a87a81687554a76283af8df27d8bda775 --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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/GATT/oh-package.json5 b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e41bae026aab3b50d0abb42fece08ba43b4a772b --- /dev/null +++ b/code/DocsSample/ConnectivityKit/Bluetooth/GATT/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" + } +}