Callback function when read value.
Callback function will be called when registerNotify finished.
This doesn't call when notify arrived. It call when 'reiterate' finished.
Callback function will be called when unregisterNotify finished.
Callback function when write value finished.
It is an array of properties of a characteristics. It contains some of belows.
console.log(characteristics.properties); // => ['read', 'write', 'notify']
See how works at https://www.bluetooth.com/ja-jp/specifications/bluetooth-core-specification/
Service instance
It is uuid as string.
console.log(attr.uuid); // => '4C84'
It contains descriptors in a characteristic. It was 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");
var service = peripheral.getService("1800")
var c = service.getCharacteristic("fff0")
for (var d of c.descriptors) {
console.log(d.uuid)
}
} catch(e) {
console.error(e);
}
This characteristics can broadcast or not.
This characteristics can indicate or not.
This characteristics can notify or not.
This characteristics can read or not.
This characteristics can write or not.
This characteristics can 'write without response' or not.
Discover services.
If connect setting param 'autoDiscovery' is true(default), services are automatically discover 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");
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();
It returns a descriptors which having specified uuid in a characteristic. Return value is null when not matched.
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")
var c = service.getCharacteristic("fff0")
var d = c.getDescriptor("fff0")
console.log(d.uuid)
} catch(e) {
console.error(e);
}
Wait for notification and return data when it arrives.
await obniz.ble.initWait();
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
await peripheral.connectWait();
let char = peripheral.getService('fff0').getCharacteristic('fff1');
let data = await c.getNotifyWait();
console.log("notify with data " + data.join(','));
data from notification of the device
Use readWait() instead from 3.5.0
Wrapper for writeWait with data converting from number. It writes data as 1byte.
It throws an error when failed.
val
Wrapper for readWait with data converting to text. It convert UTF-8 and write binary array to string.
It throws an error when failed.
It reads data from the characteristic.
Even you wrote string or number, it returns binary array. The returned value appears in the callback function (onread). If reading succeeds an Array with data will be returned. It throws an error when failed.
// Javascript Example
await obniz.ble.initWait();
var target = {
uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);
if(peripheral){
await peripheral.connectWait();
console.log("connected");
await obniz.wait(1000);
var dataArray = await peripheral.getService("FF00").getCharacteristic("FF01").readWait();
console.log(dataArray);
}
This sets a callback function to receive notify when it comes from periperal. To receive notify, you need to register on CCCD Descriptor(0x2902).
More information of BLE/CCCD is available at bluetooth.com.
await obniz.ble.initWait();
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
await peripheral.connectWait();
let char = peripheral.getService('fff0').getCharacteristic( 'fff1');
char.onregisternotify = function() {
console.log("register finshed")
}
char.registerNotify( function(data){
console.log("notify with data " + data.join(','));
});
This sets a notify callback function and wait to finish register.
await obniz.ble.initWait();
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
await peripheral.connectWait();
let char = peripheral.getService('fff0').getCharacteristic( 'fff1');
await char.registerNotifyWait( function(data){
console.log("notify with data " + data.join(','));
});
unregistrate a callback which is registrated by registerNotify or registerNotifyWait.
await obniz.ble.initWait();
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
await peripheral.connectWait();
let char = peripheral.getService('fff0').getCharacteristic( 'fff1');
char.onregisternotify = function() {
console.log("register finished")
char.unregisterNotify();
}
char.onunregisternotify = function() {
console.log("unregistrated")
}
char.registerNotify( function(data){
console.log("notify with data " + data.join(','));
});
Unregistrate a callback which is registrated by registerNotify or registerNotifyWait. And wait until done.
await obniz.ble.initWait();
var target = {
localName: "obniz-notify"
};
var peripheral = await obniz.ble.scan.startOneWait(target);
await peripheral.connectWait();
let char = peripheral.getService('fff0').getCharacteristic('fff1');
await char.registerNotifyWait(function(data){
console.log("notify with data " + data.join(','));
});
await char.unregisterNotifyWait();
console.log("unregistrated")
Use writeWait() instead from 3.5.0
Wrapper for writeWait with data converting from number. It writes data as 1byte.
It throws an error when failed.
Wrapper for writeWait with data converting from text. It convert string to UTF-8 and write binary array.
It throws an error when failed.
This writes dataArray to the characteristic. It throws an error when failed.
// Javascript Example
await obniz.ble.initWait();
var target = {
uuids: ["fff0"],
};
var peripheral = await obniz.ble.scan.startOneWait(target);
if(peripheral){
await peripheral.connectWait();
console.log("connected");
await obniz.wait(1000);
var dataArray = [0x02, 0xFF];
await peripheral.getService("FF00").getCharacteristic("FF01").writeWait(dataArray);
console.log("write success");
}
Generated using TypeDoc