$cordovaBLE scan result array blank?

I know the $cordovaBLE plugin isn’t fully documented, but I am experimenting with it and have been able to return an array of devices in the view. The devices show up in the xCode console with full properties when building/running the app so I know the scan is working, and I can ng-repeat the correct number of devices in the view, but I can’t pass the device properties. Can anyone offer any help? I am new to Ionic and AngularJS, so maybe I’ve missed something easy, but I’ve tried a ton of combinations with out luck.

This is my Controller.

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

$ionicPlatform.ready(function(){

var scan = $cordovaBLE.scan([],5);
scan.then(
  null,
  function(err) {
    // error
  },
  function(result) {
    $scope.devices = result;
});

}); // end $ionicPlatform.ready

}) // end bleCtrl

And this is my Template.

<ion-view view-title="ngCordova Development App">
<ion-content>
<button class="button button-stable button-block" ng-click="scanDevices()">Refresh List</button>
<ion-list>
    <ion-item ng-repeat="device in devices" type="item-text-wrap" href="#/devices/{{device.name}}">
      <h2>Name: {{device.name}}</h2>
      <p>ID: {{device.id}}</p>
      <p>RSSI: {{device.rssi}}</p>
    </ion-item>
</ion-list>
</ion-content>
</ion-view>
1 Like

Hey! Welcome to the world of BLE, lol. It’s definitely not fun to program.

Do the name, id and RSSI show up fine? Or are you saying that the actual properties of the BLE device are not showing up (like temperature, etc)?

Thanks,
Matt

Yeah, especially not fun for a beginner!

The names, id, and rssi weren’t showing up but I fixed it. I realized this morning that with the code above I was just ng-repeating through the first returned device. I just had to push all of the scan results to a second array.

This worked…

devices.push(result);
$scope.devices = devices;

Next step… connecting to a device.

I’m maintaining a repo of this project here if anyone is interested. I have a simple app based on the tab starter that hooks into ngCordova plugins for Device Motion, Device Orientation, Geolocation, and soon to be BLE.

1 Like

Awesome, glad you got it to work! :thumbsup:

So I’ve made it quite a bit farther, but I am stuck on the last bit… notifying values. Would you be able to give me a little help?

This is my service code…

////// START READ NOTIFICATION

this.notifyBLE = function(deviceId, data) {

serviceUUID = data.service;
characteristicUUID = data.characteristic;

$ionicPlatform.ready(function(){

  var notify = $cordovaBLE.startNotification(deviceId, serviceUUID, characteristicUUID);
  notify.then(
    function(result) {
      value = new Uint8Array(result);

    },
    function(error) {

  });

}); // end $ionicPlatform.ready

return value[0];

} // end notify function

And this is my controller code…

.controller('BleConnectCtrl', function($scope, $stateParams, $timeout, $ionicPlatform, $cordovaBLE, DeviceService) {
  $scope.notifyMe = function(data) {    
    $scope.dataValue = DeviceService.notifyBLE($stateParams.deviceId, data);
    $scope.deviceIsSelecting = true;
    $scope.deviceIsReading = true;
  }

The “notifyMe” function is hooked to a button click. When I click, I get one value returned to the view, but the value doesn’t update asynchronously even though the data from the device is incrementing. If I click the button again, the value updates in the view but stops.

Any thoughts on why I can’t get the notify function in $cordovaBLE updating asynchronously?

Jon

I tried implementing a $q service to solve the asynchronous issue, but still haven’t had any luck.

New service…

////// START READ NOTIFICATION

this.notifyBLE = function(deviceId, data) {

serviceUUID = data.service;
characteristicUUID = data.characteristic;

var deferred = $q.defer();

$ionicPlatform.ready(function(){

  var notify = $cordovaBLE.startNotification(deviceId, serviceUUID, characteristicUUID);
  notify.then(
    function(result) {
      var value = new Uint8Array(result);
      deferred.resolve(value);
    },
    function(error) {

  });

}); // end $ionicPlatform.ready

return deferred.promise;

} // end notify function

And new controller…

$scope.notifyMe = function(data) {
  $scope.deviceIsSelecting = true;
  $scope.deviceIsReading = true;

  var getValue = DeviceService.notifyBLE($stateParams.deviceId, data);
  getValue.then(
    function(result) {
      $scope.dataValue = result;
    },
    function(error) {
  });
}

And I should note that “notifyMe” is called by a button through ng-click.

hi, i got the doubt that: why use
scan.then(
null,
function(err) {why error function is the second parameter while success function is third
// error
},
function(result) {
$scope.devices = result;
});

i dig into ng-cordova.js and plugin implementation and didnt find why scan(*).then(null,error_func,success_func)? anything i miss?

your scan code is working,yes, but why? i cannot find any documentation saying that!!

That’s a good question. That’s the only way the function worked for me.