public void When_GetSelector() { string testSelector; testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelector()); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromPairingState(true)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromPairingState(false)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Disconnected)); string deviceName = "TESTNAME"; testSelector = _deviceSelectorPrefix + "(System.ItemNameDisplay:=\"" + deviceName + "\" OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromDeviceName(deviceName)); }
async private void RequestIndividualBluetoothBatteryReports() { // Find batteries //var usbDevices = GetUSBDevices(); //Paired bluetooth devices //DeviceInformationCollection pairedBluetoothDevices = // await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true)); //Connected bluetooth devices DeviceInformationCollection connectedBluetoothDevices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)); foreach (DeviceInformation device in connectedBluetoothDevices) { try { string name = device.Name; // Create battery object //Guid batteryLevel = GattCharacteristicUuids.BatteryLevel; var battery = await Battery.FromIdAsync(device.Id); GattDeviceService gattDeviceService = await GattDeviceService.FromIdAsync(device.Id); var batteryLevel = gattDeviceService; // Get report var report = await batteryLevel.GetCharacteristicsAsync(); var results = report.Characteristics.ToString(); // Update UI AddReportUIBluetooth(BatteryReportPanel, results, battery.DeviceId); } catch { /* Add error handling, as applicable */ } } }
/** * start bluetooth test */ private async void button1_Click(object sender, RoutedEventArgs e) { button.IsEnabled = false; button1.IsEnabled = false; var i = textBox.Text; int number = Convert.ToInt32(i); var sucessCnt = 0; var failCnt = 0; var accessLevel = await Radio.RequestAccessAsync(); var radios = await Radio.GetRadiosAsync(); StorageFolder storageFolder = KnownFolders.PicturesLibrary; StorageFile sampleFile = await storageFolder.CreateFileAsync("BTTestLog.txt", CreationCollisionOption.GenerateUniqueName); Radio blueRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth); if (blueRadio == null) { textBlock.Text = "bluetooth cannot open, please turn on the bluetooth first"; button.IsEnabled = true; button1.IsEnabled = true; return; } // wifiRadio.StateChanged = Radio_StateChange textBlock.Text = "0"; textBlock5.Text = "0"; textBlock6.Text = "0"; if (blueRadio.State == RadioState.Off) { await blueRadio.SetStateAsync(RadioState.On); //textBlock.Text = Convert.ToString(count + 1); } for (int count = 0; count < number; count++) { await blueRadio.SetStateAsync(RadioState.Off); await Task.Delay(5000); await blueRadio.SetStateAsync(RadioState.On); textBlock.Text = Convert.ToString(count + 1); await FileIO.AppendTextAsync(sampleFile, DateTime.Now.ToString() + " BT open successfully,test count:" + (count + 1) + "\r\n"); await Task.Delay(20000); var selector = BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected); var btDevices = await DeviceInformation.FindAllAsync(selector); var btDevice = btDevices.FirstOrDefault(); if (btDevice == null) { failCnt++; textBlock6.Text = Convert.ToString(failCnt); await FileIO.AppendTextAsync(sampleFile, DateTime.Now.ToString() + " wifi cannot connect to the Internet,fail count:" + failCnt + "\r\n"); } else { sucessCnt++; textBlock5.Text = Convert.ToString(sucessCnt); await FileIO.AppendTextAsync(sampleFile, DateTime.Now.ToString() + " wifi connected to the Internet,success count:" + sucessCnt + "\r\n"); } } button.IsEnabled = true; button1.IsEnabled = true; }
private void uiCreate_Click(object sender, RoutedEventArgs e) { uiResultMsg.Text = "Creating query..."; bool bBLE = (uiBTtype.SelectedValue as ComboBoxItem).Content.ToString().Contains("LE"); switch ((uiQuery.SelectedValue as ComboBoxItem).Content.ToString()) { case "GetDeviceSelector": if (bBLE) { uiResultMsg.Text = BluetoothLEDevice.GetDeviceSelector(); } else { uiResultMsg.Text = BluetoothDevice.GetDeviceSelector(); } break; case "GetDeviceSelectorFromPairingState(true)": if (bBLE) { uiResultMsg.Text = BluetoothLEDevice.GetDeviceSelectorFromPairingState(true); } else { uiResultMsg.Text = BluetoothDevice.GetDeviceSelectorFromPairingState(true); } break; case "GetDeviceSelectorFromPairingState(false)": if (bBLE) { uiResultMsg.Text = BluetoothLEDevice.GetDeviceSelectorFromPairingState(false); } else { uiResultMsg.Text = BluetoothDevice.GetDeviceSelectorFromPairingState(false); } break; case "GetDeviceSelectorFromConnectionStatus(Connected)": if (bBLE) { uiResultMsg.Text = BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected); } else { uiResultMsg.Text = BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected); } break; case "GetDeviceSelectorFromConnectionStatus(Disconnected)": if (bBLE) { uiResultMsg.Text = BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Disconnected); } else { uiResultMsg.Text = BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Disconnected); } break; default: uiResultMsg.Text = "Unknown value in second ComboBox"; break; } }