EddyVerbruggen / nativescript-bluetooth-demo

30 stars 21 forks source link

Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bluetoothGattService' of undefined #21

Open zfeng1 opened 5 years ago

zfeng1 commented 5 years ago

Thanks for the excellent plugin and demo. I've been using this template to build an app with buttons that write a hex value . While the demo has worked with no issues, I'm having trouble attaching a service/characteristic of 'write' or 'writeWithoutResponse' to a button on my app. The CLI keeps returning an error "TypeError: Cannot read property 'bluetoothGattService' of undefined" when I click the button.

The CLI returns all the services found for my device and in particular the read/write.

JS: ---- ###### adding service: { "UUID":"6e400001-b5a3-f393-e0a9-e50e24dcca9e", "characteristics":[{"UUID":"6e400003-b5a3-f393-e0a9-e50e24dcca9e","name":"6e400003-b5a3-f393-e0a9-e50e24dcca9e", "properties":{"read":false,"write":false,"writeWithoutResponse":false,"notify":true,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false}, "descriptors":[{"UUID":"2901","value":null},{"UUID":"2902","value":null}]}, {"UUID":"6e400002-b5a3-f393-e0a9-e50e24dcca9e","name":"6e400002-b5a3-f393-e0a9-e50e24dcca9e", "properties":{"read":false,"write":true,"writeWithoutResponse":true,"notify":false,"indicate":false,"broadcast":false,"authenticatedSignedWrites":false,"extendedProperties":false}, "descriptors":[{"UUID":"2901","value":null}]}] }

In my services.page.js I have this function:

function onWriteOpen(){ console.log("Executing writeWithoutResponse..."); bluetooth.writeWithoutResponse( { UUID: _peripheral.UUID, serviceUUID: "6e400001-b5a3-f393-e0a9-e50e24dcca9e", // read write service of BLE device characteristicUUID: "6e400002-b5a3-f393-e0a9-e50e24dcca9e", // transmit characteristic of BLE device service value: "0x4f" //awaitResponse: false // if false you will not get notified of errors (fire and forget) } ).then( function(result) { console.log("Writing " + value + "on " + _peripheral.UUID); }, function(err) { console.log("Failed to write " + value + "on "+ _peripheral.UUID + err); } ); }

Playground sample link: https://play.nativescript.org/?template=play-js&id=Aa0Mjs&v=2 Please add the nativescript-bluetooth NPM package to run it. I'd really appreciate some help on this, been stuck for a few days.

Pictures: https://imgur.com/a/KLQS1mD

zfeng1 commented 5 years ago

Just to add to the above, when I use the demo app I'm able to see the callback when writing a value using service/characteristic for the device:

JS: ---- ###### adding service: 1800 JS: ---- ###### adding service: 1801 JS: ---- ###### adding service: 00001530-1212-efde-1523-785feabcd123 JS: ---- ###### adding service: 180a JS: ---- ###### adding service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e JS: --- service selected: 6e400001-b5a3-f393-e0a9-e50e24dcca9e JS: ------- _MyGattCallback.onCharacteristicWrite JS: android.bluetooth.BluetoothGattCharacteristic@75d8936

When using the button tap action on my app, here's the error:

JS: ---- ###### adding service: 1800 JS: ---- ###### adding service: 1801 JS: ---- ###### adding service: 00001530-1212-efde-1523-785feabcd123 JS: ---- ###### adding service: 180a JS: ---- ###### adding service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e JS: Executing writeWithoutResponse... JS: Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bluetoothGattService' of undefined JS: Executing writeWithoutResponse... JS: Error in Bluetooth.writeWithoutResponse: TypeError: Cannot read property 'bluetoothGattService' of undefined