# BluetoothSearch **Repository Path**: superzhen/bluetooth-search ## Basic Information - **Project Name**: BluetoothSearch - **Description**: 【鸿蒙 Harmony Next 示例 代码】本示例基于@ohos.bluetooth.ble,引入ble,实现蓝牙设备的扫描与连接绑定。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-04-03 - **Last Updated**: 2025-04-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 实现蓝牙搜索功能 ## 介绍 本示例基于@ohos.bluetooth.ble,引入ble,实现蓝牙设备的扫描与连接绑定。 ## 效果预览 ![](screenshots/ble.gif) ## 约束与限制 1. 本示例支持 API Version 12 Release及以上版本。 2. 本示例支持 HarmonyOS 5.0.0 Release SDK及以上版本。 3. 本示例需要使用DevEco Studio 5.0.0 Release及以上版本进行编译运行。 ## 使用说明 授予权限后,点击绑定设备,即可搜索蓝牙设备进行绑定。 ### 权限说明 需要开启蓝牙权限 ```markdown "name": "ohos.permission.ACCESS_BLUETOOTH" ``` ## 实现思路 利用Tab组件构建主页面不同界面切换 ```typescript Tabs({ barPosition: BarPosition.End }) { TabContent() { // 设备 DeviceDetail() } .tabBar(this.BottomTabBuilder(this.tabList[0], 0)) TabContent() { //NavigationDetail() // 导航 } .tabBar(this.BottomTabBuilder(this.tabList[1], 1)) TabContent() { // 我的 } .tabBar(this.BottomTabBuilder(this.tabList[2], 2)) } ``` 连接绑定设备 ```typescript CustomButton({ title: 'notifyCharacteristicChanged' }).onClick(() => { let uuidBean = new UUIDBean(); //uuidBean.serviceUuid = '00000123-0000-1000-8000-00805F9B34FB' uuidBean.serviceUuid = '0000AE30-0000-1000-8000-00805F9B34FB' uuidBean.characteristicUuid = '0000AE02-0000-1000-8000-00805F9B34FB'//uuidBean.characteristicUuid = '00000003-0000-1000-8000-00805F9B34FB' //uuidBean.descriptorUuid = "" //'00002902-0000-1000-8000-00805F9B34FB' this.deviceManager.onCharacteristicValue(uuidBean) let arrayBuffer = BufferUtil.hexToArrayBuffer('aa7d000100'); let writeUuidBean = new UUIDBean(); writeUuidBean.serviceUuid = '0000AE30-0000-1000-8000-00805F9B34FB' writeUuidBean.characteristicUuid = '0000AE01-0000-1000-8000-00805F9B34FB' //uuidBean.descriptorUuid = "" //'00002902-0000-1000-8000-00805F9B34FB' this.deviceManager.writeCharacteristicValue(writeUuidBean, arrayBuffer).then(() => { }).catch((error: BusinessError) => { this.readCharacteristicValue = error.code + error.message }) }) ``` ## 工程目录 ```markdown unistep |---commons/common/src/main/ets/ | |---adapter | | |---ManDeviceDataSource.ets | |---bean | | |---ManDeviceBean.ets | | |---UUIDBean.ets | |---components/button | | |---CustomButton.ets | |---constants | | |---CommonConstants.ets | | |---RouteConstants.ets | |---listener | | |---BleScanListener.ets | | |---ScanListener.ets | |---utils | | |---BleUtil.ets | | |---BTUtil.ets | | |---BufferUtil.ets | |---DeviceManager.ets |---features | |---device/src/main/ets/view | | |---BTScanDetails.ets | | |---BTScanPage.ets | | |---DeviceConnectDetails.ets | | |---DeviceConnectPage.ets | | |---DeviceDetail.ets | |---home/src/main/ets/ | | |---constants | | | |---HomeConstants.ets | | |---view | | | |---Home.ets | | |---viewmodel | | | |---FooterTabViewModel.ets |---products/default/src/main/ets | |---entryability | | |---EntryAbility.ets | |---entrybackupability | | |---EntryBackupAbility | |---pages | | |---Index.ets // 主页 ``` ## 模块依赖 无 ## 参考文档 [蓝牙ble模块](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-bluetooth-ble-V5) ## ChangeLog | 修改内容 | 时间 | |------|------------| | 优化代码 | 2025.02 |