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: number[][] | null

Raw data of advertisement

deprecated

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

connected_at

connected_at: Date | null

This returns connection completion time with a connected state.

If not connected, returns null.

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)

manufacturerSpecificData

manufacturerSpecificData: number[] | null

manufacturerSpecificDataInScanResponse

manufacturerSpecificDataInScanResponse: number[] | null

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: Error) => void)

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

primary_phy

primary_phy: HciPhy | null

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)

secondary_phy

secondary_phy: HciPhy | null

serviceData

serviceData: number[] | null

Ad Type: 0x16 (16bit UUID)

service_data

service_data: { data: number[]; uuid: string }[] | null

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");
    }

    There are options for connection

    // 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

    replaced by {@link #disconnectWait()}

    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[]>

getPairingKeysWait

  • getPairingKeysWait(): Promise<string | null>
  • Returns Promise<string | null>

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

isPairingFinishedWait

  • isPairingFinishedWait(): Promise<boolean>
  • Returns Promise<boolean>

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>

readPhyWait

  • readPhyWait(): Promise<undefined | { rxPhy: string; txPhy: string }>
  • Check the PHY used in the connection

    // 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");
      const phy = await peripheral.readPhyWait()
      console.log(phy)
    } catch(e) {
      console.error(e);
    }

    Returns Promise<undefined | { rxPhy: string; txPhy: string }>

setPairingOption

  • Parameters

    Returns void

setPhyWait

  • setPhyWait(usePhy1m: boolean, usePhy2m: boolean, usePhyCoded: boolean, useCodedModeS8: boolean, useCodedModeS2: boolean): Promise<void>
  • Check the PHY used in the connection. Request to change the current PHY

    It will be changed if it corresponds to the PHY set by the other party.

    Changes can be seen on onUpdatePhy

    // Javascript Example
    
    await obniz.ble.initWait();
    obniz.ble.onUpdatePhy = ((txPhy, rxPhy) => {
     console.log("txPhy "+txPhy+" rxPhy "+rxPhy);
    });
    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.setPhyWait(false,false,true,true,true);//Request Only PHY Coded
    } catch(e) {
      console.error(e);
    }

    Parameters

    • usePhy1m: boolean
    • usePhy2m: boolean
    • usePhyCoded: boolean
    • useCodedModeS8: boolean
    • useCodedModeS2: boolean

    Returns Promise<void>

Generated using TypeDoc