Android中发现蓝牙设备列表

Android中发现蓝牙设备列表

本文介绍了在Xamarin.Android中发现蓝牙设备列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中写下了多个论坛和站点,讲述了有关如何发现(配对或未配对的)蓝牙设备的同一件事.

Following multiple forums and sites telling the same thing on how to discover Bluetooth devices (paired or unpaired) I wrote down this piece of code.

class MainActivity: Activity
{
    BluetoothAdapter btAdapter;
    static ArrayAdapter<string> newDevicesArrayAdapter;
    public static List<string> mDeviceList = new List<string>();
    DeviceDiscoveredReceiver receiver;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        receiver = new DeviceDiscoveredReceiver(this);
        IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
        RegisterReceiver(receiver, filter);
        btAdapter = BluetoothAdapter.DefaultAdapter;
    }

    class DeviceDiscoveredReceiver : BroadcastReceiver
    {
        Activity mainActivity;
        public DeviceDiscoveredReceiver(Activity activity)
        {
            this.mainActivity = activity;
        }
        public override void OnReceive(Context context, Intent intent)
        {
            String action = intent.Action;
            if (BluetoothDevice.ActionFound.Equals(action))
            {
                BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                mDeviceList.Add(device.Name + ";" + device.Address);
                var path = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "Download";
                string filename = Path.Combine(path, "myfile.txt");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (StreamWriter objStreamWriter = new StreamWriter(filename, true))
                {
                    objStreamWriter.WriteLine(mDeviceList.Last());
                    objStreamWriter.Close();
                }
            }
        }
    }
}

在这里,Activity类具有一个BroadcastReceiver类,在该类中,任何发现的具有MAC地址的Bluetooth设备的名称都将被记录为.txt文件.我是Xamarin的新手,也是android的新手,这是我感到困惑的地方:

Here, the Activity class has a BroadcastReceiver class where any discovered Bluetooth device's Name with MAC address gets written down to a .txt file. I am very new to Xamarin and new to android and here are the things I am confused with:

  1. 我想知道如何获取包含所有蓝牙的列表设备?
  2. 如何启动获取蓝牙设备"事件?
  3. 我在代码中缺少什么吗?因为当我没有任何反应在我的android智能手机中启动它.
  1. I am wondering how can I get a list containing all the Bluetoothdevices?
  2. How can I start the "get Bluetooth devices" event?
  3. Am I missing something in the codes? because nothing happens when Ilaunch it inside my android smartphone.

我在这里的主要目标是只是开始发现蓝牙设备,但是如何呢?

My main goal here is to just start discovering the Bluetooth devices but how?

推荐答案

好,很简单,但是由于我是新来的,花了一些时间才明白.我错过了OnCreate方法内的SetContentView(Resources.Layout.Main),因此只需将OnCreate方法更改为此:

Ok, it was easy but as I was new, it took some time to get it around my head. I missed the SetContentView(Resources.Layout.Main) inside the OnCreate method so simply change the OnCreate method to this:

 protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resources.Layout.Main); //add this line
    receiver = new DeviceDiscoveredReceiver(this);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
    RegisterReceiver(receiver, filter);
    btAdapter = BluetoothAdapter.DefaultAdapter;
}

这篇关于在Xamarin.Android中发现蓝牙设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:30