【问题标题】:How to get device id in ionic by using cordova plugin?如何使用cordova插件在离子中获取设备ID?
【发布时间】:2016-06-02 11:25:54
【问题描述】:

我正在开发一个混合离子应用程序

我想获取 android 设备的设备 ID。我安装了 cordova 插件,包含 cordova.min.js 文件。

我尝试了这段代码,仍然没有显示任何内容。

还有其他方法可以获取设备id吗?

angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('DeviceController', function($ionicPlatform, $scope, $cordovaDevice) {
$ionicPlatform.ready(function() {
$scope.$apply(function() {

var device = $cordovaDevice.getDevice();
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
});
});
})
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->
    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="js/ng-cordova.min.js"></script>
    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>
    <!-- your app's js -->
    <script src="js/app.js"></script>
</head>

<body ng-app="starter" ng-controller="DeviceController">
    <ion-pane>
        <ion-header-bar class="bar-stable">
            <h1 class="title">Device Information</h1>
        </ion-header-bar>
        <ion-content>
            <div class="item item-text-wrap">
                <ul class="list">
                    <li class="item">
                        Manufacturer : {{manufacturer}}
                    </li>
                    <li class="item">
                        Model : {{model}}
                    </li>
                    <li class="item">
                        Platform : {{platform}}
                    </li>
                    <li class="item">
                        UUID : {{uuid}}
                    </li>
                </ul>
            </div>
        </ion-content>
    </ion-pane>
</body>

</html>

【问题讨论】:

    标签: angularjs cordova ionic-framework uuid hybrid-mobile-app


    【解决方案1】:

    如果您已安装设备插件,只需执行window.device.uuid(在您收到设备就绪事件后)。

    编辑

    再次查看您的代码,您不需要在控制器中准备好平台,我不确定它是否会像那样触发。另外你为什么在那里打电话申请?这个怎么样:

    .controller('DeviceController', function () {
        alert(window.device.uuid);
    });
    

    【讨论】:

    • 仍然无法正常工作,是否必须将设备连接到互联网才能获取 uuid?
    • @PrathikKotian 不,您无需连接即可获取 uid。究竟是什么问题? window.device 对象是否未定义?
    【解决方案2】:

    您可以在您的项目中添加以下插件UniqueDeviceID

    并在 ondevice ready 中添加以下行

    window.plugins.uniqueDeviceID.get(成功,失败);

    在代码中添加成功和失败函数

    function success(){
        alert(uuid);
     }
    
    
    
    function fail(error){
     alert("error " + error);
    }
    

    【讨论】:

      【解决方案3】:

      这里要注意的是,ng-cordova.js 内部调用了方法和 API 的 od native cordova,所以即使在安装了 ng-cordova.js 之后,单独安装 cordova 插件也是非常重要的。然后我在 app.module 中初始化了设备。

      $ionicPlatform.ready(function() {
        $scope.deviceInformation = ionic.Platform.device();
      });
      

      我在介绍控制器中调用了该方法

      $scope.getDeviceInfo = function() {
        alert($scope.deviceInformation.uuid);
      }
      

      或者通过http://ngcordova.com/docs/plugins/device/会帮助你..

      【讨论】:

        【解决方案4】:

        我明白了,代码是完美的,没有错,即使插件安装正确。问题是 ng-cordova.min.js。我刚刚下载了最新版本的 ng-cordova.min.js。而且它工作得很好。 谢谢大家。 这可能对其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-03
          • 2022-10-07
          • 1970-01-01
          • 2015-02-12
          • 2017-04-01
          • 2015-08-01
          相关资源
          最近更新 更多