public BluetoothSocketWrapper Connect() { bool success = false; while (SelectSocket()) { adapter.CancelDiscovery(); try { bluetoothSocket.Connect(); success = true; break; } catch (System.IO.IOException e) { //try the fallback try { bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.GetUnderlyingSocket()); Thread.Sleep(500); bluetoothSocket.Connect(); success = true; break; } catch (FallbackException e1) { System.Console.WriteLine("BT", "Could not initialize FallbackBluetoothSocket classes.", e); } catch (InterruptedException e1) { System.Console.WriteLine("BT", e1.Message, e1); } catch (System.IO.IOException e1) { System.Console.WriteLine("BT", "Fallback failed. Cancelling.", e1); } } } if (!success) { throw new System.IO.IOException("Could not connect to device: " + device.Address); } return(bluetoothSocket); }
private bool SelectSocket() { if (candidate >= uuidCandidates.Count) { return(false); } BluetoothSocket tmp; UUID uuid = uuidCandidates[candidate++]; System.Console.WriteLine("BT", "Attempting to connect to Protocol: " + uuid); if (secure) { tmp = device.CreateRfcommSocketToServiceRecord(uuid); } else { tmp = device.CreateInsecureRfcommSocketToServiceRecord(uuid); } bluetoothSocket = new NativeBluetoothSocket(tmp); return(true); }