private async void ConnectButton_Click(object sender, RoutedEventArgs e) { if (BluetoothManager.LEScanner.Status == BluetoothLEScannerState.Started) { BluetoothManager.LEScanner.Stop(); } if (BluetoothManager.RfcommScanner.Status == BluetoothRfcommScannerState.Started) { BluetoothManager.RfcommScanner.Stop(); } ConnectionBuilder connectionBuilder = new ConnectionBuilder(BluetoothManager, ConnectionProfile, SelectedDevice); try { var result = await connectionBuilder.StartAsync(); ConnectionBuildResult = result; await Dispatcher.InvokeAsync(() => { Close(); }); } catch (Exception exception) { BluetoothManager.LEScanner.Start(); await Dispatcher.InvokeAsync(() => { MsgTextBlock.Text = exception.Message; }); } }
private async void ConnectButton_Click(object sender, RoutedEventArgs e) { if (BluetoothManager.LEScanner.Status == BluetoothLEScannerState.Started) { BluetoothManager.LEScanner.Stop(); } IsPrimaryButtonEnabled = false; var selectedDevice = (ScanResultListView.SelectedItem as BleDeviceViewModel).BluetoothDevice; MessageTextBlock.Text = "正在连接到:" + selectedDevice.Name; ConnectionBuilder connectionBuilder = new ConnectionBuilder(BluetoothManager, ConnectionProfile, selectedDevice); try { var connectionBuildResult = await connectionBuilder.StartAsync(); Result = connectionBuildResult; Hide(); } catch (Exception exception) { MessageTextBlock.Text = exception.Message; BluetoothManager.LEScanner.Start(); } }
private void BleDeviceSelectorDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args) { Result = null; BluetoothManager.LEScanner.Added += LEScanner_Added; BluetoothManager.LEScanner.Removed += LEScanner_Removed; BluetoothManager.LEScanner.EnumerationCompleted += LEScanner_EnumerationCompleted; BluetoothManager.LEScanner.Stopped += LEScanner_Stopped; BluetoothManager.LEScanner.Start(); }
public MainWindow() { InitializeComponent(); Ahk = AutoHotkeyEngine.Instance; InputSimulator = new InputSimulator(); KeyActionManager = new KeyActionManager(Dispatcher, InputSimulator); BluetoothManager bluetoothManager = new BluetoothManager(); GyroscopeReadingUuid = GyroscopeRfcommServiceConnectionWrapper.RfcommServiceId; KeyboardServiceUuid = Constants.KeyboardServiceGuid; MouseServiceUuid = Constants.MouseServiceId; MouseCharacteristicUuid = Constants.MouseActionCharacteristicWrapper; KeyboardCharacteristicUuid = Constants.KeyActionCharacteristicWrapper; var characteristicDict = new Dictionary <Guid, List <CharacteristicProfile> >(); characteristicDict.Add(KeyboardServiceWrapper.Guid, new List <CharacteristicProfile>() { new CharacteristicProfile { Notified = true, Guid = Constants.KeyActionCharacteristicWrapper }, new CharacteristicProfile { Notified = true, Guid = Constants.MouseActionCharacteristicWrapper } }); var serviceId = new List <Guid> { MouseServiceWrapper.ServiceId }; ConnectionProfile profile = new ConnectionProfile() { RequiredCharacteristicGuids = characteristicDict, RequiredServiceGuids = serviceId }; BleDeviceSelectorWindow bleDeviceSelectorWindow = new BleDeviceSelectorWindow(bluetoothManager, profile); bleDeviceSelectorWindow.ShowDialog(); ConnectionBuildResult = bleDeviceSelectorWindow.ConnectionBuildResult; MouseServiceWrapper = new MouseServiceWrapper(ConnectionBuildResult[MouseServiceUuid].RfcommConnection); MouseServiceWrapper.OnMouseMoveReceived += MouseServiceWrapper_OnMouseMoveReceived; KeyboardServiceClientWrapper = new KeyboardServiceClientWrapper(ConnectionBuildResult[KeyboardServiceUuid, KeyboardCharacteristicUuid], ConnectionBuildResult[KeyboardServiceUuid, MouseCharacteristicUuid]); KeyboardServiceClientWrapper.OnKeyStatusChanged += KeyboardServiceClientWrapper_OnKeyStatusChanged; KeyboardServiceClientWrapper.OnMouseStatusChanged += KeyboardServiceClientWrapper_OnMouseStatusChanged; }