private async void Radio_StateChanged(Windows.Devices.Radios.Radio sender, object args)
 {
     // The Radio StateChanged event doesn't run from the UI thread, so we must use the dispatcher
     // to run NotifyPropertyChanged
     await _parent.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         NotifyPropertyChanged("IsRadioOn");
     });
 }
Beispiel #2
0
 private Radio(Windows.Devices.Radios.Radio radio)
 {
     _radio = radio;
 }
        /// <summary>
        /// Mostly experimental at the moment since I cannot find the right ID to get the
        /// correct adapter that holds radio information
        /// </summary>
        /// <param name="device"></param>
        /// <param name="deviceInfo"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private async Task GetRadioInfo(BluetoothDevice device, BTDeviceInfo deviceInfo, string id = "")
        {
            // Cannot get adapter to work as I cannot find an ID to access the remote adaptor object

            try {
                // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter.fromidasync?view=winrt-19041
                // ID consists of registry information
                // 1. MatchingDeviceId
                // 2. MAC address - think this is the RemoteHostName: 20:16:04:07:61:01
                // 3. GUID representing a device class
                // see: https://xspdf.com/questions/39527.shtml
                // DeviceId is Id:Bluetooth#Bluetooth10:08:b1:8a:b0:02-20:16:04:07:61:01
                //this.log.Info("GetExtraInfo", () => string.Format("Default Id:{0}", def.));
                //var dd = device.BluetoothDeviceId;
                ////BluetoothAdapter.GetDefaultAsync();
                //id = dd.Id;
                //id = dd.ToString();


                this.log.Info("****", "----------------- Local BT Device-----------------------------");
                var localAdapter = await BluetoothAdapter.GetDefaultAsync();

                this.log.Info("GetRadioInfo", () => string.Format(" LOCAL ADAPTER ** DeviceId:{0}", localAdapter.DeviceId));
                this.log.Info("GetRadioInfo", () => string.Format(" LOCAL ADAPTER **  Address:{0}", localAdapter.BluetoothAddress));

                this.log.Info("****", "--------------------------------------------------------------");

                // This is local adapter
                // LOCAL ADAPTER** DeviceId:\\?\USB#VID_0CF3&PID_3004#6&32c5d224&0&3#{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}
                //                         "\\\\?\\USB#VID_0CF3&PID_3004#6&32c5d224&0&3#{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}"
                // LOCAL ADAPTER** BluetoothAddress:17629524439042
                // From the Properties object of the DeviceInformation from this object
                //BluetoothRfCommImpl.GetRadioInfo Info
                // Name: MICHAEL - RED - Front
                // Default: False
                // id:\\?\USB#VID_0CF3&PID_3004#6&32c5d224&0&3#{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}
                //------------------ Info--------------------------------------
                //GetDeviceInfo Properties count 8
                //GetDeviceInfo System.ItemNameDisplay - MICHAEL - RED - Front
                //GetDeviceInfo System.Devices.DeviceInstanceId - USB\VID_0CF3 & PID_3004\6 & 32c5d224 & 0 & 3
                //GetDeviceInfo System.Devices.Icon - C:\Windows\System32\DDORes.dll,-2068
                //GetDeviceInfo System.Devices.GlyphIcon - C:\Windows\System32\DDORes.dll,-3001
                //GetDeviceInfo System.Devices.InterfaceEnabled - True
                //GetDeviceInfo System.Devices.IsDefault - False
                //GetDeviceInfo System.Devices.PhysicalDeviceLocation - System.Byte[]
                //GetDeviceInfo System.Devices.ContainerId - 00000000 - 0000 - 0000 - ffff - ffffffffffff
                // ***--------------------------------------------------------------


                // Here is the list from my Arduino test device
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetRadioInfo Info  Name:
                //MikieBtRfCom Default:False id:\\?\BTHENUM#{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&001D#8&D98CD69&0&201604076101_C00000000#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo Properties count 8
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.ItemNameDisplay - MikieBtRfCom
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.DeviceInstanceId - BTHENUM\{ 00001101 - 0000 - 1000 - 8000 - 00805f9b34fb}
                //                _LOCALMFG & 001d\8 & d98cd69 & 0 & 201604076101_C00000000
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.Icon - C:\Windows\System32\DDORes.dll,-2001
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.GlyphIcon - C:\Windows\System32\DDORes.dll,-3001
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.InterfaceEnabled - True
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.IsDefault - False
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.PhysicalDeviceLocation - NA
                //11:59:48 985    I        0  BluetoothRfCommImpl.GetDeviceInfo System.Devices.ContainerId - c1576083 - 3be3 - 5b67 - b161 - b22c54822171



                var infos = await DeviceInformation.FindAllAsync();

                foreach (var i in infos)
                {
                    //this.log.Info("****", "\n------------------ Info --------------------------------------");
                    //this.log.Info("GetRadioInfo", () => string.Format(
                    //    "    Info  Name:{0} Default:{1} id:{2}", i.Name, i.IsDefault, i.Id));
                    //this.ListProperties(i.Properties);
                    //this.log.Info("****", "--------------------------------------------------------------");
                    if (i.Name == deviceInfo.Name)
                    {
                        this.log.Info("GetRadioInfo", () => string.Format(
                                          "    Info  Name:{0} Default:{1} id:{2}", i.Name, i.IsDefault, i.Id));
                        this.ListProperties(i.Properties);
                        id = i.Id;
                        // The id is
                        // \\?\BTHENUM#{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&001D#8&D98CD69&0&201604076101_C00000000#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
                        //                    CLASS ID (1101) Serial Port
                        break;
                    }
                }

                // Try to do search on the ID from locl - THIS WORKS SO THE FORMAT IS OK
                //id = localAdapter.DeviceId;
                // \\?\USB#VID_0CF3&PID_3004#6&32c5d224&0&3#{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}
                // \\?\USB  #VID_0CF3&PID_3004  #6&32c5d224&0&3  #{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}
                //            Device id         | MAC            | class id?
                // From the Device manager the MAX is 10:08:b1:8a: b0: 02

                // \\?\USB  #VID_0CF3&PID_3004  #6&32c5d224&0&3  #{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}



                // The arduino string returned is

                // RemoteHostName =                  (20:16:04:07:61:01)
                // in the ID:       10:08:b1:8a:b0:02-20:16:04:07:61:01
                // Quick hack - cannot find specified file. Same Exception as get radio - does not get the element not found so the format may be correct
                // MAC
                //  10:08:b1:8a:b0:02 -  The system cannot find the file specified. (0x80070002)
                //  20:16:04:07:61:01 -  The system cannot find the file specified. (0x80070002)
                // (20:16:04:07:61:01) - The system cannot find the file specified. (0x80070002)  // RemoteHostName
                //id = string.Format(@"\\?\BTHENUM#{0}#{1}",
                //    deviceInfo.RemoteHostName,
                //    "{00001101-0000-1000-8000-00805F9B34FB}");
                //** Id:\\?\BTHENUM#(20:16:04:07:61:01)#{00001101-0000-1000-8000-00805F9B34FB}

                //  Local Id: \\?\USB#VID_0CF3&PID_3004#6&32c5d224&0&3#{92383b0e-f90e-4ac9-8d44-8c2d0d0ebda2}
                // Remote Id: \\?\BTHENUM#{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&001D#8&D98CD69&0&201604076101_C00000000#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
                // Remote Device instance Id: BTHENUM\{00001101-0000-1000-8000-00805f9b34fb}_LOCALMFG&001d\8&d98cd69&0&201604076101_C00000000

                //id = @"\\?\BTHENUM# {00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&001D# 8&D98CD69&0&201604076101_C00000000# {86e0d1e0-8089-11d0-9ce4-08003e301f73}";
                //id = @"\\?\BTHENUM#8&D98CD69&0&201604076101_C00000000#{00001101-0000-1000-8000-00805F9B34FB}";
                //id = @"\\?\BTHENUM#{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&001D# 8&D98CD69&0&201604076101_C00000000# {86e0d1e0-8089-11d0-9ce4-08003e301f73}";


                // From the MS Doc
                // The DeviceId value that identifies the BluetoothAdapter instance. This is a
                // composite string combining registry information that includes
                // 1.the MatchingDeviceId,
                // 2. the MAC address,
                // 3. and a GUID representing a device class.
                //This is different than Windows.Devices.Enumeration.DeviceInformation.Id.However, both contain the MAC address of the Bluetooth radio device embedded within the identifier string.


                // From Device Manager - Arduino device info

                /*
                 * Device BTHENUM\Dev_201604076101\8 & 28fa0f8c & 0 & BluetoothDevice_201604076101 was configured.
                 *
                 * Driver Name: bth.inf
                 * Class Guid:
                 *  { e0cbf06c - cd8b - 4647 - bb8a - 263b43f0f974}
                 *  Driver Date: 06 / 21 / 2006
                 *  Driver Version: 10.0.19041.488
                 *  Driver Provider: Microsoft
                 *  Driver Section:
                 *                  BthGenericDevice.NT
                 *  Driver Rank:
                 *                  0xFF2000
                 *  Matching Device Id:
                 *                  BTHENUM\GENERIC_DEVICE
                 *  Outranked Drivers:
                 *                  Device Updated: false
                 *  Parent Device: BTH\MS_BTHBRB\7 & dcffc4e & 21 & 1
                 */
                id = string.Format(@"\\?\BTHENUM#{0}#{1}",
                                   deviceInfo.RemoteHostName,
                                   "{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}");


                id = @"\\?\BTHENUM\Dev_201604076101\8&28fa0f8c&0&BluetoothDevice_201604076101";



                this.log.Info("GetRadioInfo", () => string.Format(" ** Id:{0}", id));
                // This throws an exception: "Element not found"
                var adapter = await BluetoothAdapter.FromIdAsync(id);



                if (adapter != null)
                {
                    this.log.Info("GetExtraInfo", () => string.Format("Adapter"));
                    this.log.Info("GetExtraInfo", () => string.Format("Advertisement Offload {0}", adapter.IsAdvertisementOffloadSupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Central Role {0}", adapter.IsCentralRoleSupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Classic {0}", adapter.IsClassicSupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Extended Advertisement {0}", adapter.IsExtendedAdvertisingSupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Low Energy {0}", adapter.IsLowEnergySupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Peripheral Role {0}", adapter.IsPeripheralRoleSupported));
                    this.log.Info("GetExtraInfo", () => string.Format("Max advertisement length {0}", adapter.MaxAdvertisementDataLength));

                    Windows.Devices.Radios.Radio radio = await adapter.GetRadioAsync();

                    if (radio != null)
                    {
                        //radio.Kind

                        deviceInfo.Radio.Manufacturer = radio.Name;
                    }
                }
            }
            catch (Exception e) {
                this.log.Exception(9999, "", e);
            }
        }
 public Radio(Windows.Devices.Radios.Radio radio, UIElement parent)
 {
     _radio               = radio;
     _parent              = parent;
     _radio.StateChanged += Radio_StateChanged;
 }
Beispiel #5
0
        private static Radio?GetRadiosBluetooth()
        {
            var context = Android.App.Application.Context;

            var pkgManager = context.PackageManager;

            if (pkgManager is null)
            {
                // required by #nullable
                return(null);
            }

            bool hasBluetooth   = pkgManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureBluetooth);
            bool hasBluetoothLE = pkgManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureBluetoothLe);

            if (!(hasBluetooth || hasBluetoothLE))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    typeof(Radio).Log().Debug("GetRadiosBluetooth(): this device doesn't have any Bluetooth interface");
                }
                return(null);
            }

            var radio = new Radio();

            radio.Kind = RadioKind.Bluetooth;
            radio.Name = "Bluetooth";              // name as in UWP

            if (!Windows.Extensions.PermissionsHelper.IsDeclaredInManifest(Android.Manifest.Permission.Bluetooth))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosBluetooth(): no 'Bluetooth' permission, check app Manifest");
                }
                radio.State = RadioState.Unknown;
                return(radio);
            }

            if (Android.OS.Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.JellyBeanMr1)
            {
                // deprecated in API 31
#pragma warning disable CS0618 // Type or member is obsolete
                var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter;
#pragma warning restore CS0618 // Type or member is obsolete
                if (adapter == null)
                {
                    if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                    {
                        typeof(Radio).Log().Debug("GetRadiosBluetooth(): should not happen: Android inconsistence, device has Bluetooth capability but no Bluetooth adapter (old API)");
                    }
                    return(null);
                }
                else if (!adapter.IsEnabled)
                {
                    radio.State = RadioState.Off;
                }
                else
                {
                    radio.State = RadioState.On;
                }

                return(radio);
            }
            else
            {
                if (Android.App.Application.Context.GetSystemService(Android.Content.Context.BluetoothService) is Android.Bluetooth.BluetoothManager bluetoothManager)
                {
                    var adapter = bluetoothManager.Adapter;
                    if (adapter == null)
                    {
                        if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                        {
                            typeof(Radio).Log().Debug("GetRadiosBluetooth(): should not happen: Android inconsistence, device has Bluetooth capability but no Bluetooth adapter (new API)");
                        }
                        return(null);
                    }
                    if (adapter.State != Android.Bluetooth.State.On)
                    {
                        radio.State = RadioState.Off;
                    }
                    else
                    {
                        radio.State = RadioState.On;
                    }

                    return(radio);
                }
                else
                {
                    if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                    {
                        typeof(Radio).Log().Debug("GetRadiosBluetooth(): should not happen: Android inconsistence, device has Bluetooth capability but no BluetoothService (new API)");
                    }
                    return(null);
                }
            }
        }
