无法发现蓝牙设备Xamarin Android

时间:2016-04-13 17:04:59

标签: android xamarin bluetooth

我想找到所有可用的蓝牙设备(不仅仅是绑定设备)。 所以我写了这段代码:

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

我的问题是OnReceive方法永远不会触发,我真的不知道它为什么不起作用。

我正在使用Android API 23,我正在使用在Android 6.0.1上运行的华为Nexus 6P上测试我的代码

1 个答案:

答案 0 :(得分:3)

您需要向接收者添加BroadcastReceiver属性。在Android 6中,您还需要在运行时请求ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限:Request permission