简体   繁体   English

找不到蓝牙设备Xamarin Android

[英]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. 我的问题是OnReceive方法永远不会触发,而且我真的不知道为什么它不起作用。

I'm using Android API 23 and I'm testing my code on a Huawei Nexus 6P running on android 6.0.1 我正在使用Android API 23,并在运行Android 6.0.1的Huawei Nexus 6P上测试代码

You need to add BroadcastReceiver attribute to your receiver. 您需要向您的接收器添加BroadcastReceiver属性。 In Android 6 you also need to request ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions at runtime: Request permission 在Android 6中,您还需要在运行时请求ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限: 请求权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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