Beispiel #6
0
        private static Radio?GetRadiosWiFiOrCellular(RadioKind radioKind)
        {
            var context = Android.App.Application.Context;

            var pkgManager = context.PackageManager;

            if (pkgManager is null)
            {
                // required by #nullable
                return(null);
            }

            string systemFeature;

            if (radioKind == RadioKind.WiFi)
            {
                systemFeature = Android.Content.PM.PackageManager.FeatureWifi;
            }
            else
            {
                systemFeature = Android.Content.PM.PackageManager.FeatureTelephony;
            }

            if (!pkgManager.HasSystemFeature(systemFeature))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): this device doesn't have any interface of this kind");
                }
                return(null);
            }

            var radio = new Radio();

            radio.Kind = radioKind;
            if (radioKind == RadioKind.WiFi)
            {
                radio.Name = "Wi-Fi";                  // name as in UWP
            }
            else
            {
                radio.Name = "Cellular";                  // I have "Cellular 6" (Lumia 532), but maybe "6" doesn't mean anything
            }

            if (!Windows.Extensions.PermissionsHelper.IsDeclaredInManifest(Android.Manifest.Permission.AccessNetworkState))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): no 'AccessNetworkState' permission, check app Manifest");
                }
                radio.State = RadioState.Unknown;
                return(radio);
            }

            var sysService = context.GetSystemService(Android.Content.Context.ConnectivityService);

            if (sysService is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): GetSystemService returns null, assuming no radio");
                }
                return(null);
            }

            var connManager = (Android.Net.ConnectivityManager)sysService;

            if (connManager is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): Android.Net.ConnectivityManager is null, assuming no WiFi radio");
                }
                return(null);
            }

            if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.LollipopMr1)
            {
                // since API 23
                var radiostate = GetRadioStateForNet(connManager, radioKind);
                if (radiostate is null || !radiostate.HasValue)
                {
                    // detailed Log entry already done in GetRadioStateForNet
                    return(null);
                }

                if (radiostate.Value)
                {
                    radio.State = RadioState.On;
                }
                else
                {
                    radio.State = RadioState.Off;
                }

                return(radio);
            }
            else
            {
                // for Android API 1 to 28 (deprecated in 29)
#pragma warning disable CS0618 // Type or member is obsolete
                var netInfo = connManager.ActiveNetworkInfo;
                if (netInfo is null)
                {
                    radio.State = RadioState.Off;
                }
                else
                {
                    radio.State = RadioState.Off;
                    if (radioKind == RadioKind.WiFi)
                    {
                        if (netInfo.Type == Android.Net.ConnectivityType.Wifi ||
                            netInfo.Type == Android.Net.ConnectivityType.Wimax)
                        {
                            radio.State = RadioState.On;
                        }
                    }
                    else
                    {
                        if (netInfo.Type == Android.Net.ConnectivityType.Mobile)
                        {
                            radio.State = RadioState.On;
                        }
                    }
#pragma warning restore CS0618 // Type or member is obsolete
                }
                return(radio);
            }
        }