chrvadala / node-ble

Bluetooth Low Energy (BLE) library written with pure Node.js (no bindings) - baked by Bluez via DBus
https://www.npmjs.com/package/node-ble
MIT License
308 stars 45 forks source link

HELP - const gattServer = await device.gatt()- don't work :/ #25

Open mathbozzi opened 3 years ago

mathbozzi commented 3 years ago

1. const { createBluetooth } = require('node-ble')
2. const { TEST_DEVICE, TEST_SERVICE, TEST_CHARACTERISTIC, TEST_NOTIFY_SERVICE, TEST_NOTIFY_CHARACTERISTIC } = process.env
3. 
4. async function main () {
5.   const { bluetooth, destroy } = createBluetooth()
6. 
7.   // get bluetooth adapter
8.   const adapter = await bluetooth.defaultAdapter()
9.   await adapter.startDiscovery()
10.   console.log('discovering')
11. 
12.   // get device and connect
13.   const device = await adapter.waitDevice(TEST_DEVICE)
14.   console.log('got device', await device.getAddress(), await device.getName())
15.   await device.connect()
16.   console.log('connected')
17.   // console.log(device)
18.   console.log(await device.getRSSI())
19.   const gattServer = await device.gatt()
20.   console.log(gattServer)
21.   // read write characteristic
22.   const service1 = await gattServer.getPrimaryService("0000180f-0000-1000-8000-00805f9b34fb")
23.   // console.log(service1)
24.   const characteristic1 = await service1.getCharacteristic("00002a19-0000-1000-8000-00805f9b34fb")
25.   // await characteristic1.writeValue(Buffer.from('Hello world'))
26.   const buffer = await characteristic1.readValue()
27.   console.log('read', buffer, buffer.toString())
28. 
29.   destroy()
30. }
31. 
32. main()
33.   .then(console.log)
34.   .catch(console.error)
35. 

out:

discovering
got device EC:21:E5:43:94:CA BLESmart_00000044EC21E54394CA
connected
-43

I have trying to connect my blood pressure monitor to extract the data, but in line 19 - (const gattServer = await device.gatt()) doesn't seem to work. The code stay in line 19 and never get the service in line 22. May some one help me? please!

rzumbuehl commented 2 years ago

I ran into the same issue. Any solution available?

mathbozzi commented 2 years ago

Please, try to disconnect the device to all gatways. Or, try to factory reset your device.

rzumbuehl commented 2 years ago

@mathbozzi thanks for your reply. I just noticed, that the device immediately gets disconnected after the device.connect() call. That's the reason in my case why the device.gatt() call does not return. I need to further investigate on this.

afturner commented 2 years ago

@rzumbuehl did you find a cause for this issue? I am running into the same problem.

rzumbuehl commented 2 years ago

@rzumbuehl did you find a cause for this issue? I am running into the same problem.

As mentioned, the root cause of my problem was that the BLE device closed the connection to the BLE Central (Raspberry PI) immediately after the device.connect() call. This was the reason why device.gatt() call did not return.

rzumbuehl commented 2 years ago

@rzumbuehl looks you are using omron's device. Correct? I have the same device. I have similar issue. I can connect to the devices but could not manage to get any data from the notification Services.If you manage to get any data out please share

No, I'm actually using another BLE device (wearable). Sorry, I can't help here.

afturner commented 2 years ago

@rzumbuehl I was curious if you had discovered why the device was closing the connection immediately. I am observing the same behavior.

lesha369 commented 8 months ago

hello. this theme is actual. i am cant get device.gatt() .

this code not working too. and this not resolve problem .

try {
            this.gattServer = await Promise.race([
                device.gatt(),
                new Promise((resolve, reject) => setTimeout(() => {
                    reject('cant get device.gatt()');
                }, this.timeout)),
            ]);
            logger.info(`get gatt`);
            this.device = device;
        } catch (error) {
            throw error;
        }
mteichtahl commented 5 months ago

has there been any progress on this issue ?