Central: Pairing

Automatic pairing

Obniz.js includes a mechanism to automate pairing.

If an authentication error returns When reading or writing characterisics or descriptor, obniz.js will automatically try to pair it. It's automatic, so no configuration is required. Instead, it is not possible to enter passkey or bind (save key information).

The settings for automatic pairing are as follows

  • IO capability: NoInputNoOutput
  • OOB data: Authentication data not present
  • Authentication requirement: Bonding - No MITM
  • Max encryption key size: 16octet

For other settings (such as wanting to enter a passkey), please perform the manual pairing.

Retrieve LTK

Set callback onPairedCallbackto get Pairing Key(LTK) for automatics pairing.

await obniz.ble.initWait({});
obniz.ble.scan.onfind = function(peripheral){
  if(peripheral.localName == "my peripheral"){
    await peripheral.connectWait({
        pairingOption : {
          onPairedCallback : (keys) => {
            // LTK key
            console.log(keys);
          }
        }
    });
  }
}
await obniz.ble.scan.startWait();

Manual pairing

To perform manual pairing, use the peripheral.pairingWait function.
There is an optional argument, but it works even if you don't specify anything.


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

// Javascript Example

The pairingWait function returns a string. If you want to use the key information of the first pairing for the second and subsequent pairings, specify it as the argument of the pairingWait function


const keys =  "xxxxxx"; 
await obniz.ble.initWait({});
obniz.ble.scan.onfind = async function(peripheral){
  if(peripheral.localName == "my peripheral"){
      await peripheral.connectWait();
      console.log("success");
      await peripheral.pairingWait({keys: keys});
      console.log("paired");
   }
}
await obniz.ble.scan.startWait();

// Javascript Example

Pairing with passkey numbers

In the case of pairing by entering a number, specify passkeyCallback as the argument of pairingWait, the callback function will be called when passkey is needed, so please accept the input from the user and return it

  await obniz.ble.initWait({});
  obniz.ble.scan.onfind = async function(peripheral) {
    console.log(peripheral.localName);
    if (peripheral.localName === "my peripheral") {
      await peripheral.connectWait();
      console.log("success");
      let passkeyCallback = async () => {
        //HTML prompt
        const pass = 016393;
        return pass;
      };
      // pairing with user input passkey.
      await peripheral.pairingWait({ passkeyCallback });
      console.log("paired");
    }
  };
  await obniz.ble.scan.startWait();