6

New to WMI and am kind of feeling around in the dark a bit,

The following code detects connecting and disconnecting USB devices, but I also need to detect when a BT device is connected/disconnected (not just paired).

ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 OR EventType = 3");
watcher.EventArrived += new EventArrivedEventHandler(HardwareDeviceChange);
watcher.Query = query;
watcher.Start();

If it isn't achievable through WMI, is there another way I could go about it (hopefully avoiding 32feet solutions if at all possible).

Wobbles
  • 3,033
  • 1
  • 25
  • 51
  • wmi is the way to go for me i can't test but i would try this cuz we check for error and macadress maybee it help for what you try achieve "SELECT Caption FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0 and Caption like 'Standard Serial over Bluetooth link (COM%' and PNPDeviceID like '%&001653%' " – Zwan Feb 22 '16 at 13:03
  • 1
    The answer in [Monitor USB drives using DeviceWatcher](http://stackoverflow.com/a/25187240/1070452) would work for this with a few mods. Where that OP was interested in USB drives, you would want to compare Ports. In order to detect arrivals you have to compare before and after. There, the code uses Win32 Logical drives, but for BlueTooth if I change it to compare the list from `SerialPort.GetPortNames()` I can see BT devices come and go. I am not sure what you mean by a BT device - something connecting via BT or the adapter gets plugged in? – Ňɏssa Pøngjǣrdenlarp Feb 23 '16 at 14:54
  • 1
    @Plutonix I do not see how that would work as it calls the same event from WMI as my above example which does not detect the arrival or dismissal/disconnect of BT paired devices. – Wobbles Feb 23 '16 at 17:06
  • @Wobbles have you got any solution for Bluetooth devices connected/disconnected? – Sharan RM Feb 26 '19 at 13:43
  • @Wobbles do you have any solution(WqlEventQuery) paired or remove of Bluetooth device? – Sharan RM Feb 27 '19 at 04:19
  • @SharanRM not directly, I ended up just pinging the device through HID packets and testing response to determine if the device was actually present – Wobbles Feb 27 '19 at 14:33
  • thank you for reply @Wobbles – Sharan RM Feb 28 '19 at 10:36

0 Answers0