简体   繁体   中英

Discovering list of Bluetooth devices in 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();
                }
            }
        }
    }
}

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. I am wondering how can I get a list containing all the Bluetooth devices?
  2. How can I start the "get Bluetooth devices" event?
  3. Am I missing something in the codes? because nothing happens when I launch it inside my android smartphone.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM