发现附近没有显示任何内容的蓝牙设备

时间:2019-01-14 17:48:59

标签: c# bluetooth xamarin.android prism dryioc

我是Xamarin的新手,作为一项初学者任务,我要求显示附近活动的蓝牙设备的列表(仅在 Android 中)。不使用任何NuGet软件包(棱镜除外)

我关注了this manual,还浏览了here作为参考 在我的OnReceive函数中,我确实得到了BluetoothAdapter.ActionDiscoveryStartedBluetoothAdapter.ActionDiscoveryFinished但没有得到BluetoothDevice.ActionFound

我使用 dryioc容器

启动了一个新的棱镜模板应用

我正在手机OnePlus A6003(Android 9.0-API 28)上运行该应用程序,并且我有2个可以连接蓝牙设备的设备,一个在旧手机和笔记本电脑上(Windows 10),我都检查了它们如果他们可以配对到我的设备,并且一切正常

我添加的AndroidManifest.xml中的

  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我的BroadcastReceiver类

public class BluetoothDeviceReceiver : BroadcastReceiver
    {
        public static BluetoothAdapter Adapter => BluetoothAdapter.DefaultAdapter;

        public override void OnReceive(Context context, Intent intent)
        {
            var action = intent.Action;
            // Found a device
            switch (action)
            {
                case BluetoothDevice.ActionFound:
                    // Get the device
                    var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);

                    // Only update the adapter with items which are not bonded
                    if (device.BondState != Bond.Bonded)
                    {                       
                    }
                    break;
                case BluetoothAdapter.ActionDiscoveryStarted:

                    break;
                case BluetoothAdapter.ActionDiscoveryFinished:

                    break;
                default:
                    break;
            }
        }
    }

和我的mainActivity

 protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            var coarseLocationPermissionGranted =
                 ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation);
            var fineLocationPermissionGranted =
                ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation);

            if (coarseLocationPermissionGranted != Permission.Denied ||
                fineLocationPermissionGranted == Permission.Denied)
                ActivityCompat.RequestPermissions(this, LocationPermissions, LocationPermissionsRequestCode);

            // Register for broadcasts when a device is discovered
            _receiver = new BluetoothDeviceReceiver();          

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App(new AndroidInitializer()));
        }


        private void RegisterBluetoothReceiver()
        {
            if (_isReceiveredRegistered) return;

            RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionFound));
            RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryStarted));
            RegisterReceiver(_receiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished));
            _isReceiveredRegistered = true;
        }

        private static void StartScanning()
        {
            if (!BluetoothDeviceReceiver.Adapter.IsDiscovering)
                BluetoothDeviceReceiver.Adapter.StartDiscovery();
        }

        protected override void OnResume()
        {
            base.OnResume();

            StartScanning();

            // Register broadcast listeners
            RegisterBluetoothReceiver();
        }

我想念什么? 重新启动手机或其他设备不起作用

1 个答案:

答案 0 :(得分:0)

在您的代码中,您已经在permission中设置了AndroidManifest.xml。也许问题还在于权限,有两种选择尝试的方法:

一个很简单,只需将targetSdkVersion设置为小于23。

两个,需要像OnCreate()方法中那样动态添加权限:

if (Build.VERSION.SdkInt >= Build.VERSION_CODES.M)
            {
                if (this.CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != Permission.Granted)
                {
                    RequestPermissions(new String[] { Manifest.Permission.AccessCoarseLocation }, 1);
                }
            }

并要求执行您需要做的事情:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
    {
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode)
        {
            case 1:
                // todo request success  
                break;
            default:
                break;
        }


    }