halildurmus / dartwinrt

Idiomatic Dart projection of the modern Windows Runtime (WinRT) APIs.
BSD 3-Clause "New" or "Revised" License
49 stars 3 forks source link

Support events #161

Open srtonz opened 2 years ago

srtonz commented 2 years ago

Now that dart-windows/win32#370 has been closed I was hoping to get BLE device discovery and communication going.

Unfortunately the BluetoothFindFirstDevice function does not work for BLE devices, and instead it's recommended to use the BluetoothLEAdvertisementWatcher class for finding nearby BLE devices.

I've been able to make some progress toward this (thanks to the Calendar example), but I'm struggling to understand how to add event handlers for Receivedand Stopped events. Running the generate.cmd script with some debug modifications this is the error it's generating:

Projecting method: add_Received
Unable to project method: add_Received
Exception: Type information missing for GenericTypeModifier, TYPE: Windows.Foundation.TypedEventHandler`2.
#0      TypeProjection.projectType (file:///C:/Projects/win32/tool/projection/type.dart:246:5)
dart-windows/win32#1      TypeProjection.projection (file:///C:/Projects/win32/tool/projection/type.dart:54:21)
dart-windows/win32#2      TypeProjection.dartType (file:///C:/Projects/win32/tool/projection/type.dart:63:26)
dart-windows/win32#3      ParameterProjection.dartProjection (file:///C:/Projects/win32/tool/projection/parameter.dart:20:37)
dart-windows/win32#4      MethodProjection.methodParams.<anonymous closure> (file:///C:/Projects/win32/tool/projection/method.dart:76:42)
dart-windows/win32#5      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
dart-windows/win32#6      ListIterable.join (dart:_internal/iterable.dart:165:22)
dart-windows/win32#7      MethodProjection.methodParams (file:///C:/Projects/win32/tool/projection/method.dart:76:62)
dart-windows/win32#8      WinRTMethodProjection.toString (file:///C:/Projects/win32/tool/projection/winrt_method.dart:86:37)
dart-windows/win32#9      InterfaceProjection.toString.<anonymous closure> (file:///C:/Projects/win32/tool/projection/interface.dart:201:42)
dart-windows/win32#10     MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
dart-windows/win32#11     ListIterable.join (dart:_internal/iterable.dart:156:22)
dart-windows/win32#12     InterfaceProjection.toString (file:///C:/Projects/win32/tool/projection/interface.dart:201:54)
dart-windows/win32#13     generateWinRTApis (file:///C:/Projects/win32/tool/generation/generate_winrt_apis.dart:27:43)
dart-windows/win32#14     main (file:///C:/Projects/win32/tool/generation/generate.dart:36:3)
dart-windows/win32#15     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
dart-windows/win32#16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

I'd be happy to give this a go, but unsure where to start and whether this is currently possible at all?

timsneath commented 2 years ago

Grr... That's really infuriating that the Win32 APIs are half-implemented. Why do they have the other BluetoothGatt functions if you can't find devices? :( The endless Microsoft back-and-forth on preferred APIs keeps bringing the pain.

Unfortunately, the WinRT projection classes are in a very preliminary state. I've been making some progress this weekend, ironically, but I don't yet have event handlers or WinRT generics supported. So yeah, this is not possible without significant future iteration on the projection work.

Sunbreak commented 2 years ago

I would NOT recommand Dart projection on WinRT for BluetoothLE for now

Actually you have two options

  1. Call C++/WinRT APIs via MethodChannel or dart:ffi
  1. Call SetupAPI and APIs inside bthledef.h
Sunbreak commented 2 years ago

Related PR: https://github.com/timsneath/win32/pull/392

srtonz commented 2 years ago

Thanks both. We already have a working C++/WinRT implementation to use for now, this was really just trying to see how far we can push things into Dart "only". Feel free to ping me here if you need contributions in the future.

azimuthdeveloper commented 1 year ago

@srtonz it's a long shot, but is your implementation open source by any chance? Just getting into the BLE space quite a bit and am having simiilar issues getting BLE to work with Flutter.

srtonz commented 1 year ago

@azimuthdeveloper that was the intention originally, but work didn't progress beyond prototyping. At the moment a combination of flutter_reactive_ble and quick_blue (for Desktop) is working for us.

azimuthdeveloper commented 12 months ago

@srtonz I did that for a while but I could never get my app to be stable. I ended up writing the entire Windows bit in Rust which has been rock solid. I might do an article or YouTube video on how to do it.