Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BleRemoteCharacteristic

Hierarchy

Index

Constructors

constructor

Properties

Optional onread

onread: undefined | ((data: any) => void)

Callback function when read value.

Optional onregisternotify

onregisternotify: undefined | (() => void)

Callback function will be called when registerNotify finished.

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

Optional onunregisternotify

onunregisternotify: undefined | (() => void)

Callback function will be called when unregisterNotify finished.

Optional onwrite

onwrite: undefined | ((result: any) => void)

Callback function when write value finished.

properties

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

Service instance

uuid

uuid: UUID

It is uuid as string.

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

Accessors

descriptors

  • 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

canBroadcast

  • canBroadcast(): boolean
  • This characteristics can broadcast or not.

    Returns boolean

canIndicate

  • canIndicate(): boolean
  • This characteristics can indicate or not.

    Returns boolean

canNotify

  • canNotify(): boolean
  • This characteristics can notify or not.

    Returns boolean

canRead

  • canRead(): boolean
  • This characteristics can read or not.

    Returns boolean

canWrite

  • canWrite(): boolean
  • This characteristics can write or not.

    Returns boolean

canWriteWithoutResponse

  • canWriteWithoutResponse(): boolean
  • This characteristics can 'write without response' or not.

    Returns boolean

discoverAllDescriptorsWait

  • Discover services.

    If connect setting param 'autoDiscovery' is true(default), services are automatically disvocer 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[]>

getDescriptor

  • 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

    Returns BleRemoteDescriptor | null

read

  • read(): void
  • Use readWait() instead from 3.5.0

    deprecated

    Returns void

readNumberWait

  • readNumberWait(): Promise<number | null>
  • Wrapper for writeWait with data converting from number. It writes data as 1byte.

    It throws an error when failed.

    Returns Promise<number | null>

readTextWait

  • readTextWait(): Promise<string | null>
  • Wrapper for readWait with data converting to text. It convert UTF-8 and write binary array to string.

    It throws an error when failed.

    Returns Promise<string | null>

readWait

  • readWait(): Promise<number[]>
  • 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[]>

registerNotify

  • registerNotify(callback: (data: any) => void): void
  • 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 infomation 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: any) => void
        • (data: any): void
        • Parameters

          • data: any

          Returns void

    Returns void

registerNotifyWait

  • registerNotifyWait(callback: (data: any) => void): Promise<void>
  • 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: any) => void
        • (data: any): void
        • Parameters

          • data: any

          Returns void

    Returns Promise<void>

unregisterNotify

  • unregisterNotify(): 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 finshed")
     char.unregisterNotify();
    }
    
    char.onunregisternotify = function() {
      console.log("unregistrated")
    }
    
    char.registerNotify( function(data){
      console.log("notify with data " + data.join(','));
    });
    

    Returns void

unregisterNotifyWait

  • unregisterNotifyWait(): Promise<void>
  • 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>

write

  • write(array: number[], needResponse?: undefined | false | true): void
  • Use writeWait() instead from 3.5.0

    deprecated

    Parameters

    • array: number[]
    • Optional needResponse: undefined | false | true

    Returns void

writeNumberWait

  • writeNumberWait(val: number, needResponse?: undefined | false | true): Promise<boolean>
  • Wrapper for writeWait with data converting from number. It writes data as 1byte.

    It throws an error when failed.

    Parameters

    • val: number
    • Optional needResponse: undefined | false | true

    Returns Promise<boolean>

writeTextWait

  • writeTextWait(str: string, needResponse?: undefined | false | true): Promise<boolean>
  • Wrapper for writeWait with data converting from text. It convert string to UTF-8 and write binary array.

    It throws an error when failed.

    Parameters

    • str: string
    • Optional needResponse: undefined | false | true

    Returns Promise<boolean>

writeWait

  • writeWait(data: any, needResponse?: any): Promise<boolean>
  • 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