0

I connect multiple Bluetooth clients and store them in a list:

BluetoothListener blueListener = new BluetoothListener(mUUID);
blueListener.Start();

while (true)
{
    try
    {
        //accept available clients for connection
        BluetoothClient localClient = blueListener.AcceptBluetoothClient();
        myClientList.Add(localClient);

    }

    catch(Exception e)
    { }
}

Now, when some client gets unexpectedly disconnected, I would like to remove it from the list. I tried to use the client's property 'LinkKey' in order to detect the disconnected client, but I get runtime error.

An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call

StackTrace = " at System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionLength)\r\n at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.get_LinkKey()\r\n
at MSBusinessLogicPack.BluetoothLogic.BTServerCl...

This exception occurs as soon as I try to get the LinkKey value, even while the client is still connected.

Here is the code that I use to remove the client from the list, assuming I have a way to identify the client, which I currently don't:

public override void ClientDisconnected(Guid clidenID)
{
    int removeIndex = myClientList.FindIndex(x => x.LinkKey == clidenID);

    myClientList.RemoveAt(removeIndex);


}
Ivan
  • 1,081
  • 2
  • 17
  • 43
  • "but I get runtime error" ... which you did not include in your question. Please do so. – Fildor Oct 05 '17 at 08:37
  • Also, what is `myClientList`? I guess a `List<>` ? – Fildor Oct 05 '17 at 08:38
  • BTW: You should check `removeIndex` for "-1". If you try to `RemoveAt(-1)` there will be the next exception. – Fildor Oct 05 '17 at 08:48
  • I'll add it shortly. And yes, 'myClientList' is a List<> – Ivan Oct 05 '17 at 08:49
  • Could you also add the relevant portion of the stacktrace? Which line of the shown code is it throwing the exception at? – Fildor Oct 05 '17 at 08:55
  • @Fildor please see the updated question. This exception occurs as soon as I try to get the LinkKey value, even while the client is still connected. – Ivan Oct 05 '17 at 09:09
  • _"This exception occurs as soon as I try to get the LinkKey value, even while the client is still connected."_ So you get this exception regardless of the device disconnected or is still connected? Not only in the "disconnected" handler? If it is disconnected this seems to make sense, as accessing the getter seems to try and call a socket operation that only works on connected clients. – Fildor Oct 05 '17 at 09:14
  • @Fildor Yes, I'll actually update my question again. – Ivan Oct 05 '17 at 09:15

1 Answers1

0

You can wrap the int removeIndex = myClientList.FindIndex(x => x.LinkKey == clidenID);

with a try catch block, catching the exact exception class that is thrown, and remove the client from the list in the catch section.

farbiondriven
  • 2,450
  • 2
  • 15
  • 31
  • Yes, but how to know which client? – Ivan Oct 05 '17 at 10:18
  • My bad, I misunderstood. A possible workaround is to store the Linkkey in a list when you connect, then check the index of that list in ClientDisconnected. Or you create only one list storing the couple (Bluetoothclient,Linkkey). – farbiondriven Oct 05 '17 at 10:28