40 lines
1.4 KiB
Dart
40 lines
1.4 KiB
Dart
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
||
|
|
|
||
|
|
import 'package:get/get.dart';
|
||
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
||
|
|
|
||
|
|
class DeviceInfoUtils {
|
||
|
|
static Future<Map<String, String>> getDeviceInfo() async {
|
||
|
|
Map<String, String> deviceInfo = {};
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 获取设备信息
|
||
|
|
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
|
||
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
||
|
|
|
||
|
|
if (GetPlatform.isAndroid) {
|
||
|
|
AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
|
||
|
|
deviceInfo['model'] = androidInfo.model;
|
||
|
|
deviceInfo['deviceName'] = androidInfo.device;
|
||
|
|
deviceInfo['brand'] = androidInfo.brand;
|
||
|
|
deviceInfo['id'] = androidInfo.id;
|
||
|
|
// deviceInfo['uniqueIdentifier'] = androidInfo.androidId ?? 'N/A'; // 使用 androidId 作为替代
|
||
|
|
} else if (GetPlatform.isIOS) {
|
||
|
|
IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
|
||
|
|
// deviceInfo['model'] = iosInfo.model;
|
||
|
|
// deviceInfo['deviceName'] = iosInfo.name;
|
||
|
|
deviceInfo['uniqueIdentifier'] =
|
||
|
|
iosInfo.identifierForVendor ?? 'N/A'; // 使用 identifierForVendor 作为替代
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取 APP 版本
|
||
|
|
deviceInfo['appVersion'] = packageInfo.version;
|
||
|
|
deviceInfo['appName'] = packageInfo.appName;
|
||
|
|
} catch (e) {
|
||
|
|
print("Failed to get device info: $e");
|
||
|
|
}
|
||
|
|
|
||
|
|
return deviceInfo;
|
||
|
|
}
|
||
|
|
}
|