/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// /// We will enable/disable parts of the UI if the device doesn't support it. /// </summary> /// <param name="eventArgs">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> protected override async void OnNavigatedTo(NavigationEventArgs e) { rootPage = MainPage.Current; Console.WriteLine("AAAA1"); // Attach a handler to process the received advertisement. // The watcher cannot be started without a Received handler attached watcher.Received += OnAdvertisementReceived; // Attach a handler to process watcher stopping due to various conditions, // such as the Bluetooth radio turning off or the Stop method was called watcher.Stopped += OnAdvertisementWatcherStopped; // Attach handlers for suspension to stop the watcher when the App is suspended. App.Current.Suspending += App_Suspending; App.Current.Resuming += App_Resuming; var audioDeviceCollection = await DeviceInformation.FindAllAsync(AudioPlaybackConnection.GetDeviceSelector(), null); // initAudioWatcher(); rootPage.NotifyUser("Press Run to start watcher." + " " + audioDeviceCollection.Count + " audio device found", NotifyType.StatusMessage); }
private void MainGrid_Loaded(object sender, RoutedEventArgs e) { audioPlaybackConnections = new Dictionary <string, AudioPlaybackConnection>(); // Start watching for paired Bluetooth devices. this.deviceWatcher = DeviceInformation.CreateWatcher(AudioPlaybackConnection.GetDeviceSelector()); // Register event handlers before starting the watcher. this.deviceWatcher.Added += this.DeviceWatcher_Added; this.deviceWatcher.Removed += this.DeviceWatcher_Removed; this.deviceWatcher.Start(); }
public void Dispose() { GC.SuppressFinalize(this); IsConnected = false; if (AudioConnection != null) { AudioConnection.Dispose(); AudioConnection = null; } ConnectionStatusChanged -= StatusChanged; }
private async void AudioPlaybackConnection_ConnectionStateChanged(AudioPlaybackConnection sender, object args) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (sender.State == AudioPlaybackConnectionState.Closed) { ConnectionState.Text = "Disconnected"; } else if (sender.State == AudioPlaybackConnectionState.Opened) { ConnectionState.Text = "Connected"; } else { ConnectionState.Text = "Unknown"; } }); }
private void ReleaseAudioPlaybackConnectionButton_Click(object sender, RoutedEventArgs e) { // Check if an audio playback connection was already created for the selected device Id. If it was then release its reference to deactivate it. // The underlying transport is deactivated when all references are released. if (!(DeviceListView.SelectedItem is null)) { var selectedDeviceId = (DeviceListView.SelectedItem as DeviceInformation).Id; if (audioPlaybackConnections.ContainsKey(selectedDeviceId)) { AudioPlaybackConnection connectionToRemove = audioPlaybackConnections[selectedDeviceId]; connectionToRemove.Dispose(); this.audioPlaybackConnections.Remove(selectedDeviceId); // Notify that the media device has been deactivated. ConnectionState.Text = "Disconnected"; OpenAudioPlaybackConnectionButtonButton.IsEnabled = false; } } }
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { foreach (DeviceInformation device in this.devices) { if (device.Id == deviceInfoUpdate.Id) { this.devices.Remove(device); rootPage.NotifyUser(string.Format($"AudioPlayDevice {device.Name} Removed"), NotifyType.StatusMessage); break; } } if (audioPlaybackConnections.ContainsKey(deviceInfoUpdate.Id)) { AudioPlaybackConnection connectionToRemove = audioPlaybackConnections[deviceInfoUpdate.Id]; connectionToRemove.Dispose(); this.audioPlaybackConnections.Remove(deviceInfoUpdate.Id); } }
public void Disconnect() { if (AudioConnection != null) { AudioConnection.Dispose(); AudioConnection = null; } IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); ActionButtonEnabled = true; Status = Globalization.GetString("BluetoothAudio_Status_1"); OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); }
private async void EnableAudioPlaybackConnectionButton_Click(object sender, RoutedEventArgs e) { if (!(DeviceListView.SelectedItem is null)) { var selectedDeviceId = (DeviceListView.SelectedItem as DeviceInformation).Id; if (!this.audioPlaybackConnections.ContainsKey(selectedDeviceId)) { // Create the audio playback connection from the selected device id and add it to the dictionary. // This will result in allowing incoming connections from the remote device. var playbackConnection = AudioPlaybackConnection.TryCreateFromId(selectedDeviceId); if (playbackConnection != null) { // The device has an available audio playback connection. playbackConnection.StateChanged += this.AudioPlaybackConnection_ConnectionStateChanged; this.audioPlaybackConnections.Add(selectedDeviceId, playbackConnection); await playbackConnection.StartAsync(); OpenAudioPlaybackConnectionButtonButton.IsEnabled = true; } } } }
private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { // Collections bound to the UI are updated in the UI thread. await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Find the device for the given id and remove it from the list. foreach (DeviceInformation device in this.devices) { if (device.Id == deviceInfoUpdate.Id) { this.devices.Remove(device); break; } } if (audioPlaybackConnections.ContainsKey(deviceInfoUpdate.Id)) { AudioPlaybackConnection connectionToRemove = audioPlaybackConnections[deviceInfoUpdate.Id]; connectionToRemove.Dispose(); this.audioPlaybackConnections.Remove(deviceInfoUpdate.Id); } }); }
public async Task ConnectAsync() { try { if (AudioConnection != null) { AudioConnection.Dispose(); AudioConnection = null; } ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); ActionButtonEnabled = false; Status = Globalization.GetString("BluetoothAudio_Status_2"); OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, true); AudioConnection = AudioPlaybackConnection.TryCreateFromId(Id); if (AudioConnection != null) { await AudioConnection.StartAsync(); AudioPlaybackConnectionOpenResult Result = await AudioConnection.OpenAsync(); switch (Result.Status) { case AudioPlaybackConnectionOpenResultStatus.Success: { IsConnected = true; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_2"); Status = Globalization.GetString("BluetoothAudio_Status_3"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); break; } case AudioPlaybackConnectionOpenResultStatus.RequestTimedOut: { IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); Status = Globalization.GetString("BluetoothAudio_Status_4"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); LogTracer.Log("Connect to AudioPlayback failed for time out"); break; } case AudioPlaybackConnectionOpenResultStatus.DeniedBySystem: { IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); Status = Globalization.GetString("BluetoothAudio_Status_5"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); LogTracer.Log("Connect to AudioPlayback failed for being denied by system"); break; } case AudioPlaybackConnectionOpenResultStatus.UnknownFailure: { IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); Status = Globalization.GetString("BluetoothAudio_Status_6"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); if (Result.ExtendedError != null) { LogTracer.Log(Result.ExtendedError, "Connect to AudioPlayback failed for unknown reason"); } else { LogTracer.Log("Connect to AudioPlayback failed for unknown reason"); } break; } } } else { IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); Status = Globalization.GetString("BluetoothAudio_Status_7"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); } } catch (Exception ex) { LogTracer.Log(ex, $"Unable to create a new {nameof(AudioPlaybackConnection)}"); IsConnected = false; ActionButtonText = Globalization.GetString("BluetoothAudio_Button_Text_1"); Status = Globalization.GetString("BluetoothAudio_Status_7"); ActionButtonEnabled = true; OnPropertyChanged(nameof(ActionButtonEnabled)); OnPropertyChanged(nameof(ActionButtonText)); OnPropertyChanged(nameof(Status)); ConnectionStatusChanged?.Invoke(this, false); } }