简体   繁体   English

BluetoothSerial插件不适用于离子

[英]BluetoothSerial plugin is not working with ionic

I am trying to integrate bluetooth in my ionic app so that it can receive data from a bluetooth device.I tried to use the Bluetooth plugin but I get a bunch of errors both on PC and on android device. 我试图在我的离子应用程序中集成蓝牙,以便它可以从蓝牙设备接收数据。我试图使用蓝牙插件,但我在PC和Android设备上都收到了一堆错误。 I have installed bluetoothserial using the following command 我使用以下命令安装了bluetoothserial

cordova plugin add com.megster.cordova.bluetoothserial cordova插件添加com.megster.cordova.bluetoothserial

%cordova plugins
com.ionic.keyboard 1.0.4 "Keyboard"
com.megster.cordova.bluetoothserial 0.4.3 "Bluetooth Serial"
cordova-plugin-whitelist 1.0.1-dev "Whitelist"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"

and here is my code 这是我的代码

main.html main.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">
 <script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-tags-input/2.3.0/ng-tags-input.min.js"> </script>
<script src="js/app.js"></script>
</head>

app.js app.js

angular.module('starter'   ['ionic','ngCordova','ngRoute','ngTagsInput'])
.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)
    $cordovaBluetoothSerial.isEnabled().then(
       function() {
          $cordovaDialogs.alert("Bluetooth LE is enabled", "Bluetooth LE", "GREAT!");
      },
      function() {
         $cordovaDialogs.alert("Bluetooth LE is NOT enabled", "Bluetooth LE", "Oops!");
      }
   );
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
 });
})
.controller('datactr',['$scope','$http'   ,'$cordovaBluetoothSerial',function($scope,$http,$cordovaBluetoothSerial)    {
console.log($cordovaBluetoothSerial.available());
}]);

I get the following errors: 我收到以下错误:

TypeError: Cannot read property 'available' of undefined TypeError:无法读取undefined属性'available'

Uncaught ReferenceError: $cordovaBluetoothSerial is not defined 未捕获的ReferenceError:$ cordovaBluetoothSerial未定义

can anyone help? 有人可以帮忙吗?

Use $timeout, It's working for me. 使用$ timeout,它对我有用。

.controller('BlueController',function ($cordovaBluetoothSerial,$scope,$timeout) {...

$scope.checkBT = function (time) {
  $timeout(function () {
    $cordovaBluetoothSerial.isEnabled().then(fun,fun);
   },time); 
};
$scope.checkBT(750);

Try 尝试

$ionicPlatform.ready(function() {

          $cordovaBluetoothSerial.isEnabled().then(function(){
            $scope.habilitado = true;
              alert('Bluetooth habilitado');

            },function(){
              alert('Bluetooth nao habilitado');

          });

    });

This problem is more or less linked to the fact that $cordovaBluetoothSerial isn't available when you are trying to use it. 这个问题或多或少与您尝试使用它时无法使用$ cordovaBluetoothSerial这一事实有关。

Also you need to make sure that you are not testing on your browser, because it won't work! 此外,您需要确保您没有在浏览器上进行测试,因为它不起作用! Test on a proper Android or iOS device. 在适当的Android或iOS设备上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM