Comments (10)
Here's the code finally works:
fire_and_forget ReadDataAsync(BLEData data) {
{
lock_guard lock(dataQueueLock);
}
auto characteristic = co_await retrieveCharacteristic(data.deviceId, data.serviceUuid, data.characteristicUuid);
GattReadResult result = co_await characteristic.ReadValueAsync();
if (result.Status() == GattCommunicationStatus::Success) {
auto dataReader = DataReader::FromBuffer(result.Value());
auto output = dataReader.ReadString(dataReader.UnconsumedBufferLength());
BLEData data;
wcscpy_s(data.characteristicUuid, sizeof(data.characteristicUuid) / sizeof(wchar_t), to_hstring(characteristic.Uuid()).c_str());
wcscpy_s(data.serviceUuid, sizeof(data.serviceUuid) / sizeof(wchar_t), to_hstring(characteristic.Service().Uuid()).c_str());
wcscpy_s(data.deviceId, sizeof(data.deviceId) / sizeof(wchar_t), characteristic.Service().Device().DeviceId().c_str());
data.size = output.size();
memcpy(data.buf, output.data(), data.size);
{
lock_guard queueGuard(dataQueueLock);
dataQueue.push(data);
dataQueueSignal.notify_one();
}
//saveError(L"%s:%d read done: %s", __WFILE__, __LINE__, output.c_str());
}
}
from blewinrtdll.
Maybe related: #7, #19, #21, #20
Hi @tufeixp, the error message could help you.
from blewinrtdll.
Hi @adabru , I have ran the Microsoft sample project and it does not have this problem, would you please help me to debug this issue 'cause I'm really not good at C++ coding :-|
from blewinrtdll.
I have ran the Microsoft sample project and it does not have this problem
This seems like a good start.
Do you receive any error message? Also make sure that you use the version after February with the ui fix (#25).
from blewinrtdll.
Sure, I have the ui fix.
The error message is as simple as "Subscribe Characteristic catch: The attribute cannot be written"
from blewinrtdll.
I did a short google, maybe this can help: https://stackoverflow.com/questions/50596561/writeclientcharacteristicconfigurationdescriptorasync-not-working-in-uwp#50604121
You can also compare the subscribing code (https://github.com/adabru/BleWinrtDll/blob/main/BleWinrtDll/BleWinrtDll.cpp#L482) to the winrt code of the Microsoft sample.
For faster debugging you may consider using the DebugBle program. You can also print to the console from the dll like in https://github.com/adabru/BleWinrtDll/blob/main/BleWinrtDll/BleWinrtDll.cpp#L108.
from blewinrtdll.
Thanks @adabru I looked into the code, and find it's my miss-understanding lead me to the question, that read-only params eg. the serial infor can Not be "notified" other than query. I understand the current code does not implement a query method, and I tried to code in Cpp and able to read the correct info I need, however, due to my lacking of coding experience, the code will freeze and fail to pass over the data to C#, here's my simple code:
fire_and_forget ReadDataAsync(BLEData data)
{
{
lock_guard lock(characteristicQueueLock);
}
auto characteristic = co_await retrieveCharacteristic(data.deviceId, data.serviceUuid, data.characteristicUuid);
GattReadResult result = co_await characteristic.ReadValueAsync();
if (result.Status() == GattCommunicationStatus::Success) {
auto dataReader = DataReader::FromBuffer(result.Value());
auto output = dataReader.ReadString(dataReader.UnconsumedBufferLength());
Characteristic charStruct;
wcscpy_s(charStruct.uuid, sizeof(charStruct.uuid) / sizeof(wchar_t), to_hstring(characteristic.Uuid()).c_str());
wcscpy_s(charStruct.userDescription, sizeof(charStruct.userDescription) / sizeof(wchar_t), output.c_str());
{
lock_guard queueGuard(characteristicQueueLock);
characteristicQueue.push(charStruct);
characteristicQueueSignal.notify_one();
}
saveError(L"%s:%d read done: %s", __WFILE__, __LINE__, charStruct.userDescription);
}
}
void ReadData(BLEData* data)
{
ReadDataAsync(*data);
}
from blewinrtdll.
Glad you found the problem! Do you want a blocking, a non-blocking, or an optionally blocking read function?
from blewinrtdll.
A none blocking read function is needed~
from blewinrtdll.
Ok. The code actually already looks quite nice.
The first lock_guard looks redundant?
I guess you can use dataQueue instead of characteristicQueue. You could also reuse the function PollData without modification. Furthermore you would have more space for your data. The size limit could be a reason for the crash.
And maybe a try catch would be helpful to prevent a freeze.
from blewinrtdll.
Related Issues (20)
- Connecting and Subscribing to heart sensor HOT 3
- Unable to write attribute HOT 5
- Suddenly unable to subscribe to notifications from and write to characteristic HOT 1
- Communication with Dongle
- Laptop BLE no issue Resolving Characteristics, Desktop has issues... HOT 2
- Problems connecting two devices at the same time. HOT 1
- BleScan.Impl.PollData make my Unity crashed. HOT 2
- The ValueChanged function runs at irregular intervals. HOT 1
- Does BleWinrtDll provide API for pairing with passkey? HOT 4
- again: BleWinrtDll.cpp:501 SubscribeCharacteristicAsync catch: The attribute cannot be written. HOT 6
- Write to device problem HOT 2
- Error subscribing to Characteristic HOT 3
- Hololens 2 unable to connect to Microbit HOT 9
- η΄ζ₯θΏζ₯ Direct connection HOT 1
- Device Filtering by Service HOT 1
- Connect 2 DLLs to one BLE IndoorBike
- Connect 2 DLLs to one BLE IndoorBike HOT 3
- Microbit Bluetooth UART service data reading problem HOT 2
- Read issue after polling characteristics HOT 3
- Can't write the correct values HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blewinrtdll.