This project has moved. For the latest updates, please go here.

System.Net.Sockets.SocketException when starting listenning bluetooth connection during Windows startup

Topics: Bluetooth - Microsoft
Jul 10, 2012 at 2:22 PM

Hi there,

I am developing a windows service which starts listenning the bluetooth connection when it is run using the following code:

                btListener = new BluetoothListener(service);
                btListener.Start();

If the service is started manually after Windows completes the startup (says after the Windows starts for around 5 minutes), then, everything works fine: the Bluetooth service is started successfully.

However, if I set the Windows service's startup type to automatic, that part of code causes the following exception:

System.Net.Sockets.SocketException (0x80004005): An invalid argument was supplied
   at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.ThrowSocketExceptionForHR(Int32 errorCode)
   at InTheHand.Net.Bluetooth.Msft.MicrosoftSdpService.SetService(Byte[] sdpRecord, ServiceClass cod)
   at InTheHand.Net.Bluetooth.Msft.WindowsBluetoothListener.SetService(Byte[] sdpRecord, ServiceClass cod)
   at InTheHand.Net.Bluetooth.Msft.WindowsBluetoothListener.Start(Int32 backlog)
   at InTheHand.Net.Bluetooth.Msft.WindowsBluetoothListener.Start()

I am thinking that is because the "Start" method is called too soon when the Bluetooth device is not yet initialized. Is there anyone also encountering this similar problem? Any idea on how to know when the "Start" method should be called to ensure it can start successfully?

Appreciate on any feedback or comments.

Thanks,

Quoc

Developer
Jul 13, 2012 at 12:48 PM

For a first attempt I'd make your service dependent on the Bluetooth service.  It might that that's the important bit and not the device itself being registered. I'm not at a PC with Bluetooth at the moment and can't remember the actual name of the respective service.

Jul 13, 2012 at 2:38 PM

Thanks, I set the service start type to "Automatic (Delay start)" and it solves the problem.