using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.GenericAttributeProfile; using System.Diagnostics; using System.Threading.Tasks; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace App7 { /// /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } BluetoothLEDevice bluetoothLeDevice1 = null; BluetoothLEDevice bluetoothLeDevice2 = null; private async void Button1_Click(object sender, RoutedEventArgs e) { bluetoothLeDevice1?.Dispose(); bluetoothLeDevice1 = null; await ConnectExplore(bluetoothLeDevice1, "90:fd:9f:6e:14:81"); } private async void Button2_Click(object sender, RoutedEventArgs e) { bluetoothLeDevice2?.Dispose(); bluetoothLeDevice2 = null; await ConnectExplore(bluetoothLeDevice2, "90:fd:9f:4c:b0:c6"); } async Task ConnectExplore(BluetoothLEDevice bleDevice, string macAddress) { string mac_addr_clean = macAddress.Replace(":", ""); ulong mac_addr_num = Convert.ToUInt64(mac_addr_clean, 16); bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(mac_addr_num); if (bleDevice != null) { Debug.WriteLine($"{DateTime.Now:HH:mm:ss.f} -> {macAddress} \t{bleDevice.ConnectionStatus} \t{bleDevice.DeviceAccessInformation.CurrentStatus} \t{bleDevice.BluetoothDeviceId.Id}"); var gattResult = await bleDevice.GetGattServicesAsync(); Debug.WriteLine($"{DateTime.Now:HH:mm:ss.f} -> {macAddress} bleDevice.GetGattServicesAsync gattResult {gattResult.Status}"); if (gattResult.Status == GattCommunicationStatus.Success) { foreach (var service in gattResult.Services) { Debug.WriteLine("service uuid: " + service.Uuid.ToString()); } } } else { Debug.WriteLine($"{DateTime.Now} -> {macAddress} FromBluetoothAddressAsync return null"); } } } }