Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • BleRemotePeripheral

Index

Constructors

constructor

Properties

address

BLE address

address_type

address_type: BleDeviceAddressType | null

adv_data

adv_data: number[]

This returns raw advertise data.


// Javascript Example
 await obniz.ble.initWait();
 var target = {
  uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.adv_data)

advertise_data_rows

advertise_data_rows: any

Raw data of advertisement

ble_event_type

ble_event_type: BleEventType | null

connected

connected: boolean

This returns connection state as boolean.

// Javascript Example
await obniz.ble.initWait();
var target = {
    uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.connected) // => false

device_type

device_type: BleDeviceType | null

iBeacon

iBeacon: IBeacon | null

This returns iBeacon data if the peripheral has it. If none, it will return null.

// Javascript Example
await obniz.ble.initWait();
var target = {
 uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.iBeacon)

localName

localName: string | null

This returns local name if the peripheral has it.

// Javascript Example

await obniz.ble.initWait();
var target = {
 uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.localName)

Optional onconnect

onconnect: undefined | (() => void)

This function is called when connection succeeds.

// Javascript Example
await obniz.ble.initWait();
obniz.ble.scan.onfind = function(peripheral){
  if(peripheral.localName == "my peripheral"){
    peripheral.onconnect = function(){
      console.log("success");
    }
     await peripheral.connectWait();
   }
}
await obniz.ble.scan.startWait();

Optional ondisconnect

ondisconnect: undefined | ((reason?: any) => void)

This function is called when a connected peripheral is disconnected or first connection establish was failed.

// Javascript Example
 await obniz.ble.initWait();
 obniz.ble.scan.onfind = function(peripheral){
  if(peripheral.localName == "my peripheral"){
      peripheral.onconnect = function(){
          console.log("success");
      }
      peripheral.ondisconnect = function(reason){
          console.log("closed", reason);
      }
      peripheral.connect();
  }
}
await obniz.ble.scan.startWait();

Optional onerror

onerror: undefined | ((err: any) => void)

This gets called with an error message when some kind of error occurs.

rssi

rssi: number | null

This returns RSSI(dbm) as number.

// Javascript Example

await obniz.ble.initWait();
obniz.ble.scan.onfind = async (peripheral) => {
 console.log(peripheral.localName, peripheral.rssi); // null, -80
};

await obniz.ble.scan.startWait();

scan_resp

scan_resp: number[] | null

This returns raw scan response data.


// Javascript Example
 await obniz.ble.initWait();
 var target = {
  uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.adv_data)
console.log(peripheral.scan_resp)

Accessors

services

  • It contains all discovered services in a peripheral as an array. It is 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");
      for (var service of peripheral.services) {
          console.log(service.uuid)
      }
    } catch(e) {
      console.error(e);
    }

    Returns BleRemoteService[]

Methods

connect

  • deprecated

    As of release 3.5.0, replaced by {@link #connectWait()}

    Parameters

    Returns void

connectWait

  • This connects obniz to the peripheral. If ble scanning is undergoing, scan will be terminated immidiately.

    It throws when connection establish failed.

    when connection established, all service/characteristics/desriptors will be discovered automatically. This function will wait until all discovery done.

    About Failures Connection fails some reasons. You can find reason from thrown error. Also obniz provide 90 seconds timeout for connection establish.

    // 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");
    } catch(e) {
      console.log("can't connect");
    }

    Parameters

    Returns Promise<void>

disconnect

  • disconnect(): void
  • deprecated

    Returns void

disconnectWait

  • disconnectWait(): Promise<void>
  • This disconnects obniz from peripheral.

    It throws when failed

    // 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");
      await peripheral.disconnectWait();
      console.log("disconnected");
    } catch(e) {
       console.log("can't connect / can't disconnect");
    }

    Returns Promise<void>

discoverAllServicesWait

  • 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");
         }
         peripheral.connectWait({autoDiscovery:false});
        }
    }
    await obniz.ble.scan.startWait();

    Returns Promise<BleRemoteService[]>

getService

  • It returns a service which having specified uuid in services. 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")
      if (!service) {
        console.log("service not found")
        return;
      }
      console.log(service.uuid)
    } catch(e) {
      console.error(e);
    }

    Parameters

    Returns BleRemoteService | null

pairingWait

  • Start pairing. This function return keys which you can use next time pairing with same device.

    // Javascript Example
    await obniz.ble.initWait({});
    obniz.ble.scan.onfind = function(peripheral){
    if(peripheral.localName == "my peripheral"){
         peripheral.onconnect = async function(){
             console.log("success");
             const keys = await peripheral.pairingWait();
    
             // Please store `keys` if you want to bond.
         }
         await peripheral.connectWait();
        }
    }
    await obniz.ble.scan.startWait();

    If you have already keys, please use options.keys

    // Javascript Example
    
    const keys = "xxxxx";
    await obniz.ble.initWait({});
    obniz.ble.scan.onfind = function(peripheral){
    if(peripheral.localName == "my peripheral"){
         peripheral.onconnect = async function(){
             console.log("success");
             await peripheral.pairingWait({keys});  // pairing with stored keys.
    
         }
         await peripheral.connectWait();
        }
    }
    await obniz.ble.scan.startWait();

    Go to BlePairingOptions to see more option.

    Parameters

    Returns Promise<string>

setPairingOption

  • Parameters

    Returns void

Generated using TypeDoc