0

I am trying to connect bluetooth device (RFComm). Firstly i think i need to get the device list, so i try to use Windows.Devices.Enumeration.

VS2019 does not show any error while editing code. But when I start to run it, VS show me in output:

error CS1545: Property, indexer, or event 'DeviceWatcher.Added' is not supported by the language; try directly calling accessor methods 'DeviceWatcher.add_Added(TypedEventHandler<DeviceWatcher, DeviceInformation>)' or 'DeviceWatcher.remove_Added(EventRegistrationToken)'

Can i not use this class in c#? Or how should i modify the code?

Code:


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            deviceWatcher = DeviceInformation.CreateWatcher();

            deviceWatcher.Added += onAdded;

            deviceWatcher.Start();
        }

        private DeviceWatcher deviceWatcher = null;

        public void onAdded(DeviceWatcher dw, DeviceInformation di)
        {
            this.listBox1.Items.Add(di.Id.ToString() + "_" + di.Name);
        }

    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Ye Luo
  • 1
  • 1
    [Tagging help page](https://stackoverflow.com/help/tagging): _"The only time you should use tags in your title is when they are organic to the conversational tone of the title."_ - I've removed the tags from your question title for you. – ProgrammingLlama Nov 05 '19 at 07:22
  • `DeviceInformation` belongs to `Windows.Devices.Enumeration`, an UWP assembly. – Jimi Nov 05 '19 at 08:05
  • 1
    You could create a [WqlEventQuery](https://learn.microsoft.com/en-us/dotnet/api/system.management.wqleventquery.-ctor) to initialize a [ManagementEventWatcher](https://learn.microsoft.com/en-us/dotnet/api/system.management.managementeventwatcher) in the `CIMV2` scope with a generic `TargetInstance` and subscribe to the [ManagementEventWatcher.EventArrived](https://learn.microsoft.com/en-us/dotnet/api/system.management.managementeventwatcher.eventarrived) event. – Jimi Nov 05 '19 at 08:21
  • Sample code here: [How do i get info about recently connected usb device](https://stackoverflow.com/a/54298316/7444103) – Jimi Nov 05 '19 at 08:29
  • Thanks a lot for your answer! But i cannot find the class WqlEventQuery in System.Management. Do i need anyother setting to use this class? – Ye Luo Nov 05 '19 at 12:17
  • 1
    No. You have to add a Project reference to `System.Management` and of course `using System.Management` on top of the class that uses these objects – Jimi Nov 05 '19 at 13:07
  • Thank you! Now i can try this class. – Ye Luo Nov 05 '19 at 22:10
  • Hi, now i can read all other devices, and paired bluetooth device. But i cannot scan new bluetooth device. Devicewatcher is only UWP assembly, and sealed. How can i scann BT devices in winform? – Ye Luo Nov 06 '19 at 08:44
  • In fact months ago i have tried 32feet. But it cannot find all bluetooth device, i do not mean it cannot find BLE device, some normal BT devices it cannot find, while i can find them in win10 bluetooth scanner, maybe some devices are filtered. Or is it possible to operate the win10 bluetooth scanner? – Ye Luo Nov 06 '19 at 08:49

0 Answers0