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 (ResultCollection.Count != 0) //.SelectedItem != null) { UpdateStatus("Connecting to remote device. Please wait...", NotifyType.StatusMessage); } else { UpdateStatus("Please select an item to connect to", NotifyType.ErrorMessage); return; } var coll = ResultCollection; //var xx = resultsListView.SelectedItem; RfcommChatDeviceDisplay deviceInfoDisp = ResultCollection[0]; // 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) { UpdateStatus("This app does not have access to connect to the remote device (please grant access in Settings > Privacy > Other Devices", NotifyType.ErrorMessage); return; } // If not, try to get the Bluetooth device try { bluetoothDevice = await BluetoothDevice.FromIdAsync(deviceInfoDisp.Id); } catch (Exception ex) { UpdateStatus(ex.Message, NotifyType.ErrorMessage); //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) { UpdateStatus("Bluetooth Device returned null. Access Status = " + accessStatus.ToString(), NotifyType.ErrorMessage); return; } // 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(Constants.RfcommChatServiceUuid), BluetoothCacheMode.Uncached); if (rfcommServices.Services.Count > 0) { chatService = rfcommServices.Services[0]; } else { UpdateStatus( "Could not discover the chat service on the remote device", NotifyType.StatusMessage); //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(Constants.SdpServiceNameAttributeId)) { UpdateStatus( "The Chat service is not advertising the Service Name attribute (attribute id=0x100). " + "Please verify that you are running the BluetoothRfcommChat server.", NotifyType.ErrorMessage); //ResetMainUI(); return; } var attributeReader = DataReader.FromBuffer(attributes[Constants.SdpServiceNameAttributeId]); var attributeType = attributeReader.ReadByte(); if (attributeType != Constants.SdpServiceNameAttributeType) { UpdateStatus( "The Chat service is using an unexpected format for the Service Name attribute. " + "Please verify that you are running the BluetoothRfcommChat server.", NotifyType.ErrorMessage); //ResetMainUI(); return; } var serviceNameLength = attributeReader.ReadByte(); // The Service Name attribute requires UTF-8 encoding. attributeReader.UnicodeEncoding = UnicodeEncoding.Utf8; 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); SendCh('@'); _Mode = Mode.Connected; UpdateStatus( "Chatting now.", NotifyType.StatusMessage); StopWatcher(); recvdtxt = ""; listening = true; } catch (Exception ex) when((uint)ex.HResult == 0x80070490) // ERROR_ELEMENT_NOT_FOUND { UpdateStatus("Please verify that you are running the BluetoothRfcommChat server.", NotifyType.ErrorMessage); //ResetMainUI(); return; } catch (Exception ex) when((uint)ex.HResult == 0x80072740) // WSAEADDRINUSE { UpdateStatus("Please verify that there is no other RFCOMM connection to the same device.", NotifyType.ErrorMessage); //ResetMainUI(); return; } }