Creends
Creends

Reputation: 37

Can't discover bluetooth devices Xamarin Android

I want to find all available bluetooth devices(not only bonded devices). So I wrote this code :

using System;
using Android.App;
using Android.Widget;
using Snackbar = Android.Support.Design.Widget.Snackbar;
using v7 = Android.Support.V7.App;
using Android.OS;
using Android.Views;
using Android.Bluetooth;
using Android.Content;

namespace Morpion
{
    [Activity(Label = "HomeActivity")]
    public class HomeActivity : Activity
    {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
        static TextView connectionStateText;
        bool connected = false;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Home);
            Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

            var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
            SetActionBar(toolbar);
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.OptionMenu, menu);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            RelativeLayout layout = FindViewById<RelativeLayout>(Resource.Id.homeLayout);
            connectionStateText = FindViewById<TextView>(Resource.Id.connectionStateText);
            ProgressBar connectionBar = FindViewById<ProgressBar>(Resource.Id.connectionBar);

            if (item.ItemId == Resource.Id.about)
            {
                var about = new v7.AlertDialog.Builder(this);
                about.SetTitle("About");
                about.SetMessage(Resource.String.about);
                about.SetNeutralButton("Close", delegate { });
                about.Show();
            }
            else if (item.ItemId == Resource.Id.connect)
            {
                if (!connected && !bluetoothAdapter.IsEnabled)
                {
                    Snackbar.Make(layout, "Please activate bluetooth", Snackbar.LengthLong)
                            .SetAction("Activate", delegate
                            {
                                bluetoothAdapter.Enable();
                            })
                            .Show();
                }
                else if (!connected)
                {
                    connectionStateText.Text = "Connection";
                    connectionBar.Indeterminate = true;
                    connectionBar.Visibility = ViewStates.Visible;
                    System.Collections.Generic.ICollection<BluetoothDevice> devices = bluetoothAdapter.BondedDevices;

                    var filter = new IntentFilter(BluetoothDevice.ActionFound);
                    var receiver = new Receiver();
                    if (bluetoothAdapter.IsDiscovering)
                    {
                        bluetoothAdapter.CancelDiscovery();
                    }
                    RegisterReceiver(receiver, filter);
                    bluetoothAdapter.StartDiscovery();
                }
                else
                {
                    connectionStateText.Text = "Not connected";
                    item.SetTitle("Connect");
                    connected = false;
                }
            }
            return base.OnOptionsItemSelected(item);
        }

        public class Receiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                string action = intent.Action;

                if (action == BluetoothDevice.ActionFound)
                {
                    Console.WriteLine("found");
                }
            }
        }
    }
}

My problem is that the OnReceive method never fire and I really dont know why it doesn't work.

I'm using Android API 23 and I'm testing my code on a Huawei Nexus 6P running on android 6.0.1

Upvotes: 0

Views: 2343

Answers (1)

Giorgi
Giorgi

Reputation: 30893

You need to add BroadcastReceiver attribute to your receiver. In Android 6 you also need to request ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions at runtime: Request permission

Upvotes: 3

Related Questions