Coder Social home page Coder Social logo

Comments (10)

tufeixp avatar tufeixp commented on July 22, 2024 1

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.

adabru avatar adabru commented on July 22, 2024

Maybe related: #7, #19, #21, #20

Hi @tufeixp, the error message could help you.

from blewinrtdll.

tufeixp avatar tufeixp commented on July 22, 2024

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.

adabru avatar adabru commented on July 22, 2024

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.

tufeixp avatar tufeixp commented on July 22, 2024

Sure, I have the ui fix.
The error message is as simple as "Subscribe Characteristic catch: The attribute cannot be written"

from blewinrtdll.

adabru avatar adabru commented on July 22, 2024

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.

tufeixp avatar tufeixp commented on July 22, 2024

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.

adabru avatar adabru commented on July 22, 2024

Glad you found the problem! Do you want a blocking, a non-blocking, or an optionally blocking read function?

from blewinrtdll.

tufeixp avatar tufeixp commented on July 22, 2024

A none blocking read function is needed~

from blewinrtdll.

adabru avatar adabru commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.