BLE Central Peripheral

peripheral.connected

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

peripheral.rssi

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

obniz.ble.scan.start();

peripheral.adv_data

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)

peripheral.localName

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)

peripheral.iBeacon

// Javascript Example

This returns iBeacon data if the peripheral has it. If none, it will return null.
The return values are shown below.

{
    uuid : "907e1d1d-d85d-497f-9e93-4c813a459cae", //hex string
    major : 1000, //number
    minor : 100, //number
    power : 300, //number
    rssi : -22, //number
}
// Javascript Example
await obniz.ble.initWait(); 
var target = {
    uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

console.log(peripheral.iBeacon)

[await] peripheral.connectWait()

This connects obniz to the peripheral.
If ble scannning 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.

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

peripheral.connect()

This function will try to connect a peripheral. onconnect will be caled when connected or ondisconnect will be called when failed.

If ble scannning is undergoing, scan will be terminated immidiately.

when connection established, all service/characteristics/desriptors will be discovered automatically. onconnect will be called after all discovery done.

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

peripheral.onconnect

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");
        }
        peripheral.connect();
    }
}
obniz.ble.scan.start();

peripheral.ondisconnect

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(){
            console.log("closed");
        }
        peripheral.connect();
    }
}
obniz.ble.scan.start();

[await] peripheral.disconnectWait()

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

peripheral.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);
}

peripheral.getService(uuid: string)

It returns a service which having specified uuid in peripheral.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) // => 1800 or 
} catch(e) {
  console.error(e);
}

peripheral.onerror

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

{
   error_code : 1,
   message : "ERROR MESSAGE",
   device_address : "abcdefghijkl", //hex string or null
   service_uuid : "FF00",           //hex string or null
   characteristic_uuid : "FF01", //hex string or null
   descriptor_uuid : "FF01", //hex string or null
}
// Javascript Example
await obniz.ble.initWait(); 
var target = {
    uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);

peripheral.onerror = function(err){
    console.log("error : " + err.message);
}

You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us