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

Unable to write attribute #48

Open
StevenDextrain opened this issue Apr 24, 2023 · 5 comments
Open

Unable to write attribute #48

StevenDextrain opened this issue Apr 24, 2023 · 5 comments

Comments

@StevenDextrain
Copy link

Hello !
First of all, thanks a lot for your project and the work you've done, as a novice when working with Bluetooth, it helps a lot. I've scoured the different issues already listed on this repo, but can't find something that answers my questions.
That being said, I'm currently having an issue when trying to subscribe to my write notification on my bluetooth device.
I get this error :
"Could not establish connection to device with ID BluetoothLE#BluetoothLEc4:23:60:eb:a3:ec-c5:02:be:bb:2a:69
System.Exception: Connection failed: BleWinrtDll.cpp:501 SubscribeCharacteristicAsync catch: Unable to write attribute (translated from French, not sure about the translation, sorry about that)"

The strange thing is that it happens when trying to subscribe to my device's Write characteristic.
I have 2 different characteristics on my device, one that is for Read/Notify, and one that is for Write, and I figured the script would be able to identify which is which, and subscribe to notifications for the Notify one, and subscribe to the Write characteristic to send messages, but I realize I might be wrong ?

I also had another question, it might be because I can't reach this part yet because of my previous issue, but I don't see a way to read data sent by the BLE device, am I missing something ? I looked for "Read" "Receive" and other keywords in the code but found no results

Anyways, I'm very much stuck and I'd really appreciate if you could help me 😁
Thank you so much, have a nice day !

@adabru
Copy link
Owner

adabru commented Apr 24, 2023

Hi @StevenDextrain :-)

you only subscribe to characteristics that issue notify events. After subscription you can read incoming messages with PollData.

You can write to characteristics without prior subscription.

"ReadCharacteristic" is not implemented in the dll although it is a ble functionality. If you need it you can checkout #6 (comment) . But subscribing alone might be sufficient for you.

@StevenDextrain
Copy link
Author

Hey again !
Thanks for so quick of a response 😁
I'm wondering what you mean by "But subscribing alone might be sufficient for you.", is there already a way to collect my device's data without implementing ReadCharacteristic ?

@StevenDextrain
Copy link
Author

Ah nevermind, I just realized I missed part of the first (and most important part of your reply 🙃), I'm gonna try that and come back with another question, or hopefully, everything will work fine and I'll close the issue !
Thanks again !

@StevenDextrain
Copy link
Author

Hello again,

It doesn't work (yet), and I noticed that the BleApi.SendData(in data, false) method call returned "false".
I was wondering if it meant that there was an issue while sending the data or if I'm totally misled...
Follow-up question, PollData is used to read what the device is sending constantly, right ?
I'm sorry about all of these questions, but I've never programmed in C++, and I've also never had to work with hardware before 🙃 (So you're basically my only hope)

Thanks again, and have a nice day !

@adabru
Copy link
Owner

adabru commented May 13, 2023

BleApi.SendData(in data, false) method call returned "false".

For the non-blocking call, the result is always false:

bool result = false;
// copy data to stack so that caller can free its memory in non-blocking mode
SendDataAsync(*data, block ? &signal : 0, block ? &result : 0);
if (block)
signal.wait(lock);
return result;

You should see an error in the message window eventually, if it doesn't succeed. The blocking call returns true on success of sending:

if (status != GattCommunicationStatus::Success)
saveError(L"%s:%d Error writing value to characteristic with uuid %s", __WFILE__, __LINE__, data.characteristicUuid);
else if (result != 0)
*result = true;

The success doesn't guarantee that the package arrives at the target device though. There is a possibility to do that though.

PollData is used to read what the device is sending constantly, right ?

Exactly:

SubscribeCharacteristicAsync

subscription->revoker = characteristic.ValueChanged(auto_revoke, &Characteristic_ValueChanged);

Characteristic_ValueChanged

dataQueue.push(data);

PollData

if (!dataQueue.empty()) {
*data = dataQueue.front();
dataQueue.pop();
return true;
}

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

2 participants