Class BleRemoteCharacteristic

Hierarchy

Constructors

Properties

onread?: ((data) => void)

Type declaration

    • (data): void
    • Callback function when read value.

      Parameters

      • data: any

      Returns void

onregisternotify?: (() => void)

Type declaration

    • (): void
    • Callback function will be called when [[registerNotify]] finished.

      This doesn't call when notify arrived. It call when 'reiterate' finished.

      Returns void

onunregisternotify?: (() => void)

Type declaration

    • (): void
    • Callback function will be called when [[unregisterNotify]] finished.

      Returns void

onwrite?: ((result) => void)

Type declaration

    • (result): void
    • Callback function when write value finished.

      Parameters

      • result: any

      Returns void

properties: BleAttributePropery[]

It is an array of properties of a characteristics. It contains some of belows.

console.log(characteristics.properties); // => ['read', 'write', 'notify']

See how works at https://www.bluetooth.com/ja-jp/specifications/bluetooth-core-specification/

Service instance

uuid: string

It is uuid as string.

console.log(attr.uuid); // => '4C84'

Accessors

  • get descriptors(): BleRemoteDescriptor[]
  • It contains descriptors in a characteristic. It was discovered when connection automatically.

    // Javascript Example

    await obniz.ble.initWait();
    var target = {
    uuids: ["fff0"],
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    if(!peripheral) {
    console.log('no such peripheral')
    return;
    }
    try {
    await peripheral.connectWait();
    console.log("connected");
    var service = peripheral.getService("1800")
    var c = service.getCharacteristic("fff0")
    for (var d of c.descriptors) {
    console.log(d.uuid)
    }
    } catch(e) {
    console.error(e);
    }

    Returns BleRemoteDescriptor[]

Methods

  • Discover services.

    If connect setting param 'autoDiscovery' is true(default), services are automatically discover on connection established.

    // Javascript Example
    await obniz.ble.initWait({});
    obniz.ble.scan.onfind = function(peripheral){
    if(peripheral.localName == "my peripheral"){
    peripheral.onconnect = async function(){
    console.log("success");
    await peripheral.discoverAllServicesWait(); //manually discover
    let service = peripheral.getService("1800");
    await service.discoverAllCharacteristicsWait(); //manually discover
    let characteristics = service.getCharacteristic("ff00");
    await characteristics.discoverAllDescriptorsWait(); //manually discover
    let descriptor = characteristics.getDescriptor("fff1");
    }
    peripheral.connect({autoDiscovery:false});
    }
    }
    await obniz.ble.scan.startWait();

    Returns Promise<BleRemoteDescriptor[]>

  • It returns a descriptors which having specified uuid in a characteristic. Return value is null when not matched.

    Case is ignored. So aa00 and AA00 are the same.

    // Javascript Example

    await obniz.ble.initWait();
    var target = {
    uuids: ["fff0"],
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    if(!peripheral) {
    console.log('no such peripheral')
    return;
    }
    try {
    await peripheral.connectWait();
    console.log("connected");
    var service = peripheral.getService("1800")
    var c = service.getCharacteristic("fff0")
    var d = c.getDescriptor("fff0")
    console.log(d.uuid)
    } catch(e) {
    console.error(e);
    }

    Parameters

    • uuid: string

    Returns null | BleRemoteDescriptor

  • Wait for notification and return data when it arrives.


    await obniz.ble.initWait();
    var target = {
    localName: "obniz-notify"
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    await peripheral.connectWait();
    let char = peripheral.getService('fff0').getCharacteristic('fff1');

    let data = await c.getNotifyWait();
    console.log("notify with data " + data.join(','));

    Returns Promise<any>

    data from notification of the device

  • It reads data from the characteristic.

    Even you wrote string or number, it returns binary array. The returned value appears in the callback function (onread). If reading succeeds an Array with data will be returned. It throws an error when failed.

    // Javascript Example
    await obniz.ble.initWait();
    var target = {
    uuids: ["fff0"],
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    if(peripheral){
    await peripheral.connectWait();
    console.log("connected");
    await obniz.wait(1000);

    var dataArray = await peripheral.getService("FF00").getCharacteristic("FF01").readWait();
    console.log(dataArray);
    }

    Returns Promise<number[]>

  • This sets a callback function to receive notify when it comes from periperal. To receive notify, you need to register on CCCD Descriptor(0x2902).

    More information of BLE/CCCD is available at bluetooth.com.

    await obniz.ble.initWait();
    var target = {
    localName: "obniz-notify"
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    await peripheral.connectWait();
    let char = peripheral.getService('fff0').getCharacteristic( 'fff1');

    char.onregisternotify = function() {
    console.log("register finshed")
    }

    char.registerNotify( function(data){
    console.log("notify with data " + data.join(','));
    });

    Parameters

    • callback: ((data) => void)
        • (data): void
        • Parameters

          • data: any

          Returns void

    Returns void

    Deprecated

    replaced by #registerNotifyWait()

  • This sets a notify callback function and wait to finish register.


    await obniz.ble.initWait();
    var target = {
    localName: "obniz-notify"
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    await peripheral.connectWait();
    let char = peripheral.getService('fff0').getCharacteristic( 'fff1');

    await char.registerNotifyWait( function(data){
    console.log("notify with data " + data.join(','));
    });

    Parameters

    • callback: ((data) => void)
        • (data): void
        • Parameters

          • data: any

          Returns void

    Returns Promise<void>

  • unregistrate a callback which is registrated by [[registerNotify]] or [[registerNotifyWait]].


    await obniz.ble.initWait();
    var target = {
    localName: "obniz-notify"
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    await peripheral.connectWait();
    let char = peripheral.getService('fff0').getCharacteristic( 'fff1');

    char.onregisternotify = function() {
    console.log("register finished")
    char.unregisterNotify();
    }

    char.onunregisternotify = function() {
    console.log("unregistrated")
    }

    char.registerNotify( function(data){
    console.log("notify with data " + data.join(','));
    });

    Returns void

    Deprecated

    replaced by #unregisterNotifyWait()

  • Unregistrate a callback which is registrated by [[registerNotify]] or [[registerNotifyWait]]. And wait until done.



    await obniz.ble.initWait();
    var target = {
    localName: "obniz-notify"
    };

    var peripheral = await obniz.ble.scan.startOneWait(target);
    await peripheral.connectWait();
    let char = peripheral.getService('fff0').getCharacteristic('fff1');

    await char.registerNotifyWait(function(data){
    console.log("notify with data " + data.join(','));
    });
    await char.unregisterNotifyWait();
    console.log("unregistrated")

    Returns Promise<void>

  • This writes dataArray to the characteristic. It throws an error when failed.

    // Javascript Example

    await obniz.ble.initWait();
    var target = {
    uuids: ["fff0"],
    };
    var peripheral = await obniz.ble.scan.startOneWait(target);
    if(peripheral){
    await peripheral.connectWait();
    console.log("connected");
    await obniz.wait(1000);

    var dataArray = [0x02, 0xFF];
    await peripheral.getService("FF00").getCharacteristic("FF01").writeWait(dataArray);
    console.log("write success");
    }

    Parameters

    • data: any
    • Optional needResponse: any

    Returns Promise<boolean>

Generated using TypeDoc