initDeviceId method
dynamic
initDeviceId()
初始化设备ID,并存入SP中
Implementation
initDeviceId() async {
// DeviceInfoFlutterPlugin().getAppleIDClientIdentifier().then((value) => print('getAppleIDClientIdentifier = $value'));
// DeviceInfoFlutterPlugin().getIMEI().then((value) => print('getAppleIDClientIdentifier = $value'));
String? deviceId = PrefUtils.getString(SPKey.keyStrDeviceId);
_log("initDeviceId from cache deviceId(md5): $deviceId");
if (TextUtils.isEmpty(deviceId)) {
try {
deviceId = await PlatformDeviceId.getDeviceId;
if (PlatformUtils.isIos()) {
// 针对IOS设备特殊处理,如果钥匙串里面有设备码,优先读取,没有就写入当前获取到的设备码
String? iosDeviceId = await FlutterKeychain.get(key: keyIOSDeviceId);
if (TextUtils.isNotEmpty(iosDeviceId)) {
deviceId = iosDeviceId;
} else {
// final deviceInfoPlugin = DeviceInfoPlugin();
// final iosInfo = await deviceInfoPlugin.iosInfo;
// print('Running on ${iosInfo.name}'); // e.g. "张三"
// print('Running on ${iosInfo.model}'); // e.g. "iPhone"
// print('Running on ${iosInfo.systemName}'); // e.g. "iOS"
// print('Running on ${iosInfo.systemVersion}'); // e.g. "13.4.1"
// print('Running on ${iosInfo.localizedModel}'); // e.g. "iPhone"
// print('Running on ${iosInfo.identifierForVendor}'); // e.g. "iPod7,1"
// print('Running on ${iosInfo.isPhysicalDevice.toString()}'); // e.g. "true"
// print('Running on ${iosInfo.utsname.sysname}'); // e.g. "Darwin"
// print('Running on ${iosInfo.utsname.nodename}'); // e.g. "张三"
// print('Running on ${iosInfo.utsname.release}'); // e.g. "19.4.0"
// print('Running on ${iosInfo.utsname.version}'); // e.g. "Darwin Kernel Version 19.4.0: Mon Feb 24 22:04:29 PST 2020; root:xnu-6153.102.3~1/RELEASE_ARM64_T8015"
// print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPhone10,4"
// DeviceInfoFlutterPlugin().getAppleIDClientIdentifier().then((value) => print('getAppleIDClientIdentifier = $value'));
// DeviceInfoFlutterPlugin().getIMEI().then((value) => print('getAppleIDClientIdentifier = $value'));
await FlutterKeychain.put(key: keyIOSDeviceId, value: deviceId ?? "IOS_${DateTime.now().millisecondsSinceEpoch}");
}
} else if (kIsWeb) {
// 针对H5的设备码特殊处理
int randomInt = Random().nextInt(100000); // 10万以内的随机数
if (TextUtils.isNotEmpty(deviceId)) {
deviceId = "${deviceId}H5_${DateTime.now().millisecondsSinceEpoch}$randomInt";
} else {
deviceId = "H5_${DateTime.now().millisecondsSinceEpoch}$randomInt";
}
}
_log("initDeviceId create deviceId: $deviceId");
} on PlatformException {
_log("initDeviceId create failed: unable to get deviceId.");
}
if (null != deviceId) {
/// md5(商户编码+设备编码)
String combineDeviceId = LiveSdkManager.instance.getAppId() + deviceId;
_log("initDeviceId combineDeviceId: $combineDeviceId");
String encodeMd5 = EncryptUtils.encodeMd5(combineDeviceId);
_log("initDeviceId combineDeviceId md5: $encodeMd5");
_deviceId = encodeMd5;
PrefUtils.setString(SPKey.keyStrDeviceId, encodeMd5);
}
}
await initDeviceInfo();
}