Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jkh675 opened this issue Oct 9, 2023 · 6 comments

Comments

@jkh675
Copy link

jkh675 commented Oct 9, 2023

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
image

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.

@jkh675 jkh675 changed the title bluetooth class are undefined while bluetooth.write() ERROR appears while executing bluetooth.write() (bluetooth class are undefined ) Oct 9, 2023
@farfromrefug
Copy link
Member

@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?

@jkh675
Copy link
Author

jkh675 commented 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

  JS: CONSOLE INFO: NativescriptBle: startNotifying {"characteristicUUID":"0739","serviceUUID":"1b2c","peripheralUUID":"B0:B2:1C:A7:F6:0A"}
  JS: CONSOLE INFO: NativescriptBle: startNotifying ---- peripheralUUID:B0:B2:1C:A7:F6:0A serviceUUID:1b2c characteristicUUID:0739
  JS: CONSOLE INFO: NativescriptBle: TNS_BluetoothGattCallback.onDescriptorWrite ---- gatt: android.bluetooth.BluetoothGatt@6d7b00d, descriptor: android.bluetooth.BluetoothGattDescriptor@52f0594, status: 0
  JS: CONSOLE INFO: NativescriptBle: TNS_BluetoothGattCallback.onCharacteristicChanged ---- gatt: android.bluetooth.BluetoothGatt@6d7b00d, characteristic: android.bluetooth.BluetoothGattCharacteristic@9e3d01, device: B0:B2:1C:A7:F6:0A
  JS: CONSOLE INFO: NativescriptBle: write {"characteristicUUID":"0739","serviceUUID":"1b2c","peripheralUUID":"B0:B2:1C:A7:F6:0A","value":["1"]}
  JS: CONSOLE INFO: NativescriptBle: actual write ---- peripheralUUID:B0:B2:1C:A7:F6:0A serviceUUID:1b2c characteristicUUID:0739

@farfromrefug
Copy link
Member

@jkh675 if there is a crash it will appear in thé logcat

@jkh675
Copy link
Author

jkh675 commented Oct 12, 2023

nope it just crash, no any informations

@farfromrefug
Copy link
Member

@jkh675 then it is hard to help you. Might be related to the data you are trying to write

@CatchABus
Copy link
Contributor

CatchABus commented 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 encoding property to write. I set it to utf-8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants