Coder Social home page Coder Social logo

com.heytz.ble's Introduction

com.heytz.ble

黑子信息科技ble控制

###IOS

加入  BLUETOOTH_USAGE_DESCRIPTION 默认 " "

使用 cordova plugin add com.heytz.ble  --variable BLUETOOTH_USAGE_DESCRIPTION="you usage message" 

更新日志

17.07.10 

    1.取消Android需要配置AndroidManifest.xml application

API

Methods

scan

Scan and discover BLE peripherals.

HeytzBle.scan(services, seconds, success, failure);

Description

Function scan scans for BLE devices. The success callback is called each time a peripheral is discovered. Scanning automatically stops after the specified number of seconds.

ios { "name": "TI SensorTag", "id": "BD922605-1B07-4D55-8D09-B66653E51BBA", "rssi": -79, "advertising": /* ArrayBuffer or map */ } android { "name": "TI SensorTag", "id": "B6:66:53:E5:1B:BA" } Advertising information format varies depending on your platform. See Advertising Data for more information.

Parameters

  • services: List of services to discover, or [] to find all devices
  • seconds: Number of seconds to run discovery
  • success: Success callback function that is invoked which each discovered device.
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

HeytzBle.scan([], 5, function(device) {
    console.log(JSON.stringify(device));
}, failure);

startScan

Scan and discover BLE peripherals.

HeytzBle.startScan(services, success, failure);

Description

Function startScan scans for BLE devices. The success callback is called each time a peripheral is discovered. Scanning will continue until stopScan is called. ios { "name": "TI SensorTag", "id": "BD922605-1B07-4D55-8D09-B66653E51BBA", "rssi": -79, "advertising": /* ArrayBuffer or map */ } android { "name": "TI SensorTag", "id": "BD922605-1B07-4D55-8D09-B66653E51BBA" } Advertising information format varies depending on your platform. See Advertising Data for more information.

Parameters

  • services: List of services to discover, or [] to find all devices
  • success: Success callback function that is invoked which each discovered device.
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

HeytzBle.startScan([], function(device) {
    console.log(JSON.stringify(device));
}, failure);

setTimeout(HeytzBle.stopScan,
    5000,
    function() { console.log("Scan complete"); },
    function() { console.log("stopScan failed"); }
);

stopScan

Stop scanning for BLE peripherals.

HeytzBle.stopScan(success, failure);

Description

Function stopScan stops scanning for BLE devices.

Parameters

  • success: Success callback function, invoked when scanning is stopped. [optional]
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

HeytzBle.startScan([], function(device) {
    console.log(JSON.stringify(device));
}, failure);

setTimeout(HeytzBle.stopScan,
    5000,
    function() { console.log("Scan complete"); },
    function() { console.log("stopScan failed"); }
);

/* Alternate syntax
setTimeout(function() {
    HeytzBle.stopScan(
        function() { console.log("Scan complete"); },
        function() { console.log("stopScan failed"); }
    );
}, 5000);
*/

connect

Connect to a peripheral.

HeytzBle.connect(device_id, connectSuccess, connectFailure);

Description

Function connect connects to a BLE peripheral. The callback is long running. Success will be called when the connection is successful. Service and characteristic info will be passed to the success callback in the peripheral object. Failure is called if the connection fails, or later if the peripheral disconnects. An peripheral object is passed to the failure callback.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • connectSuccess: Success callback function that is invoked when the connection is successful.
  • connectFailure: Error callback function, invoked when error occurs or the connection disconnects.

disconnect

Disconnect.

HeytzBle.disconnect(device_id, [success], [failure]);

Description

Function disconnect disconnects the selected device.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • success: Success callback function that is invoked when the connection is successful. [optional]
  • failure: Error callback function, invoked when error occurs. [optional]

write

Writes data to a characteristic.

HeytzBle.write(device_id, service_uuid, characteristic_uuid, value, success, failure);

Description

Function write writes data to a characteristic.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • service_uuid: UUID of the BLE service
  • characteristic_uuid: UUID of the BLE characteristic
  • data: binary data, use an ArrayBuffer
  • success: Success callback function that is invoked when the connection is successful. [optional]
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

