nativescript-community / nativescript-community/ble
ERROR appears while executing bluetooth.write() (bluetooth class are undefined )
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 202
- Forks
- 79
- PR merge metrics
- No merged PRs in 30d
Description
here is the code. what I am doing here is when ble device notifies the phone, the phone sends back some data to the ble device.
here is the connection code:
await this.bluetooth.connect({
UUID: device_uuid,
onConnected: async (data) => {
this.stopPlatePeripheralUUID = data.UUID;
this.isConnected = true;
await this.bluetooth.discoverAll({
peripheralUUID: this.stopPlatePeripheralUUID,
});
},
onDisconnected: (data) => {
console.log(`Disconnect from ${data.UUID}`);
this.isConnected = false;
this.bluetooth.clear();
},
});
here is where the problem getting involved's code:
this.bluetooth.startNotifying({ // => when the code run in to this line, the error shows up, but only visible in console
characteristicUUID: TIME_CORRECTION_CHARACTERISTIC_UUID,
serviceUUID: SERVICE_UUID,
peripheralUUID: this.stopPlatePeripheralUUID,
onNotify: (result: ReadResult) => {
console.log(result);
await this.bluetooth.write({
characteristicUUID: TIME_CORRECTION_CHARACTERISTIC_UUID,
serviceUUID: SERVICE_UUID,
peripheralUUID: this.stopPlatePeripheralUUID,
value: ["ADR11BC2BEF2E9BF7530"],
timeout: 10,
});
},
});
but when this code runs, it shows this error
index.android.js:1940 error TypeError: Cannot read properties of undefined (reading 'bluetooth')
at Bluetooth.writeValueToChar (index.android.js:1852:1)
at index.android.js:1921:1
at Bluetooth.attachSubDelegate (index.android.js:2387:1)
at index.android.js:1896:1
at new Promise (<anonymous>)
at index.android.js:1873:1
at index.android.js:1632:1
at async index.js:118:1 TypeError: Cannot read properties of undefined (reading 'bluetooth')
at Bluetooth.writeValueToChar (file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:1998:34)
at file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:2067:26
at Bluetooth.attachSubDelegate (file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:2533:13)
at file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:2042:18
at new Promise (<anonymous>)
at file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:2019:51
at file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:1778:83
at async file:///data/data/org.nativescript.stopplatetimer/files/app/vendor.js:70019:36
I have tried a lot of solutions to try to solve this error like replacing this.bluetooth to new Bluetooth() or getBluetoothInstance() but it did nothing it just gets worse. and this error I have been doing a lot of research but still have no progress on this error. so I really need help with this. thx.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeScript call sites for startNotifying and bluetooth.write, then follow the Android stack trace through Bluetooth.writeValueToChar and attachSubDelegate. Reproduce the notification-to-write flow and determine why the write path reaches an undefined bluetooth value; done means the flow completes without the reported TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, typescript
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100