Code Monkey home page Code Monkey logo

hansemannn.ti.bluetooth-1's Introduction

Android Titanum BLE Module

Work in progress

iOS version

For an iOS version have a look at Hans Knoechel (@hansemannnn / Web) repository at https://github.com/hansemannn/ti.bluetooth

Methods

  • initialize()
  • isScannig()
  • getState()
  • startScan()
  • stopScan()

Events

  • didDiscoverPeripheral
  • didUpdateState

TODO

  • Method: scanForPeripheralsWithServices()
  • Event: didDiscoverServices
  • Event: didDiscoverCharacteristicsForService
  • Event: didUpdateValueForCharacteristic

Example

var BLE = require('ti.bluetooth');

// Initialize the BLE Central Manager
BLE.initialize();

var win = Ti.UI.createWindow({
    backgroundColor: '#fff'
});

var btn1 = Ti.UI.createButton({
    title: 'Start scan',
    top: 40
});

btn1.addEventListener('click', function() {
    if (BLE.isScanning()) {
        alert('Already scanning, please stop scan first!');
        return;
    } else if (BLE.getState() != BLE.MANAGER_STATE_POWERED_ON) {
        alert('The BLE manager needs to be powered on before. Call initialize().');
        return;
    }

    //BLE.scanForPeripheralsWithServices(['384DF4C0-8BAE-419D-9A65-2D67942C2DB7']);
    BLE.startScan();
});

var btn2 = Ti.UI.createButton({
    title: 'Stop scan',
    top: 100
});

btn2.addEventListener('click', function() {
    if (!BLE.isScanning()) {
        alert('Not scanning!');
        return;
    }
    BLE.stopScan();
});

// BLE.addEventListener('didConnectPeripheral', function(e) {
//     Ti.API.info('didConnectPeripheral');
//     Ti.API.info(e);
// });

BLE.addEventListener('didDiscoverPeripheral', function(e) {
    Ti.API.info('didDiscoverPeripheral');
    Ti.API.info(JSON.stringify(e));
});

BLE.addEventListener('didUpdateState', function(e) {
    Ti.API.info('didUpdateState');

    switch (e.state) {
        case BLE.MANAGER_STATE_RESETTING:
            Ti.API.info('Resetting');
            break;

        case BLE.MANAGER_STATE_UNSUPPORTED:
            Ti.API.info('Unsupported');
            break;

        case BLE.MANAGER_STATE_UNAUTHORIZED:
            Ti.API.info('Unauthorized');
            break;

        case BLE.MANAGER_STATE_POWERED_OFF:
            Ti.API.info('Powered Off');
            break;

        case BLE.MANAGER_STATE_POWERED_ON:
            Ti.API.info('Powered On');
            break;

        case BLE.MANAGER_STATE_UNKNOWN:
        default:
            Ti.API.info('Unknown');
            break;
    }
});

// BLE.addEventListener('didDiscoverServices', function(e) {
//     Ti.API.info('didDiscoverServices');
//     Ti.API.info(e);
// });
// 
// BLE.addEventListener('didDiscoverCharacteristicsForService', function(e) {
//     Ti.API.info('didDiscoverCharacteristicsForService');
//     Ti.API.info(e);
// });
// 
// BLE.addEventListener('didUpdateValueForCharacteristic', function(e) {
//     Ti.API.info('didUpdateValueForCharacteristic');
//     Ti.API.info(e);
// });

win.add(btn1);
win.add(btn2);
win.open();

hansemannn.ti.bluetooth-1's People

Contributors

m1ga avatar

Watchers

James Cloos avatar Brenton House avatar

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.