【OpenHarmony】鸿蒙开发之ohos_beacon_library

简介

蓝牙工具,主要涉及信标区域监控以及信标设备测距

效果展示

下载安装

dart 复制代码
ohpm install @ohos/beacon-library 

使用说明

信标组件使用样例

1.在page页面中引入信标库必要的文件

javascript 复制代码
import { Identifier, Region, BeaconParser, BeaconManager, RangeNotifier,Beacon } from '@ohos/beacon-library'
import bluetooth from '@ohos.bluetooth.ble';

2.使用

php 复制代码
- 启动测距
_this.beaconManager.addRangeNotifier(new class implements RangeNotifier {
    didRangeBeaconsInRegion(beacons: Array<Beacon>, region: Region): void {
        _this.beaconList = beacons
    }
});
_this.beaconManager.startRangingBeacons(region);

- 停止测距
this.beaconManager.stopRangingBeacons(new Region("myRangingUniqueId", null, null, null));

- 启动监控
_this.beaconManager.addMonitorNotifier(new class implements MonitorNotifier {
    INSIDE:number = 1
    OUTSIDE:number = 0

    didEnterRegion(region:Region):void {
        _this.enterRegionText = "I just saw a region: " + region.getUniqueId()
    }

    didExitRegion(region:Region):void {
        _this.enterRegionText = "I no longer see an beacon in Region:" + region.getUniqueId()
    }

    didDetermineStateForRegion(state:number, region:Region):void {
        _this.determineStateText = "I have just switched from seeing/not seeing beacons: " + state
    }
});
_this.beaconManager.startMonitoring(region);

- 停止监控
this.beaconManager.stopMonitoring(new Region("myMonitorUniqueId", null, null, null));

DD一下: 欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。

erlang 复制代码
`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点学习。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

接口说明

@State private beaconManager: BeaconManager = BeaconManager.getInstanceForApplication()

  1. 当扫描到信标时的监控通知处理接口 .beaconManager.addMonitorNotifier(notifier:MonitorNotifier): void
  2. 开启信标监控,查找匹配region的beacons .beaconManager.startMonitoring(region:Region ):void
  3. 当扫描到信标时的距离通知处理接口 .beaconManager.addRangeNotifier(notifier:RangeNotifier):void
  4. 开启信标距离测算 .beaconManager.startRangingBeacons(region:Region):void
  5. 为信标解释器指定编码规则 new BeaconParser().setBeaconLayout(beaconLayout:String):void
  6. 将常见的数据类型封装为一个Identifier类型。 Identifier.parse(stringValue:String, desiredByteLength?:number):Identifier
  7. 根据uniqueId与其它参数的组合,构造匹配信标的字段条件。 Region(uniqueId: string, id1?: Identifier, id2?: Identifier, id3?: Identifier, identifiers?: Array<Identifier>, bluetoothAddress?: string)

目录结构

lua 复制代码
|---- ohos_beacon_library  
|     |---- entry  # 示例代码文件夹
|     |---- library # beacon库
|	      |----src
|             |----main
|                 |----ets
|                     |----components
|                         |---- beacon # beacon库文件夹
|                             |---- client #客户端三种情况数据处理
|                             |---- distance 设备相关设置及距离配置逻辑
|                             |---- logging #日志相关逻辑
|                             |---- service #服务端处理逻辑
|                             |---- simulator #信标计算
|                             |---- startup #数据发送与接收
|                             |---- utils #工具类
|                             |---- AltBeacon.ets #建立信标通信
|                             |---- BeaconParser.ets #beacon数据包解析
|                         |---- bluetooth      # 蓝牙库
|                             |---- BleAdvertisement.ets #信标广播逻辑
|                             |---- Pdu.ets #蓝牙数据传递单元
|     |---- README.md  # 安装使用方法                    

关于混淆

  • 代码混淆,请查看代码混淆简介
  • 如果希望beacon-library库在代码混淆过程中不会被混淆,需要在混淆规则配置文件obfuscation-rules.txt中添加相应的排除规则:
bash 复制代码
-keep
./oh_modules/@ohos/beacon-library
相关推荐
缘澄2 小时前
组件基础-List&Tabs
harmonyos
爱笑的眼睛1117 小时前
HarmonyOS TextArea 组件:文本输入区域的简单使用指南
华为·harmonyos
前端世界17 小时前
鸿蒙异步处理从入门到实战:Promise、async/await、并发池、超时重试全套攻略
华为·harmonyos
祥睿夫子18 小时前
鸿蒙ArkTS开发:Number、Boolean、String三种核心基本数据类型详解(附实战案例)
harmonyos·arkts
小喷友19 小时前
第5章 高级UI与动画
前端·app·harmonyos
whysqwhw19 小时前
鸿蒙ArkTS 与 Native 交互场景分类总结与关键实现
harmonyos
爱笑的眼睛1119 小时前
HarmonyOS 递归实战:文件夹文件统计案例解析
华为·harmonyos
鸿蒙小白龙20 小时前
openharmony之启动恢复子系统详解
harmonyos·鸿蒙·鸿蒙系统
GeniuswongAir1 天前
交叉编译.so到鸿蒙使用
华为·harmonyos
keepDXRcuriosity1 天前
ArkTS 语言全方位解析:鸿蒙生态开发新选择
华为·harmonyos·arkts·鸿蒙