Trouble using BluetoothSerial in Ionic

Hi All,

I am trying to use ngCordova Bluetooth serial module in my app. I am trying to create a service that I could use in my controller. I have managed to get the list of bluetooth devices and I am able to log it within the service but I am not able to return the result back to the controller for further processing. Any help on this is much appreciated.

here is service
bluetoothServices.js

  angular.module('myApp.bluetoothServices', ['ngCordova'])
  .factory('bluetooth',['$cordovaBluetoothSerial','$ionicPopup','goBackMany','$q',function($cordovaBluetoothSerial,$ionicPopup,goBackMany,$q) {

// if isEnabled returns failure, this function is called:
  var showNotEnabledAlert = function(title, message) {
        var alertPopup = $ionicPopup.alert({
            title: 'Bluetooth not found',
            template: 'Bluetooth on the device is not enabled. Please enable bluetooth before connecting.'
        });
        alertPopup.then(function(res) {
        	goBackMany(1);
        });

    };

var listPaired = function($q) {
          // list the available BT ports:
          var d = $q.defer();
           $cordovaBluetoothSerial.list().then(
              function(results) {
                  d.resolve(results);
              },
              function(error) {
                  d.reject(error);
              }
          );
           return d.promise;
      }

return {
    autoconnect: function(name) {
        $cordovaBluetoothSerial.isEnabled().then(
        	listPaired,
          showNotEnabledAlert
      );
    }
  };
}]);

And here is my controller

angular.module('myApp.controllers', ['ionic', 'ngCordova', 'ngMessages'])
.controller('bluetoothCtrl', ['$scope',  'bluetooth',
  function($scope,bluetooth) {
      $scope.data = {};
   bluetooth.autoconnect('sdc').then(function(data){
        console.log(data);
   });
  }
]);

When I console log within the service in listPaired function. I am able to see the results but when I do the same in the controller, I am not able to get it. I am not sure what I am doing wrong here.

try removing $q from

var listPaired = function($q) {
ā€¦
}

Hello smhharan,

Did you solved your problem, because i just begun to use this bluetooth plugin and iā€™m having many problems. The first problem is: ā€œCannot read property ā€˜isEnabledā€™ of undefinedā€, even if i try the example in ionicview?
Can someone help me? Basically iā€™m trying with the code of smhharan.
Thank you.

@danyarzeta
Did you find any solution?

The code is ok, to test you code you have to build it and installing directly on your device, in my case a tested it on an android device i had to generate the apk, and it works. Is the only way you can prove it!

@danyarzeta Thanks for reply!!

So there is no way to test it in browser? Do i must need to deploy it on device?

Yes, it is the only way you can test your code. Make your apk and install it on your device.

1 Like

hi all

I am trying to build the bluetooth app my code is as follows but it showing me bluetoothSerial is not defined plz anybody tell wer i am going wrong

app.controller(ā€œbluetoothā€,function($scope,$ionicPlatform,$cordovaBluetoothSerial){

 $scope.bluetoothEnable="";
 console.log($scope.bluetoothEnable)

 bluetoothSerial.isEnabled(
    function() {
      $scope.bluetoothEnable="Bluetooth Enable";
    },
    function() {
      $scope.bluetoothEnable="Bluetooth Disable";
    }
);



    });

This is help you:

this.bluetoothSerial.isEnabled().then(this.success, this.failure);