private void UpdatePairingButtons() { RfcommDeviceDisplay deviceDisp = (RfcommDeviceDisplay)resultsListView.SelectedItem; if (null != deviceDisp) { ConnectButton.IsEnabled = true; } else { ConnectButton.IsEnabled = false; } }
public async void Connect(RfcommDeviceDisplay deviceInfoDisp) { try { bluetoothDevice = await BluetoothDevice.FromIdAsync(deviceInfoDisp.Id); } catch { //rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage); 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. // 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 string uuid = "17fcf242-f86d-4e35-805e-" + Constants.BLUETOOTH_ID; Guid RfcommChatServiceUuid = Guid.Parse(uuid); var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync( RfcommServiceId.FromUuid(RfcommChatServiceUuid), BluetoothCacheMode.Uncached); if (rfcommServices.Services.Count > 0) { ConnectService = rfcommServices.Services[0]; } else { 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 UInt16 SdpServiceNameAttributeId = 0x100; var attributes = await ConnectService.GetSdpRawAttributesAsync(); if (!attributes.ContainsKey(SdpServiceNameAttributeId)) { MessageBox.Show("sdpAttributeがおかしい"); return; } byte SdpServiceNameAttributeType = (4 << 3) | 5; var attributeReader = DataReader.FromBuffer(attributes[SdpServiceNameAttributeId]); var attributeType = attributeReader.ReadByte(); if (attributeType != SdpServiceNameAttributeType) { MessageBox.Show("sdpNameAttributeがおかしい"); return; } var serviceNameLength = attributeReader.ReadByte(); lock (this) //lock構文、排他制御 { ConnectSocket = new StreamSocket(); } try { await ConnectSocket.ConnectAsync(ConnectService.ConnectionHostName, ConnectService.ConnectionServiceName); } catch (Exception ex) when((uint)ex.HResult == 0x80072740) // WSAEADDRINUSE { MessageBox.Show("socket接続がおかしい"); } Console.WriteLine("Connect"); main.ComState.Text = "演奏開始待機中"; main.ConnectDevice.Text = deviceInfoDisp.Name.ToString(); Start(main); }