Closed dunda123 closed 4 years ago
Hey there,
I'm sorry, the examples are in a pretty bad state, I am not at version 1.0.0
with this library so things are still a bit messy. Try this code:
import Sblendid from "@sblendid/sblendid";
const batteryServiceUuid = "180f";
const batteryLevelUuid = "2a19";
(async () => {
try {
console.log("Searching for a peripheral");
const peripheral = await Sblendid.connect(peripheral => {
process.stdout.write(".");
return peripheral.hasService(batteryServiceUuid);
});
console.log("Found", peripheral);
const batteryService = await peripheral.getService<any>(batteryServiceUuid);
await batteryService!.on(batteryLevelUuid, (batteryLevel: any) => {
console.log("Battery Level", batteryLevel.readUInt8(0), "%");
});
} catch (error) {
console.log(error);
process.exit(1);
}
})();
What I did is to use any
(which noone should) and !
to tell TypeScript that the service is definitely not there, which it cannot be sure of.
If you are on TypeScript you need to have
{
"compilerOptions": {
"target": "ES2015"
}
}
in your tsconfig.json
and you need to install
npm i @types/node --save-dev
for this to work.
But I am very afraid this example still fails. This will fix TypeScript but you will receive the error
Error: Failed to turn on notifications for 2a19
which is probably my bad. This example is not right yet. Unfortunately I want to implement Linux support first before I can fix this issue. I am somewhat sure that the problem lies in trying to subscribe to this specific service uuid, which probably can't be done this way (so the example is bad) and not that the library is broken because I know of other users who work with subscriptions.
You can try reading the battery level instead of subscribing to it:
import Sblendid from "@sblendid/sblendid";
const batteryServiceUuid = "180f";
const batteryLevelUuid = "2a19";
(async () => {
try {
console.log("Searching for a peripheral");
const peripheral = await Sblendid.connect(peripheral => {
process.stdout.write(".");
return peripheral.hasService(batteryServiceUuid);
});
console.log("Found", peripheral);
const batteryService = await peripheral.getService<any>(batteryServiceUuid);
const batteryLevel = (await batteryService!.read(
batteryLevelUuid
)) as Buffer;
console.log("Battery Level", batteryLevel.readUInt8(0), "%");
} catch (error) {
console.log(error);
process.exit(1);
}
})();
Thanks. Linux support is very important for me. I have already solved this problem by list the characteristics and using the on('event',...)
method. Your library is the best I found. But it is very difficult to find, which is a pity. Sorry for my english.
Thanks for the great work!!!
Oh thank you so much. I will still keep this in mind, of course I want this to be bug-free for version 1.0.0
. Btw your English is totally fine!
Hi. I'm testing your library, but I couldn't compile the example of subscribe:
Compile error:
Can you help me? Thanks a lot