feat: 获取sn
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_webrtc/flutter_webrtc.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import 'config/ice_servers.dart';
|
||||
import 'controller/remote_controller.dart';
|
||||
import 'utils/control_commands.dart';
|
||||
import 'utils/device_utils.dart';
|
||||
import 'widgets/remote_touch_view.dart';
|
||||
|
||||
void main() {
|
||||
@@ -54,7 +54,15 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_deviceIdController.text = 'controller_${const Uuid().v4().substring(0, 8)}';
|
||||
_initDeviceId();
|
||||
}
|
||||
|
||||
/// 异步获取设备标识并填入设备ID输入框(非系统签名,使用兜底方案)。
|
||||
Future<void> _initDeviceId() async {
|
||||
final id = await DeviceUtils.getSerialNumber();
|
||||
if (mounted) {
|
||||
setState(() => _deviceIdController.text = id);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
42
webrtc_controller_flutter/lib/utils/device_utils.dart
Normal file
42
webrtc_controller_flutter/lib/utils/device_utils.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
/// 设备信息工具类(控制端,非系统签名应用)
|
||||
///
|
||||
/// 与 WebRTCControlled 的 DeviceUtils 对应,但控制端没有系统签名,
|
||||
/// 因此无法稳定获取真实的硬件序列号,这里采用「不要求准确」的兜底方案:
|
||||
/// 1. 优先使用官方插件([DeviceInfoPlugin])提供的 serialNumber;
|
||||
/// 2. 失败则使用 iOS 的 identifierForVendor;
|
||||
/// 3. 再失败则生成随机 UUID。
|
||||
class DeviceUtils {
|
||||
DeviceUtils._();
|
||||
|
||||
/// 获取设备标识(适用于普通应用)。
|
||||
///
|
||||
/// 兼容各平台:Android 使用官方序列号,iOS 使用 identifierForVendor,
|
||||
/// 均不可用时回退到随机 UUID。
|
||||
static Future<String> getSerialNumber() async {
|
||||
const uuid = Uuid();
|
||||
try {
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
if (Platform.isAndroid) {
|
||||
final androidInfo = await deviceInfo.androidInfo;
|
||||
final serial = androidInfo.serialNumber;
|
||||
if (serial.isNotEmpty && serial.toLowerCase() != 'unknown') {
|
||||
return serial;
|
||||
}
|
||||
} else if (Platform.isIOS) {
|
||||
final iosInfo = await deviceInfo.iosInfo;
|
||||
final id = iosInfo.identifierForVendor;
|
||||
if (id != null && id.isNotEmpty) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
// 获取失败,走兜底逻辑
|
||||
}
|
||||
return uuid.v4();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user