Reputation: 93
I am developing a windows service which starts listenning the bluetooth connection using InTheHand.Net.Bluetooth library 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
Upvotes: 1
Views: 2044
Reputation: 4570
Typically you'd solve this kind of problem by configuring your service to be dependent on whichever service (or services) it needs to be running before it starts. Then the SCM will make sure it doesn't start your service before the other services have started.
This KB article is a bit old but it explains the basics.
Upvotes: 1