Git Product home page Git Product logo

ng-cordova-bluetoothle's Introduction

ng-cordova-bluetoothle

This wraps the Bluetooth Low Energy Phonegap Plugin by Rand Dusing in AngularJS code so it can be used more easily in AngularJS mobile applications.

Using ngCordovaBluetoothLE In Your Project

The core Apache Cordova plugin is required to use this wrapper:

cordova plugin add https://github.com/randdusing/BluetoothLE

Example Usage

Scanning all BLE devices in range

var params = {
  request: true,
  time : 10000 //in milliseconds, time to scan defaults to 5000ms if not provided
};
$cordovaBluetoothle.startScan(params).then(function(success) {
	console.log(device.name);
	console.log(device.address);
});

The Scan method will stop as soon as it finds its first device. This is probably not what you want; and is a limitation of wrapping a continuous operation (like scan) into a one-time promise resolution. This is definitely open for improvement. I use 'find' instead to find a specific device.

Scanning for Specific Device using .find()

var params = {
  request: true, //ask user to enable bluetooth
  name: 'Jewelbot' //device name to look for
};


$ionicPlatform.ready()
        .then(function () {
          return $cordovaBluetoothle.initialize(params)
            .then(function () {
              //successfully initialized
              return $cordovaBluetoothle.find(params);
            }, function (err) {
              //error trying to initialiez
            });
        })
        .then(function (data) {
          // Scanning for specific device
          if (data.status === 'scanResult') {
            $scope.model.status = 'Found device: ' + data.name;
            $scope.model.devices.push(data);
            return $cordovaBluetoothle.stopScan();
          }
        }, function (error) {
          $scope.model.status = 'Error while scanning.' + JSON.stringify(error);
          return $cordovaBluetoothle.stopScan();
        }, function (notify) {
          // found other devices; but not the device you're looking
        })
        .then(function () {
          $scope.model.status = 'ending scan...';
          return $cordovaBluetoothle.isScanning().then(function(isScanning) {
            $scope.model.status = isScanning ? 'Scan Not Ended' : 'Scan Ended';
            if (isScanning) {
              return $cordovaBluetoothle.stopScan();
            }
          });
        });

Pairing (assumes address found during scan)

.controller("ExampleController", ['$cordovaBluetoothle', '$ionicPlatform', '$scope', function($cordovaBluetoothle,$ionicPlatform, $scope ) {
    
    $ionicPlatform.ready()
          .then(function () {
            return $cordovaBluetoothle.connect({address: $scope.model.address})
              .then( function (success) {
                //successfully paired
              })
              .error(function (err) {
                //error while pairing, return promise that invokes disconnect.
                return $cordovaBluetoothle.disconnect(address);
              })
              .notify(function (notify) {
                //still trying to connect. 
              });
          });

 

Available Functions

Here are a list of available functions:

$cordovaBluetoothle.initalize() //ios
$cordovaBluetoothle.enable() //android
$cordovaBluetoothle.disable() //android
$cordovaBluetoothle.startScan()
$cordovaBluetoothle.stopScan()
$cordovaBluetoothle.retrieveConnected()
$cordovaBluetoothle.connect()
$cordovaBluetoothle.disconnect()
$cordovaBluetoothle.reconnect()
$cordovaBluetoothle.close() // android
$cordovaBluetoothle.discover() //android
$cordovaBluetoothle.characteristics() 
$cordovaBluetoothle.descriptors() 
$cordovaBluetoothle.read() 
$cordovaBluetoothle.subscribe() 
$cordovaBluetoothle.unsubscribe() 
$cordovaBluetoothle.write()

$cordovaBluetoothle.readDescriptor()
$cordovaBluetoothle.writeDescriptor()
$cordovaBluetoothle.rssi()
$cordovaBluetoothle.isInitialized()
$cordovaBluetoothle.isEnabled()
$cordovaBluetoothle.isScanning()
$cordovaBluetoothle.isConnected()
$cordovaBluetoothle.isDiscovered()
$cordovaBluetoothle.find()

Have a question or found a bug?

Open an issue.

Resources

AngularJS - http://www.angularjs.org

Apache Cordova - http://cordova.apache.org

License and Source

The code that powers the 'build generation' is a carbon copy (with minimal changes) of Nic Raboy's ngCordovaBeacon project. As of the Fork, it was licensed under the MIT license; The Cordova Wrapper was written by myself, and my employer and I retain the copyright for that; but this project is also licensed under the MIT License.

If you want to follow more of Nic's work, his blog is located here. If you want to know more about me or Jewelbots, you can follow Jewelbots on twitter, our Ink, or me.

Contributing

ng-cordova-bluetoothle's People

Contributors

gortok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ng-cordova-bluetoothle's Issues

Exclude node_modules

I believe you should drop node_modules from your repository and add it to .gitignore.

As it stands, your repository cannot be checked out on Windows due to the node_modules file path being too long. This is fixed in npm 3+ and would be fixed if we resolved node_modules ourselves.

This also breaks bower install on Windows.

Connect() has a inconsistent behaviour

In my application I am using the plugin for connecting to a bluetooth device to read data. I see that the behaviour of connect() function is not consistent. Sometimes when I try to connect I get a connect error. There is no much information as to why the error occurred. After the error I call the close() function and the close is successful. My question is if the connect() fails then how is the close() successful. And what could be causing this inconsistent behaviour?

Getting value info after reading

Hi, i have read a characteristic value from a service and everything suppose to be ok but that value is not readable at all. The value returned is like: AAAAAAAAA== (i think that is base64)
After using the encodedStringToBytes and bytesToString methods, the result is "" with no value.
Any help?
Thanks a lot,

IOS not working.

Hi,

I am currently working on a project with Intel XDK 2807. We have installed the plugin and wrote all the code for the project in android and it works perfectly.

We have now the need to have it IOS and when we try to run the code, all the functions are returning a unsupported function error.

I am sure this is a small thing that we cannot figure out.

Can you please help us on this.

Thanks

Scan not reteriving devices

I have successfully implemented this plugin but am facing some issues, when i call startScan using the following code:

$cordovaBluetoothLE.startScan({serviceUuids:[], allowDuplicates: false}).then
        (null,
          function(obj) 
          {
            //Handle errors
            console.log("Start Scan Error : " + JSON.stringify(obj));
            var alertPopup = $ionicPopup.alert(
            {
              title: 'Start Scan',
              template: "Start Scan Error : " + JSON.stringify(obj),
              okText: 'OK'
            });
          },
          function(obj) 
          {
            //Handle successes
            if (obj.status == "scanResult")
            {
              console.log("Scan Result");
              var alertPopup = $ionicPopup.alert(
              {
                title: 'Start Scan',
                template: "Scan Result" + "\n" + obj.address,
                okText: 'OK'
              });

              addDevice(obj);

            }
            else if (obj.status == "scanStarted")
            {
              console.log("Scan Started");
              var alertPopup = $ionicPopup.alert(
              {
                title: 'Start Scan',
                template: "Scan Started",
                okText: 'OK'
              });
            }
            else
            {
              console.log("Unexpected Start Scan Status");
              var alertPopup = $ionicPopup.alert(
              {
                title: 'Start Scan',
                template: "Unexpected Start Scan Status",
                okText: 'OK'
              });
            }
          }
        );

it does not get into the condition

if (obj.status == "scanResult") 

so it discover available devices

i didn't know when it get into it, so if you could help me about the this issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.