Central: Connection

There are two methods to connect to BLE peripheral.

  • Connect to a peripheral found by scanning.
  • Connect without scanning by specifying the device address.

Scanning

Use bleRemotePeripheral.connectWait() to connect to the peripheral found by scanning.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    localName: "Blank"
  };
  var peripheral = await obniz.ble.scan.startOneWait(target);
  if(peripheral) {
    console.log("found");
    try {
      await peripheral.connectWait();
      console.log("connected");
    } catch(e) {
      console.error(e);
    }
  }
}

It is not possible to scan while connecting, so scanning will be stop automatically when connecting starts. After connecting succeeded, you can run scanning again.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    uuids: ["1111"]
  };

  obniz.ble.scan.onfind = async function(peripheral){
   try {
      await peripheral.connectWait();
      console.log("connected");
      await obniz.ble.scan.startWait(target);
    } catch(e) {
      console.error(e);
    }
  };

  obniz.ble.scan.onfinish = function(){
    obniz.ble.scan.start(target);
  };

  await obniz.ble.scan.startWait(target);
}

Connected peripherals can be access from getConnectedPeripherals().

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    uuids: ["1111"]
  };

  obniz.ble.scan.onfind = async function(peripheral){
   try {
      await peripheral.connectWait();
      console.log("connected");
      console.log(obniz.ble.getConnectedPeripherals()); // => array of peripherals
    } catch(e) {
      console.error(e);
    }
  };

  obniz.ble.scan.onfinish = async function(peripherals, error){
    await obniz.ble.scan.startWait(target);
  };

  await obniz.ble.scan.startWait(target);
}

Connect without scanning

You can connect a device of which you have the address, for example, you scanned it before, using directConnectWait() function or directConnect() function. It will cause saving time in case there are lots of peripherals.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait(); 
  try {
    var peripheral = await obniz.ble.directConnectWait("e4b9efb29218", "random");
    console.log("connected");
  } catch(e) {
    console.log("can't connect");
  }
}

Automatic search for services and characteristis

obniz.js automatically searches for services of the peripheral and characteristics and descriptors associated with it after the BLE connection is completed, and connecting to them is also completed just after searching finished, so it is possible to operate all services and below immediately.

If you don't want it to search automatically due to problems such as connection time, set the argument of bleRemotePeripheral.connectWait() to {autoDiscovery:false}.

In that case, you need to search for services and characteristics manually, so you need to use bleRemoteService.discoverAllCharacteristicsWait() or bleRemoteCharacteristic.discoverAllDescriptorsWait().

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait({});
  obniz.ble.scan.onfind = async 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();
}

Error handling

In case the other party left while communication or impossible thing is requested, error will occur.

The error that occurred with peripheral is received by the function set in bleRemotePeripheral.onerror.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    localName: "Blank"
  };
  var peripheral = await obniz.ble.scan.startOneWait(target);
  if(peripheral) {
    console.log("found");
    try {
      await peripheral.connectWait();
      peripheral.onerror = function(err){
        console.log("error : " + err.message);
      }
      console.log("connected");
    } catch(e) {
      console.error(e);
    }
  }
}

Disconnection

Use disconnectWait() to disconnect peripherals.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
  await obniz.ble.initWait();
  var target = {
    localName: "Blank"
  };
  var peripheral = await obniz.ble.scan.startOneWait(target);
  if(peripheral) {
    try {
      console.log("found");
      await peripheral.connectWait();
      console.log("connected");
      await peripheral.disconnectWait();
      console.log("disconnected");
    } catch(e) {
      console.error(e);
    }
  }
}