Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70612

Trouble using BluetoothSerial in Ionic

$
0
0

@smhharan wrote:

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.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>