如何在列表视图C#中获取所有可用的蓝牙设备 [英] How do I get all the available bluetooth devices in a listview C#

查看:440
本文介绍了如何在列表视图C#中获取所有可用的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够获取附近蓝牙设备的名称和地址,但我不知道如何将它们全部放入OnCreate中定义的列表视图中()



我尝试过:



I'm able to get the name and the address of nearby bluetooth devices but I don't know how to put them all in the listview that is defined in the OnCreate()

What I have tried:

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));

推荐答案

类似于:

Something like:
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() + ", ";
    }
}


这篇关于如何在列表视图C#中获取所有可用的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