Repository navigation
ERROR appears while executing bluetooth.write() (bluetooth class are undefined ) #275
Copy link
Copy link
Open
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]bluetooth class are undefined while bluetooth.write() [/-][+]ERROR appears while executing bluetooth.write() (bluetooth class are undefined )[/+]farfromrefug commentedon Oct 11, 2023
@jkh675 you have a build issue. the JS is not finding java class
com.nativescript.bluetooth.Bluetooth. You can try a clean build. Also are you using native--api-usage.json?normalid-awa commentedon Oct 11, 2023
thanks for your reply! I've tried clean build and I have also tried to update all packages to the newest version, but the problem was not solved instead, when the write function called it didn't show up any message, it just crashed, without any information.
here is the log
farfromrefug commentedon Oct 11, 2023
@jkh675 if there is a crash it will appear in thé logcat
normalid-awa commentedon Oct 12, 2023
nope it just crash, no any informations
farfromrefug commentedon Oct 12, 2023
@jkh675 then it is hard to help you. Might be related to the data you are trying to write
CatchABus commentedon Oct 18, 2023
@jkh675 In the past, I think I had similar problems when my device specified characteristic was not writable.
Also, make sure to add
encodingproperty to write. I set it toutf-8.