Member 13339709 Ответов: 1

Как получить все доступные устройства bluetooth в listview C#


Я могу получить имя и адрес близлежащих устройств bluetooth, но я не знаю, как поместить их все в listview, который определен в OnCreate()

Что я уже пробовал:

public class BluetoothDeviceReceiver : BroadcastReceiver
    {
        
        public override void OnReceive(Context context, Intent intent)
        {
            String action = intent.Action;
            var inte = new Intent(context, typeof(availableDevices));

            if (action == BluetoothDevice.ActionFound)
            {
                //Get device            
                BluetoothDevice newDevice = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                inte.PutExtra("Name", newDevice.Name);
                inte.PutExtra("Add", newDevice.Address);
                context.StartActivity(inte);
                
            }

            else if (action != BluetoothDevice.ActionFound)
            {
                Toast.MakeText(context, "No devices found", ToastLength.Long).Show();
            }
        }
    }

//OnCreate
BluetoothDeviceReceiver mReceiver = new BluetoothDeviceReceiver();
            IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);

            ls = FindViewById<ListView>(Resource.Id.listView2);

            tableItems = new List<TableItem>();
            ls.Adapter = new HomeScreenAdapter(this, tableItems);
            TextView tx = FindViewById<TextView>(Resource.Id.textView2);

            Intent discoverableIntent = new Intent(BluetoothAdapter.ActionRequestDiscoverable);
            discoverableIntent.PutExtra(BluetoothAdapter.ExtraDiscoverableDuration, 300);
            StartActivity(discoverableIntent);


            if (!mBluetoothAdapter.IsDiscovering)
            {

                mBluetoothAdapter.StartDiscovery();

            }
            
                RegisterReceiver(mReceiver, filter);
                var name = Intent.GetStringExtra("Name");
                var add = Intent.GetStringExtra("Add");
                tableItems.Add(new TableItem(name, add));

1 Ответов

Рейтинг:
2

David Crow

Что-то вроде:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.isEnabled())
{
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    String str = "";
                
    Iterator<BluetoothDevice> it = devices.iterator();
    while (it.hasNext())
    {
        BluetoothDevice device = it.next();
                    
        if (it.hasNext())
            str += device.getName() + ", ";
    }
}