private void UpdatePairingButtons() { RfcommChatDeviceDisplay deviceDisp = (RfcommChatDeviceDisplay)resultsListView.SelectedItem; if (null != deviceDisp) { ConnectButton.IsEnabled = true; } else { ConnectButton.IsEnabled = false; } }
/// <summary> /// Invoked once the user has selected the device to connect to. /// Once the user has selected the device, /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ConnectButton_Click(object sender, RoutedEventArgs e) { // Make sure user has selected a device first if (resultsListView.SelectedItem != null) { Console.WriteLine("STATUS: Connecting to remote device. Please wait..."); } else { Console.WriteLine("ERROR: Please select an item to connect to"); return; } RfcommChatDeviceDisplay deviceInfoDisp = resultsListView.SelectedItem as RfcommChatDeviceDisplay; // Perform device access checks before trying to get the device. // First, we check if consent has been explicitly denied by the user. DeviceAccessStatus accessStatus = DeviceAccessInformation.CreateFromId(deviceInfoDisp.Id).CurrentStatus; if (accessStatus == DeviceAccessStatus.DeniedByUser) { Console.WriteLine("ERROR: This app does not have access to connect to the remote device (please grant access in Settings > Privacy > Other Devices"); return; } // If not, try to get the Bluetooth device try { _bluetoothDevice = await BluetoothDevice.FromIdAsync(deviceInfoDisp.Id); } catch (Exception ex) { Console.WriteLine("ERROR: " + ex.Message); ResetMainUi(); return; } // If we were unable to get a valid Bluetooth device object, // it's most likely because the user has specified that all unpaired devices // should not be interacted with. if (_bluetoothDevice == null) { Console.WriteLine("ERROR: Bluetooth Device returned null. Access Status = " + accessStatus.ToString()); } // This should return a list of uncached Bluetooth services (so if the server was not active when paired, it will still be detected by this call var rfcommServices = await _bluetoothDevice.GetRfcommServicesForIdAsync( RfcommServiceId.FromUuid(Common.RemoteConstants.RfcommChatServiceUuid), BluetoothCacheMode.Uncached); if (rfcommServices.Services.Count > 0) { _chatService = rfcommServices.Services[0]; } else { Console.WriteLine("ERROR: Could not discover the chat service on the remote device"); ResetMainUi(); return; } // Do various checks of the SDP record to make sure you are talking to a device that actually supports the Bluetooth Rfcomm Chat Service var attributes = await _chatService.GetSdpRawAttributesAsync(); if (!attributes.ContainsKey(Common.RemoteConstants.SdpServiceNameAttributeId)) { Console.WriteLine("ERROR: The Chat service is not advertising the Service Name attribute (attribute id=0x100). " + "Please verify that you are running the BluetoothRfcommChat server."); ResetMainUi(); return; } var attributeReader = DataReader.FromBuffer(attributes[Common.RemoteConstants.SdpServiceNameAttributeId]); var attributeType = attributeReader.ReadByte(); if (attributeType != Common.RemoteConstants.SdpServiceNameAttributeType) { Console.WriteLine("ERROR: The Chat service is using an unexpected format for the Service Name attribute. " + "Please verify that you are running the BluetoothRfcommChat server."); ResetMainUi(); return; } var serviceNameLength = attributeReader.ReadByte(); // The Service Name attribute requires UTF-8 encoding. attributeReader.UnicodeEncoding = UnicodeEncoding.Utf8; StopWatcher(); lock (this) { _chatSocket = new StreamSocket(); } try { await _chatSocket.ConnectAsync(_chatService.ConnectionHostName, _chatService.ConnectionServiceName); SetChatUi(attributeReader.ReadString(serviceNameLength), _bluetoothDevice.Name); _chatWriter = new DataWriter(_chatSocket.OutputStream); DataReader chatReader = new DataReader(_chatSocket.InputStream); ReceiveStringLoop(chatReader); } catch (Exception ex) when((uint)ex.HResult == 0x80070490) // ERROR_ELEMENT_NOT_FOUND { Console.WriteLine("ERROR: Please verify that you are running the BluetoothRfcommChat server."); ResetMainUi(); } catch (Exception ex) when((uint)ex.HResult == 0x80072740) // WSAEADDRINUSE { Console.WriteLine("ERROR: Please verify that there is no other RFCOMM connection to the same device."); ResetMainUi(); } }