don / cordova-plugin-ble-central

Bluetooth Low Energy (BLE) Central plugin for Apache Cordova (aka PhoneGap)
Apache License 2.0
942 stars 603 forks source link

ble is not defined #79

Closed daniperezluna closed 8 years ago

daniperezluna commented 8 years ago

Hi, I'm triying to develop an app with ionic an ngCordova but when I launch the app I receive the following message:

ReferenceError: ble is not defined at Object.isEnabled (ng-cordova.js:610) at new (controllers.js:35) at invoke (ionic.bundle.js:13012) at Object.instantiate (ionic.bundle.js:13020) at ionic.bundle.js:17289 at self.appendViewElement (ionic.bundle.js:48435) at Object.switcher.render (ionic.bundle.js:46629) at Object.switcher.init (ionic.bundle.js:46549) at self.render (ionic.bundle.js:48295) at self.register (ionic.bundle.js:48253)

This is my 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'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', '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 && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

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

});

And this my controllers.js:

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope) {})

.controller('ChatsCtrl', function($scope, Chats) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})

.controller('AccountCtrl', function($ionicPlatform,$scope,$cordovaBLE) {

  $ionicPlatform.ready(function(){

         $cordovaBLE.isEnabled(
    function() {
        alert("Bluetooth is enabled");
    },
    function() {
        alert("Bluetooth is *not* enabled");
    }
);
  });

})
NewLunarFire commented 8 years ago

ble is not defined if the platform you are testing your code on doesn't support BLE.

daniperezluna commented 8 years ago

I am testing my code directly on Android 4.4.2 who supports BLE.

daniperezluna commented 8 years ago

If you need more information about my devices I am using :

Thanks for your answers and your help

NewLunarFire commented 8 years ago

From the specs of the phone Bluetooth Smart should be supported, but just to make sure have you been able to connect to your peripheral from another application?

don commented 8 years ago

ble is defined by the plugin. If you're seeing errors about ble not defined, it's typically that the plugin didn't install correctly or you're trying to call a function before Cordova fires ondeviceready. Try uninstalling and re-installing the plugin first.

daniperezluna commented 8 years ago

I install again the plugin and it's works properly. I am using ionic and I can scan but I can't connect to my device. I have the macAddress but when I do ble.connect(peripheral.id, app.onConnect, app.onDisconnect); never connect to the device.

Someone knows why is it?

I have tried with the heart rate example, using the functions inside ionicPlatform and I have changed the values of the heartRate.service and heartRate.measurement.

I have a blood pressure monitor and those values are '1810' and '2a35' but when the monitor sends data and I try to connect always say that is not possible.

I saw you use ble.notify but it is not present in factory.

don commented 8 years ago

@daniperezluna try connecting to your phone to the monitor using nRF Master Control Panel

You'll need ble.startNotification to get blood pressure measurement, since that's sent with an indication and not readable.

soumendub3 commented 8 years ago

@don I have the similar issue as daniperezluna started the issue with and I followed the instructions, uninstalled the plugin and reinstalled back but I cannot get the plugin to work. I am using ionic framework with AngularJS. @daniperezluna Also, what I am not following is how do you get $cordovaBLE in controllers.js? Are you still using the same code as posted above? If not, can you share your working code? Thanks.

daniperezluna commented 8 years ago

@soumendub3 Have you tried to debug it on your phone? My problem was that this message appears when I open the app in the browser

don commented 8 years ago

@soumendub3 I suggest you get simple Bluetooth examples working without Ionic or Angular, then work on adding Bluetooth support to your Ionic app.

I have a simple Ionic Bluetooth LE App you can try too. https://github.com/don/ionic-ble. Note that it does not use the ng-cordova wrappers.

Look at BLE in services.js and BLECtrl and BLEDetailCtrl in controllers.js

soumendub3 commented 8 years ago

@don Thanks for the response Don - yes, I found your ionic-ble code and I started with your example. I had trouble with it as well and I created an issue in the project. I understand the BLE and BLEDetailCtrl and see how you defined them but my trouble there was different. Can you please take a look at that one? Link to the issue

soumendub3 commented 8 years ago

@daniperezluna Yes, I tried on my phone and it was not related to browser.

don commented 8 years ago

@daniperezluna great news

@soumendub3 I'll continue this in the other project

daniperezluna commented 8 years ago

@soumendub3 Here you are my code https://github.com/daniperezluna/BlueHeart It's an app to read data from a Blood Pressure. To try it you have to see that only seek to devices who has its characteristics

wifixcort commented 8 years ago

I have the same issue in ionic. If I write: ble.isEnabled(function() { alert("Bluetooth is enabled"); ble.scan([], 5, function(device) { console.log(JSON.stringify(device)); }, function(function(error){}){}); }, function() { alert("Bluetooth is DISABLED"); } ); works, but just in app.js, if I try to use in my controller.js it doesn't work

abnuozal commented 8 years ago

Make it like this in your controller:

$ionicPlatform.ready(function(){ window.ble.isEnabled( //do something ); })

20gg commented 7 years ago

@wifixcort you can try this way.bind Event with $scope ,just like

$scope.onIsEnabled = function(){ ble.isEnabled( function() { console.log("Bluetooth is enabled"); }, function() { console.log("Bluetooth is not enabled"); } ); }

wifixcort commented 7 years ago

@20gg Thanks for your advice, I'll try it :+1: