获取可信设备,设备与周边设备建立可信关系后,通过设备信息查询接口可以获取所有上线并且可信的设备。

设备与周边设备建立可信关系后,通过设备信息查询接口可以获取所有上线并且可信的设备。

HarmonyOS
2024-05-21 20:46:31
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
janux

核心代码解释

1. 申请分布式数据同步权限。

2. 发现周边不可信设备。

3. 建立设备间的可信关系。

4. 查询周围上线并且可行的设备。

核心代码如下:

Button("startMonitor") 
          .onClick(() => { 
            try { 
              //这里构造一个对象并赋值,原因是同一个包名构造的对象并不是单例的 
              this.dmInstance = deviceManager.createDeviceManager("com.example.test_240220184822040") 
              this.myDeviceId = this.dmInstance.getLocalDeviceId() 
              this.dmInstance.on('discoverSuccess', (data) => { 
 
                console.log('testTag - discoverSuccess on:' + JSON.stringify(data)) 
 
                //若deviceId等于目标id,则与该设备绑定信任关系   181cfec6c9e18c41  f7c2338a316948a5 
                if (data.device.deviceId == "f7c2338a316948a5") { 
                  //绑定信任设备所需options,即绑定的验证方式,本例为pin码方式验证 
                  let bindParam: Record<string, string | number> = { 
                    'bindType': 1, // 认证类型: 1 - 无帐号PIN码认证 
                  }; 
 
                  //回调数据对象,绑定成功后回调的对象封装 
                  class Data { 
                    deviceId: string = ''; 
                  } 
 
                  //绑定信任关系所用接口 
                  this.dmInstance!.bindTarget(data.device.deviceId, bindParam, (_err: BusinessError, data: Data) => { 
                    console.info('testTag - bindTarget result:' + JSON.stringify(data)); 
                  }) 
                } 
                hilog.info(0x0000, 'testTag', '%{public}s', JSON.stringify(data)); 
              }); 
            } catch (err) { 
              let e: BusinessError = err as BusinessError; 
              console.error('testTag - createDeviceManager errCode:' + e.code + ',errMessage:' + e.message); 
            } 
          }) 
 
 
        Text("myDeviceId - " + this.myDeviceId) 
          .id('message') 
          .textAlign(TextAlign.Center) 
          .width('100%') 
          .fontSize(15) 
          .copyOption(CopyOptions.LocalDevice) 
          .size({ width: '95%', height: '75%' }) 
          .border({ width: 1, radius: 20, color: Color.Black }) 
          .padding(10) 
          .margin({ top: 16 }) 
 
 
 
        Button("startDiscover").onClick(() => { 
          let discoverParam: Record<string, number> = { 
            'discoverTargetType': 1 
          }; 
          let filterOptions: Record<string, number> = { 
            'availableStatus': 0 
          }; 
          try { 
            this.dmInstance!.startDiscovering(discoverParam, filterOptions); 
          } catch (err) { 
            let e: BusinessError = err as BusinessError; 
            console.error('testTag - startDiscovering errCode:' + e.code + ',errMessage:' + e.message); 
          } 
          this.flagA = false 
          this.flagB = true 
        }).enabled(this.flagA) 
 
 
 
 
        Button("stopDiscover").onClick(() => { 
          try { 
            this.dmInstance!.stopDiscovering() 
          } catch (err) { 
            let e: BusinessError = err as BusinessError; 
            console.error('testTag - stopDiscovering errCode:' + e.code + ',errMessage:' + e.message); 
          } 
          this.flagA = true 
          this.flagB = false 
        }).enabled(this.flagB) 
 
 
 
        Button("getAvailableDeviceList").onClick(() =>{ 
          try { 
            let deviceInfoList: Array<deviceManager.DeviceBasicInfo> = this.dmInstance!.getAvailableDeviceListSync(); 
            console.log("testTag - deviceInfoList: " + JSON.stringify(deviceInfoList)) 
          } catch (err) { 
            let e: BusinessError = err as BusinessError; 
            console.error('testTag - getAvailableDeviceListSync errCode:' + e.code + ',errMessage:' + e.message); 
          } 
 
        })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.

总结:

发现周围终端设备并上报。周围设备需要连接同局域网或者同时打开蓝牙,可以根据设备类型、距离、设备是否可信等进行筛选;不同设备协同合作完成分布式业务的前提是设备间可信,对于周边发现的不可信设备,可通过绑定使彼此建立可信关系,提供pin码、碰、扫、靠等设备认证框架,支持对接各种认证交互接口。:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/devicemanager-guidelines-0000001774120506

实现效果

注明适配的版本信息

IDE:DevEco    Studio 4.1.3.500

SDK:HarmoneyOS    4.0.10.11

分享
微博
QQ
微信
回复
2024-05-22 16:49:55


相关问题
应用通过什么接口获取设备标识信息
2380浏览 • 1回复 待解决
鸿蒙如何获取设备绑定设备ID?
1802浏览 • 1回复 待解决
如何获取设备相关信息
2767浏览 • 1回复 待解决
如何获取当前设备设备名称
3580浏览 • 1回复 待解决
如何获取设备CPU信息
3354浏览 • 1回复 待解决
HarmonyOS 获取设备内存接口
606浏览 • 1回复 待解决
HarmonyOS 旋转设备获取设备方向问题
827浏览 • 1回复 待解决
HarmonyOS 应用如何获取设备信息
917浏览 • 1回复 待解决
设备信息udid无法获取
2810浏览 • 1回复 待解决
HarmonyOS 如何获取对应设备信息
514浏览 • 1回复 待解决
HarmonyOS 如何查询设备终端信息
743浏览 • 1回复 待解决
音频发声设备切换查询
1825浏览 • 1回复 待解决
如何获取设备信息,你会吗?
3177浏览 • 1回复 待解决
HarmonyOS如何获取规范格式设备信息
1067浏览 • 1回复 待解决
如何查询设备SDK版本等信息
3372浏览 • 1回复 待解决
HarmonyOS RN侧如何获取HarmonyOS设备信息
838浏览 • 1回复 待解决
HarmonyOS 关于获取设备信息内容确认
938浏览 • 1回复 待解决
HarmonyOS 获取设备UUID
788浏览 • 1回复 待解决