Hierarchy

  • BleRemotePeripheral

Constructors

Properties

address: string

BLE address

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

Raw data of advertisement

Deprecated

ble_event_type: null | BleEventType
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: null | Date

This returns connection completion time with a connected state.

If not connected, returns null.

device_type: null | BleDeviceType
iBeacon: null | IBeacon

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: null | string

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: null | number[]
manufacturerSpecificDataInScanResponse: null | number[]
onconnect?: (() => void)

Type declaration

    • (): 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();

      Returns void

ondisconnect?: ((reason?) => void)

Type declaration

    • (reason?): 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();

      Parameters

      • Optional reason: any

      Returns void

onerror?: ((err) => void)

Type declaration

    • (err): void
    • This gets called with an error message when some kind of error occurs.

      Parameters

      • err: Error

      Returns void

primary_phy: null | HciPhy
rssi: null | number

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

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

Ad Type: 0x16 (16bit UUID)

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

Accessors

  • get services(): BleRemoteService[]
  • 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

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

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

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

  • 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

    • uuid: string

    Returns null | BleRemoteService

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

  • 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;
    }>

  • 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