public async Task Connect() { var tc = new TaskCompletionSource <bool>(); _bluetoothConnectionThread = new BluetoothConnectionThread(BluetoothDevice); _bluetoothConnectionThread.DeviceConnected += (o, t) => { _isConnected = true; _bluetoothConnectionThread.ReceivedData += _bluetoothConnectionThread_ReceivedData; tc.TrySetResult(true); }; _bluetoothConnectionThread.DeviceConnectionFailed += (o, t) => { _isConnected = false; tc.TrySetResult(false); }; _bluetoothConnectionThread.Run(); if (!await tc.Task) { throw new Exception("failed to connect"); } }
public Task Connect() { var tc = new TaskCompletionSource <bool>(); _bluetoothConnectionThread = new BluetoothConnectionThread(BluetoothDevice); _bluetoothConnectionThread.DeviceConnected += (o, t) => { _isConnected = true; _bluetoothConnectionThread.ReceivedData += _bluetoothConnectionThread_ReceivedData; tc.TrySetResult(true); }; _bluetoothConnectionThread.DeviceConnectionFailed += (o, t) => { _isConnected = false; tc.TrySetResult(false); }; _bluetoothConnectionThread.Run(); return(tc.Task); }