示例#1
0
        public static IBluetoothDevice GetIDevice(this BluetoothDevice bluetoothDevice)
        {
            System.Diagnostics.Debug.WriteLine("-" + bluetoothDevice.Name + " " + bluetoothDevice.Address);
            var device = new AndroidBluetoothDevice()
            {
                Name = bluetoothDevice.Name, Address = bluetoothDevice.Address
            };

            device.BluetoothDevice = bluetoothDevice;
            try
            {
                switch (bluetoothDevice.Type)
                {
                case global::Android.Bluetooth.BluetoothDeviceType.Classic:
                    device.Type = BluetoothDeviceType.Classic;
                    break;

                case global::Android.Bluetooth.BluetoothDeviceType.Dual:
                    device.Type = BluetoothDeviceType.Dual;
                    break;

                case global::Android.Bluetooth.BluetoothDeviceType.Le:
                    device.Type = BluetoothDeviceType.Le;
                    break;

                case global::Android.Bluetooth.BluetoothDeviceType.Unknown:
                    device.Type = BluetoothDeviceType.Unknown;
                    break;
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                System.Diagnostics.Debug.WriteLine("Bluetooth getting Uuids with SDP");
                device.BluetoothDevice.FetchUuidsWithSdp();

                System.Diagnostics.Debug.WriteLine("Bluetooth Android: Getting UUIDs");
                var array = bluetoothDevice.GetUuids();
                if (array != null)
                {
                    var uuids = array.ToList();

                    System.Diagnostics.Debug.WriteLine("Bluetooth Android: " + uuids.Count + " found");

                    foreach (var uuid in uuids)
                    {
                        System.Diagnostics.Debug.WriteLine(device.Name + " Adding uuid " + uuid.Uuid.ToString());
                        var stringUUID = uuid.ToString();
                        device.UniqueIdentifiers.Add(Guid.Parse(stringUUID));
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Bluetooth Android: No Paired devices");
                }
            }
            catch (Java.Lang.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return(device);
        }
        public override async Task <List <IBluetoothDevice> > GetPairedDevices()
        {
            var devices = new List <IBluetoothDevice>();

            try
            {
                // Get a set of currently paired devices
                var pairedDevices = btAdapter.BondedDevices;

                if (btAdapter.BondedDevices == null)
                {
                }
                else
                {
                    if (btAdapter.IsDiscovering)
                    {
                        btAdapter.CancelDiscovery();
                    }

                    // If there are paired devices, add each one to the ArrayAdapter
                    if (pairedDevices.Count > 0)
                    {
                        foreach (var paireddevice in pairedDevices)
                        {
                            var device = new AndroidBluetoothDevice()
                            {
                                Name = paireddevice.Name, Address = paireddevice.Address
                            };
                            device.BluetoothDevice = paireddevice;


                            try
                            {
                                device.BluetoothDevice.FetchUuidsWithSdp();

                                var array = paireddevice.GetUuids();
                                if (array == null)
                                {
                                    array    = new ParcelUuid[1];
                                    array[0] = new ParcelUuid(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                                }
                                if (array != null)
                                {
                                    var uuids = array.ToList();

                                    foreach (var uuid in uuids)
                                    {
                                        System.Diagnostics.Debug.WriteLine(device.Name + " Adding uuid " + uuid.Uuid.ToString());
                                        var stringUUID = uuid.ToString();
                                        device.UniqueIdentifiers.Add(Guid.Parse(stringUUID));
                                    }
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Bluetooth Android: No Paired devices");
                                }
                            }
                            catch (Java.Lang.Exception ex)
                            {
                                System.Diagnostics.Debug.WriteLine(ex.Message);
                            }

                            devices.Add(device);
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Bluetooth Android: No devices found");
                    }
                }
            }
            catch (Java.Lang.Exception ex)
            {
                throw new Exception(ex.Message);
            }


            return(devices);
        }