BLE address
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)
Raw data of advertisement
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
This returns connection completion time with a connected state.
If not connected, returns 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)
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)
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();
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();
This gets called with an error message when some kind of error occurs.
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();
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)
Ad Type: 0x16 (16bit UUID)
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);
}
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");
}
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");
}
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();
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);
}
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.
BlePairingOptions
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);
}
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);
}
Generated using TypeDoc