| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 06-May-2025 | - | 37 | 34 | ||
| entry/ | 06-May-2025 | - | 1,429 | 1,234 | ||
| hvigor/ | 06-May-2025 | - | 24 | 22 | ||
| screenshots/devices/ | 06-May-2025 | - | ||||
| .gitignore | D | 06-May-2025 | 100 | 7 | 7 | |
| README.md | D | 06-May-2025 | 1.7 KiB | 41 | 21 | |
| README_zh.md | D | 06-May-2025 | 5.5 KiB | 82 | 54 | |
| build-profile.json5 | D | 06-May-2025 | 1 KiB | 42 | 41 | |
| hvigorfile.js | D | 06-May-2025 | 168 | 2 | 1 | |
| hvigorw | D | 06-May-2025 | 2.1 KiB | 64 | 28 | |
| hvigorw.bat | D | 06-May-2025 | 2 KiB | 73 | 56 | |
| oh-package.json5 | D | 06-May-2025 | 815 | 27 | 25 | |
| ohosTest.md | D | 06-May-2025 | 735 | 14 | 9 |
README.md
1# Bluetooth 2 3### Introduction 4 5Bluetooth is a short-range wireless technology used to implement communication between fixed and mobile devices over low-cost short-distance wireless connections. This sample describes how to discover, pair, and unpair Bluetooth devices. The display effect is as follows: 6 7 8 9### Concepts 10 11Bluetooth: The Bluetooth module provides basic classic Bluetooth capabilities. 12 13### Required Permissions 14 15ohos.permission.USE_BLUETOOTH 16 17ohos.permission.LOCATION 18 19ohos.permission.DISCOVER_BLUETOOTH 20 21ohos.permission.MANAGE_BLUETOOTH 22 23ohos.permission.APPROXIMATELY_LOCATION 24 25### Usage 26 271. Start the app. If Bluetooth is enabled, the system searches for nearby devices that can be paired. If Bluetooth is not enabled, enable it. 28 292. After nearby devices are detected, select the device to pair. A dialog box is displayed. Touch **Pair** to set up a connection with this device; touch **Cancel** otherwise. 30 313. To delete a paired device, touch the device in the paired device list. A dialog box is displayed, asking you whether to disconnect from this device. Touch **OK** to disconnect from this device; touch **Cancel** otherwise. 32 33### Constraints 34 35- This sample can only be run on standard-system devices. 36 37- This sample is based on the stage model, which is supported from API version 9. 38 39- This sample requires DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100) to compile and run. 40 41- The ohos.permission.MANAGE_BLUETOOTH permission used in this sample is of the system_basic level. You need to configure a signature for it. For details about the application permission level (APL), see [Permission List](https://gitee.com/openharmony/docs/blob/master/en/application-dev/security/AccessToken/permissions-for-system-apps.md).
README_zh.md
1# 蓝牙(仅对系统应用开放) 2 3### 介绍 4 5蓝牙技术是一种无线数据和语音通信开放的全球规范,它是基于低成本的近距离无线连接,为固定和移动设备建立通信环境的一种特殊的近距离无线技术连接。本示例通过[@ohos.bluetooth](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-connectivity-kit/js-apis-bluetooth-sys.md) 接口实现蓝牙设备发现、配对、取消配对功能。实现效果如下: 6 7### 效果预览 8 9|发现设备|连接设备|断开连接| 10|--------------------------------|--------------------------------|--------------------------------| 11|||| 12 13使用说明 14 151.启动应用,若蓝牙本来就打开的,系统会直接搜索周围可配对的设备,若蓝牙未打开,打开开关。 16 172.搜索到附近的设备后,选择需要配对的设备,点击该设备,会跳出匹配弹窗,若确定匹配,点击**匹配**按钮,否则点击**取消**按钮。 18 193.若需要搜寻特定的设备可以使用命令"hdc shell ifconfig"命令查看被搜寻设备wlan的"HWaddr"字段地址,在已安装此应用的设备上点击相应的地址即可。 20 214.匹配后,若想删除匹配的设备,点击已配对的设备列表中的设备,会跳出断开连接弹窗,若确定删除该设备,点击**确定**,否则点击**取消**。 22 23### 工程目录 24``` 25entry/src/main/ets/ 26|---Application 27|---Common 28| |---PinDialog.ets // 任务信息组件 29| |---TitleBar.ets // 标题组件 30|---MainAbility 31|---Model 32| |---Logger.ts // 日志工具 33|---pages 34| |---Index.ets // 首页 35``` 36### 具体实现 37 38* 蓝牙设备发现、配对、取消配对等操作的功能结构主要封装在Index、PinDialog,源码参考:[Index.ets](entry/src/main/ets/pages/Index.ets) ,[PinDialog.ets](entry/src/main/ets/Commom/PinDialog.ets) 39 * 启动和关闭蓝牙:在Index页面中通过Toggle组件的onChange函数来控制蓝牙的开关,开关打开的情况下会执行initBluetooth函数,关闭的情况下执行bluetooth.disableBluetooth()方法来断开蓝牙; 40 * 验证蓝牙是否处于连接状态:蓝牙打开的时候通过bluetooth.on('stateChange')方法来监听蓝牙连接状态改变事件,确认已打开,则执行foundDevices()函数来查找设备接口,确认已关闭则执行bluetooth.stopBluetoothDiscovery()方法来停止查询接口。 41 * 发现设备:在foundDevices()函数中通过bluetooth.on('bluetoothDeviceFind')方法来监听设备发现事件,并且通过bluetooth.getPairedDevices()方法来更新已配对蓝牙地址,然后通过bluetooth.startBluetoothDiscovery()方法开启蓝牙扫描,去发现远端设备,并且通过bluetooth.setBluetoothScanMode()方法来被远端设备发现。 42 * 蓝牙配对:通过bluetooth.on('pinRequired')方法来监听远端蓝牙设备的配对请求事件,点击配对执行bluetooth.setDevicePairingConfirmation(this.data.deviceId, true)方法,点击取消执行bluetooth.setDevicePairingConfirmation(this.data.deviceId, false)方法。 43 * 取消配对:最后通过bluetooth.cancelPairedDevice()来断开指定的远端设备连接。 44 45### 相关权限 46 47[ohos.permission.USE_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionuse_bluetooth) 48 49[ohos.permission.LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionlocation) 50 51[ohos.permission.DISCOVER_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissiondiscover_bluetooth) 52 53[ohos.permission.MANAGE_BLUETOOTH](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionmanage_bluetooth) 54 55[ohos.permission.APPROXIMATELY_LOCATION](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissionapproximately_location) 56 57### 依赖 58 59不涉及。 60 61### 约束与限制 62 631.本示例仅支持标准系统上运行。 64 652.本示例已适配API version 9版本SDK,本示例涉及使用系统接口:cancelPairedDevice(),需要手动替换Full SDK才能编译通过,具体操作可参考[替换指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/full-sdk-switch-guide.md) 。 66 673.本示例需要使用DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400, built on April 7, 2023)及以上版本才可编译运行。 68 694.本示例所配置的权限ohos.permission.MANAGE_BLUETOOTH为system_basic级别(相关权限级别可通过[权限定义列表](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md) 查看),需要手动配置对应级别的权限签名(具体操作可查看[自动化签名方案](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/hapsigntool-overview.md)) 。 70 71### 下载 72 73如需单独下载本工程,执行如下命令: 74``` 75git init 76git config core.sparsecheckout true 77echo code/SystemFeature/Connectivity/Bluetooth/ > .git/info/sparse-checkout 78git remote add origin https://gitee.com/openharmony/applications_app_samples.git 79git pull origin master 80 81``` 82