Flutter获取Android/iOS设备信息

我们在进行各个系统的原生开发时,都有对应的方法获取设备信息,那么在使用Flutter时如何获取设备相关的相关信息呢?

我们本文就来介绍一个Flutter插件:
Flutter Device Info

下面我们来逐步介绍如何获取设备信息。

首先在工程的pubspec.yaml中添加依赖

dependencies:
  device_info: ^0.4.0+1

下载安装这个依赖包

在工程主目录下执行:

flutter packages get

在代码中使用

首先我们引入device_info.dart:

import 'package:device_info/device_info.dart';

iOS安装cocoapods

如果需要运行在iOS上运行,如果电脑上没有安装cocoapods,此时会报出异常:

Warning: CocoaPods not installed. Skipping pod install.
  CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart side.
  Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
  For more info, see https://flutter.io/platform-plugins
To install:
  brew install cocoapods
  pod setup

此时我们则需要安装并配置cocoapods(确保机器上已经安装好了brew):

brew install cocoapods
pod setup

获取Android与iOS设备信息

void getDeviceInfo() async {
  DeviceInfoPlugin deviceInfo = new DeviceInfoPlugin();
  if(Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;    
    print(_readAndroidBuildData(androidInfo).toString());
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    print(_readIosDeviceInfo(iosInfo).toString());
  }
}

构造设备信息的Map
为了更方便的打印和使用信息,我们将AndroidDeviceInfo和IosDeviceInfo构造成Map:

Android:

Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
  return <String, dynamic>{
    'version.securityPatch': build.version.securityPatch,
    'version.sdkInt': build.version.sdkInt,
    'version.release': build.version.release,
    'version.previewSdkInt': build.version.previewSdkInt,
    'version.incremental': build.version.incremental,
    'version.codename': build.version.codename,
    'version.baseOS': build.version.baseOS,
    'board': build.board,
    'bootloader': build.bootloader,
    'brand': build.brand,
    'device': build.device,
    'display': build.display,
    'fingerprint': build.fingerprint,
    'hardware': build.hardware,
    'host': build.host,
    'id': build.id,
    'manufacturer': build.manufacturer,
    'model': build.model,
    'product': build.product,
    'supported32BitAbis': build.supported32BitAbis,
    'supported64BitAbis': build.supported64BitAbis,
    'supportedAbis': build.supportedAbis,
    'tags': build.tags,
    'type': build.type,
    'isPhysicalDevice': build.isPhysicalDevice,
    'androidId': build.androidId
  };
}

iOS:

Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
  return <String, dynamic>{
    'name': data.name,
    'systemName': data.systemName,
    'systemVersion': data.systemVersion,
    'model': data.model,
    'localizedModel': data.localizedModel,
    'identifierForVendor': data.identifierForVendor,
    'isPhysicalDevice': data.isPhysicalDevice,
    'utsname.sysname:': data.utsname.sysname,
    'utsname.nodename:': data.utsname.nodename,
    'utsname.release:': data.utsname.release,
    'utsname.version:': data.utsname.version,
    'utsname.machine:': data.utsname.machine,
  };
}

设备信息示例(基于模拟器):

Android:

{
    version.securityPatch: 2018-09-05, 
    version.sdkInt: 28, 
    version.release: 9, 
    version.previewSdkInt: 0, 
    version.incremental: 5124027, 
    version.codename: REL, 
    version.baseOS: , 
    board: goldfish_x86_64, 
    bootloader: unknown, 
    brand: google, 
    device: generic_x86_64, 
    display: PSR1.180720.075, 
    fingerprint: google/sdk_gphone_x86_64/generic_x86_64:9/PSR1.180720.075/5124027:user/release-keys, 
    hardware: ranchu, 
    host: abfarm730, 
    id: PSR1.180720.075, 
    manufacturer: Google, 
    model: Android SDK built for x86_64, 
    product: sdk_gphone_x86_64, 
    supported32BitAbis: [x86], 
    supported64BitAbis: [x86_64], 
    supportedAbis: [x86_64, x86], 
    tags: release-keys, 
    type: user, 
    isPhysicalDevice: false,
    androidId: 998921b52c7a7b79
}

iOS:

{
    name: iPhone XR, 
    systemName: iOS, 
    systemVersion: 12.1, 
    model: iPhone, 
    localizedModel: iPhone, 
    identifierForVendor: 367F5936-39E1-4DFA-8DD2-9542424256BE, 
    isPhysicalDevice: false, 
    utsname.sysname:: Darwin, 
    utsname.nodename:: bogon, 
    utsname.release:: 18.2.0, 
    utsname.version:: Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; 
    root:xnu-4903.241.1~1/RELEASE_X86_64, 
    utsname.machine:: x86_64
}

获取屏幕宽高密度等信息
关于如何获取屏幕宽度高度和分辨率等信息,
有需要的同学可以私信或者留言,我会发一篇
有兴趣的可以加群交流技术【Android技术交流】:https://jq.qq.com/?_wv=1027&k=55YgqsK。进群 免费领取安卓进阶学习视频,源码,面试资料,群内有大牛一起交流讨论技术;点击链接加入群聊(包括java基础与原理,自定义控件、NDK、架构设计、混合式开发(Flutter,Weex)、性能优化、完整商业项目开发等)

    原文作者:Android全栈
    原文地址: https://www.jianshu.com/p/4b6ac2014cb2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