ERROR appears while executing bluetooth.write() (bluetooth class are undefined )

50 Views Asked by At

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.

0

There are 0 best solutions below