cordova-plugin-bluetoothle icon indicating copy to clipboard operation
cordova-plugin-bluetoothle copied to clipboard

respond() to a status writeRequested not work

Open marcui13 opened this issue 2 years ago • 1 comments

Hi everyone!

I'm trying to respond to a 'writeRequested' state with the respond() method, but I see that it doesn't work as expected.

I need to reply to the device that made the writeRequest with a 'received' message.

This is the code that proves:

`if (status.status == "writeRequested") {

  if (
    status.service == "1234" &&
    status.characteristic == "D6D95414-516C-4DCF-B5CF-3BB1A560AF0F"
  ) {
    this.bluetoothle
      .respond({
        address: status.address,
        requestId: status.requestId,
        value: btoa('Recived message in tablet!') // encode base64
      })
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });
    this.recivedMessages.push(atob(status.value)); // decode base64
    this.cdr.detectChanges();
    console.log(atob(status.value));
  }

}`

When i debug the code, i see that the respond() method have success status, but the message is not visible in the other device.

writeRequested

NOTE: when i use the same method to respond 'readRequested' status works fine

Like this:

`if (status.status == "readRequested") {

  if (
    status.service == "1234" &&
    status.characteristic == "D6D95414-516C-4DCF-B5CF-3BB1A560AF0F"
  ) {
    this.bluetoothle
      .respond({
        address: status.address,
        requestId: status.requestId,
        value: btoa('Hello from tablet!')
      })
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });
  }

}`

This is the code of my writeRequest method:

`writeRequest(devAddress:string, messageS:string){

this.bluetoothle.write({
  address: devAddress,
  service: "1337",
  characteristic:"99AD1F53-0DC6-4D84-85CF-33CBE47C6EF8",
  value: btoa(messageS),
  type: ""
})
.then((resp)=>{
  console.log('RESPONSE from writeRequest', resp);
})
.catch((err)=>{
  console.log('ERROR IN writeRequest:', err);
})

}`

Is there an error in my implementation? Any idea how to solve it?

Of course, thank you for your time and kindness!

Best regards, marcui :)

marcui13 avatar Feb 17 '23 15:02 marcui13

Hi folks!

I continued debugging the code and put some console logs to have more information of the ejecution.

I can see that the respond() method in the 'writeRequested' state has a success status, but i don´t know how to display the message 'Recived message in...' on the device sending the message.

writeRequested screen

That screenshot is the screen of the tablet that recive the message from my phone.

My idea is that when you receive a message on either device, you can send a 'received' message to the device that sent that message

I would greatly appreciate your help!

Regards, marcui :)

marcui13 avatar Feb 22 '23 14:02 marcui13