写点什么

harmony_flutter_udid

作者:flfljh
  • 2024-12-16
    湖南
  • 本文字数:1763 字

    阅读完需:约 6 分钟

harmony_flutter 获取 udid

UDID 说明:在恢复出厂设置后,UDID(唯一设备标识符)可能会发生变化!另外,如果设备通过 OTA(在线更新)升级到了 Android 8.0,并且应用程序被重新安装了,由于 Android 8.0 的安全性更改,UDID 也可能会改变。对于已经获取 root 权限或越狱的设备,其 ID 是可以被更改的,请注意这一点。不过,由于 ID 的复杂性,通过随机猜测来冒充另一个已存在的用户应该是不可能实现的。

ohos 端建立通道获取系统 udid

import identifier from '@ohos.identifier.oaid';//获取odid库
export default class FlutterUdidPlugin implements MethodCallHandler, FlutterPlugin { private channel: MethodChannel | null = null private applicationContext: common.Context | null = null private ability: UIAbility | null = null;
constructor(context?: common.Context) { if (context) { this.applicationContext = context; } }
static registerWith(): void { }
getUniqueClassName(): string { return TAG; }
onAttachedToEngine(binding: FlutterPluginBinding): void { this.onAttachedToEngine1(binding.getApplicationContext(), binding.getBinaryMessenger()); }
private onAttachedToEngine1(applicationContext: Context, messenger: BinaryMessenger) { this.applicationContext = applicationContext; this.channel = new MethodChannel(messenger, "flutter_udid") this.channel.setMethodCallHandler(this) }
onAttachedToAbility(binding: AbilityPluginBinding): void { this.ability = binding.getAbility() }
onDetachedFromAbility(): void { this.ability = null; }
onMethodCall(call: MethodCall, result: MethodResult): void { if (call.method == "getUDID") { this.requestPermissions().then((data: boolean) => { this.getUDID().then((udid: string | null) => { if (udid == null || udid == "") { result.error("UNAVAILABLE", "UDID not available.", null) } else { //传递到flutter层 result.success(udid) } }) }) } else { result.notImplemented() } }
onDetachedFromEngine(binding: FlutterPluginBinding): void { this.applicationContext = null; this.channel?.setMethodCallHandler(null) }
//ohos端获取oaid private async getUDID(): Promise<string | null> { try { return await identifier.getOAID() } catch (err) { return null } }
private async requestPermissions(): Promise<boolean> { if (!this.ability) { Log.i(TAG, "Could not launch BarcodeScanner because the plugin is not attached to any ability") return false } try { const results = await requestPermissions(this.ability!.context) return results ? results : false } catch (e) { } return false }}
复制代码

flutter 端代码


class FlutterUdid { //建立通道 static const MethodChannel _channel = const MethodChannel('flutter_udid');
/// Returns the UDID in the platform-specific format. /// iOS: 7946DA4E-8429-423C-B405-B3FC77914E3E, /// Android: 8af8770a27cfd182 static Future<String> get udid async { final String udid = await _channel.invokeMethod('getUDID'); return udid; }
/// Returns the UDID in a consistent format for all platforms. /// Example: 984725b6c4f55963cc52fca0f943f9a8060b1c71900d542c79669b6dc718a64b static Future<String> get consistentUdid async { final String udid = await _channel.invokeMethod('getUDID'); var bytes = utf8.encode(udid); var digest = sha256.convert(bytes); return digest.toString(); }}
复制代码

flutter example 引用

import 'package:flutter_udid/flutter_udid.dart';
String udid = await FlutterUdid.udid;
复制代码


用户头像

flfljh

关注

还未添加个人签名 2024-10-29 加入

还未添加个人简介

评论

发布
暂无评论
harmony_flutter_udid_flfljh_InfoQ写作社区