0

I am struggling to implement a function to detect a new headset being connected to the windows device in a WPF application. I have tried to implement it in the following way but the code fails to trigger when connecting any sound device (testing using USB headsets currently as jack sockets are not available in the windows devices currently in use).

Following previous postings on here I have implemented the following :-

In my MainWindow Class :-

//Create sound listener for new devices
        public MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
        public NotificationClientImplementation notificationClient;
        public IMMNotificationClient notifyClient;
        public int RegisterEndpointNotificationCallback([In][MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            //DeviceEnum declared below
            return deviceEnum.RegisterEndpointNotificationCallback(client);
        }

and I have a separate class for NotificationClientImplementation :-

public class NotificationClientImplementation : IMMNotificationClient
    {

        public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
        {
            //Do some Work
        }

        public void OnDeviceAdded(string deviceId)
        {
            //Do some Work
            ((MainWindow)System.Windows.Application.Current.MainWindow).SoundNewAction();
        }

        public void OnDeviceRemoved(string deviceId)
        {
            //Do some Work
        }

        public void OnDeviceStateChanged(string deviceId, DeviceState newState)
        {
            //Do some Work
        }


        public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
        {
            //Do some Work
        }   
    }

I have then implemented back in the main window the following to do the work :-

public void SoundNewAction()
        {
            m_notifyIcon.BalloonTipText = "A New Sound Device has been connected";
            m_notifyIcon.BalloonTipTitle = "New Sound Device;
            m_notifyIcon.BalloonTipClicked += new EventHandler(BalloonTipClicked);

            m_notifyIcon.ShowBalloonTip(2000);
        }

For some reason the code is never triggered, I have tried connecting new devices, devices already known to the machine and every option I can find but it doesn't trigger the action.

I know I am missing something simple but I can't for the life of me find it.

Thanks in advance.

Rich G
  • 93
  • 1
  • 7
  • Your [mre] seems incomple if I take a peek at [Check for device change add remove events](https://stackoverflow.com/questions/16245706/check-for-device-change-add-remove-events) which could be used as duplicate to close this one - it handles the same things and has a detailed WPF part. Maybe you can fix your code using the other post? But that post is 7y old so maybe there are other ways to accomplish the same thing implemented since then... – Patrick Artner Jun 23 '21 at 15:27

0 Answers0