Use an ArrayBuffer when writing data.

// send 1 byte to switch a light on
var data = new Uint8Array(1);
data[0] = 1;
HeytzBle.write(device_id, "FF10", "FF11", data.buffer, success, failure);

// send a 3 byte value with RGB color
var data = new Uint8Array(3);
data[0] = 0xFF;  // red
data[0] = 0x00; // green
data[0] = 0xFF; // blue
HeytzBle.write(device_id, "ccc0", "ccc1", data.buffer, success, failure);

// send a 32 bit integer
var data = new Uint32Array(1);
data[0] = counterInput.value;
HeytzBle.write(device_id, SERVICE, CHARACTERISTIC, data.buffer, success, failure);

// send a array
var data = [];
data[0] = 162;
HeytzBle.write(device_id, SERVICE, CHARACTERISTIC, data, success, failure);

startNotification

Register to be notified when the value of a characteristic changes.

HeytzBle.startNotification(device_id, service_uuid, characteristic_uuid, success, failure);

Description

Function startNotification registers a callback that is called every time the value of a characteristic changes. This method handles both notifications and indications. The success callback is called multiple times.

Raw data is passed from native code to the success callback as an ArrayBuffer.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • service_uuid: UUID of the BLE service
  • characteristic_uuid: UUID of the BLE characteristic
  • success: Success callback function invoked every time a notification occurs
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

var onData = function(buffer) {
    // Decode the ArrayBuffer into a typed Array based on the data you expect
    var data = new Uint8Array(buffer);
    alert("Button state changed to " + data[0]);
}

HeytzBle.startNotification(device_id, "FFE0", "FFE1", onData, failure);

stopNotification

Stop being notified when the value of a characteristic changes.

HeytzBle.stopNotification(device_id, service_uuid, characteristic_uuid, success, failure);

Description

Function stopNotification stops a previously registered notification callback.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • service_uuid: UUID of the BLE service
  • characteristic_uuid: UUID of the BLE characteristic
  • success: Success callback function that is invoked when the notification is removed. [optional]
  • failure: Error callback function, invoked when error occurs. [optional]

isConnected

Reports the connection status.

HeytzBle.isConnected(device_id, success, failure);

Description

Function isConnected calls the success callback when the peripheral is connected and the failure callback when not connected.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • success: Success callback function that is invoked with a boolean for connected status.
  • failure: Error callback function, invoked when error occurs. [optional]

Quick Example

HeytzBle.isConnected(
    'FFCA0B09-CB1D-4DC0-A1EF-31AFD3EDFB53',
    function() {
        console.log("Peripheral is connected");
    },
    function() {
        console.log("Peripheral is *not* connected");
    }
);

read

Reads the value of a characteristic.

ble.read(device_id, service_uuid, characteristic_uuid, success, failure);

Description

Function read reads the value of the characteristic.

Raw data is passed from native code to the callback as an ArrayBuffer.

Parameters

  • device_id: UUID or MAC address of the peripheral
  • service_uuid: UUID of the BLE service
  • characteristic_uuid: UUID of the BLE characteristic
  • success: Success callback function that is invoked when the connection is successful. [optional]
  • failure: Error callback function, invoked when error occurs. [optional]

showBluetoothSettings

Show the Bluetooth settings on the device.

HeytzBle.showBluetoothSettings(success, failure);

Description

Function showBluetoothSettings opens the Bluetooth settings for the operating systems.

enable

Enable Bluetooth on the device.

HeytzBle.enable(success, failure);

Description

Function enable prompts the user to enable Bluetooth.

Android

enable is only supported on Android and does not work on iOS.

If enable is called when Bluetooth is already enabled, the user will not prompted and the success callback will be invoked.

Parameters

  • success: Success callback function, invoked if the user enabled Bluetooth.
  • failure: Error callback function, invoked if the user does not enabled Bluetooth.

Quick Example

HeytzBle.enable(
    function() {
        console.log("Bluetooth is enabled");
    },
    function() {
        console.log("The user did *not* enable Bluetooth");
    }
);

Other Bluetooth Plugins

com.heytz.ble's People

Watchers

 avatar  avatar

Forkers

heytz

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.