【问题标题】:Getting Bluetooth working in Ionic让蓝牙在 Ionic 中工作
【发布时间】:2016-06-10 09:22:25
【问题描述】:

我第一次使用 Ionic 插件。有一台 PaypalHere 平板电脑,我正在尝试连接到我的应用程序,该应用程序使用蓝牙。所以以后需要在某个地方输入一个Mac地址。但只需要先让蓝牙工作。

我已经使用“cordova plugin add cordova-plugin-bluetooth-serial”安装了蓝牙 https://github.com/don/BluetoothSerial

我最初安装了 npm inistall -g cordova,但现在看到有关 ngcordova 教程的参考资料?

谁能帮助我现在需要添加到我的 app.js 文件(控制器)以获得蓝牙正在工作并完全启用蓝牙的提示?

我假设我需要在控制器中注入服务,然后运行一些函数来执行检查。

这里有一个帖子,但似乎不完整 BluetoothSerial plugin is not working with ionic

这是我的 app.js 代码:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {


    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('tabs', {
      url: '/tab',
      abstract: true,
      templateUrl: 'templates/tabs.html'
    })
    .state('tabs.home', {
      url: '/home',
      views: {
        'home-tab' : {
         templateUrl: 'templates/home.html'
        }
      }
    })
    .state('tabs.list', {
      url: '/list',
      views: {
        'list-tab' : {
         templateUrl: 'templates/list.html',
         controller: 'ListController'
        }
      }
    })
    .state('tabs.detail', {
      url: '/list/:aId',
      views: {
        'list-tab' : {
         templateUrl: 'templates/detail.html',
         controller: 'ListController'
        }
      }
    })

      // if none of the above states are matched, use this as the fallback
      $urlRouterProvider.otherwise('/tab/home');

})

.controller('ListController', ['$scope', '$http', '$state', function($scope, $http, $state) {
        $http.get('js/data.json').success(function(data) {
          $scope.orders = data;
          $scope.whichorder = $state.params.aId;
        });
}]);

我在 PC 上的 Chrome 浏览器中执行此操作

【问题讨论】:

标签: angularjs cordova ionic-framework bluetooth


【解决方案1】:

首先看一下documentation,然后在你的控制器中创建一个这样的函数:

enableBluetooth() {
    bluetoothSerial.enable(
        function () {
            alert("Bluetooth is enabled");
        },
        function () {
            alert("The user did *not* enable Bluetooth");
        }
    );
}

现在您可以从按钮点击事件中调用enableBluetooth() 方法,例如:

<button ng-click="controller.enableBluetooth()">Enable Bluetooth</button>

单击按钮并接受成功回调后,应提醒蓝牙已启用。确保代码正常运行后,在此处添加要执行的代码。

注意两点:

  1. controller 是我的控制器在相应视图中的名称。
  2. 当然你不必通过按钮点击来触发enableBluetooth()方法,这只是一个例子。您可以根据自己的需要随意调整。

从长远来看,您可能需要考虑创建(蓝牙)服务并将所有与蓝牙相关的代码放在那里。只需搜索 Angular 服务,那里有很多很好的教程,也有关于 SO 的。但我强烈建议先让它工作;)


附加信息: 得到这个在我的 Android 6 设备上运行的简单示例,确保该插件也支持您的平台(浏览器)!

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 2015-08-09
    • 2021-12-05
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多