Ejemplo n.º 1
0
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    string action = intent.Action;

                    // When discovery finds a device
                    if (action == BluetoothDevice.ActionFound)
                    {
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                        // If it's already paired, skip it, because it's been listed already
                        if (device.BondState != Bond.Bonded)
                        {
                            ParcelUuid[] uuids = device.GetUuids();
                            if ((uuids == null) || (uuids.Any(uuid => SppUuid.CompareTo(uuid.Uuid) == 0)))
                            {
                                _newDevicesArrayAdapter.Add(device.Name + "\n" + device.Address);
                            }
                        }
                        // When discovery is finished, change the Activity title
                    }
                    else if (action == BluetoothAdapter.ActionDiscoveryFinished)
                    {
                        _chat._scanButton.Enabled = true;
                        //_chat.SetProgressBarIndeterminateVisibility (false);
                        _chat.FindViewById <ProgressBar>(Resource.Id.progress_bar).Visibility = ViewStates.Invisible;
                        _chat.SetTitle(Resource.String.select_device);
                        if (_newDevicesArrayAdapter.Count == 0)
                        {
                            var noDevices = _chat.Resources.GetText(Resource.String.none_found);
                            _newDevicesArrayAdapter.Add(noDevices);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
Ejemplo n.º 2
0
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    string action = intent.Action;

                    switch (action)
                    {
                    case BluetoothDevice.ActionFound:
                    case BluetoothDevice.ActionNameChanged:
                    {
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                        // If it's already paired, skip it, because it's been listed already
                        if (device.BondState != Bond.Bonded)
                        {
                            ParcelUuid[] uuids = device.GetUuids();
                            if ((uuids == null) || (uuids.Any(uuid => SppUuid.CompareTo(uuid.Uuid) == 0)))
                            {
                                // check for multiple entries
                                int index = -1;
                                for (int i = 0; i < _newDevicesArrayAdapter.Count; i++)
                                {
                                    string item = _newDevicesArrayAdapter.GetItem(i);
                                    if (!ExtractDeviceInfo(_newDevicesArrayAdapter.GetItem(i), out string _, out string address))
                                    {
                                        return;
                                    }
                                    if (string.Compare(address, device.Address, StringComparison.OrdinalIgnoreCase) == 0)
                                    {
                                        _newDevicesArrayAdapter.Remove(item);
                                        index = i;
                                        break;
                                    }
                                }
                                string newName = device.Name + "\n" + device.Address;
                                if (index < 0)
                                {
                                    _newDevicesArrayAdapter.Add(newName);
                                }
                                else
                                {
                                    _newDevicesArrayAdapter.Insert(newName, index);
                                }
                            }
                        }
                        break;
                    }

                    case BluetoothAdapter.ActionDiscoveryFinished:
                        // When discovery is finished, change the Activity title
                        _chat._scanButton.Enabled = true;
                        //_chat.SetProgressBarIndeterminateVisibility (false);
                        _chat.FindViewById <ProgressBar>(Resource.Id.progress_bar).Visibility = ViewStates.Invisible;
                        _chat.SetTitle(Resource.String.select_device);
                        if (_newDevicesArrayAdapter.Count == 0)
                        {
                            var noDevices = _chat.Resources.GetText(Resource.String.none_found);
                            _newDevicesArrayAdapter.Add(noDevices);
                        }
                        break;
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }