蓝牙LE无法检测到设备

时间:2018-10-13 06:44:31

标签: xamarin xamarin.forms bluetooth xamarin.android bluetooth-lowenergy

我尝试使用Bluetooth LE检测设备,该操作与该库中针对Xamarin所做的一样,但设备发现0

包含了Android中的必要清单。将扫描时间增加到30秒,但是什么也没有。启用了蓝牙,版本4.0

Asus_Z00ED设备(Android 6.0-API 23)。该项目的创建目标是Android 8.1。试图在清单中切换Android(8.0,9.0)的目标版本,但也没有检测到

缺少检测还有什么原因?

public class Page6 : ContentPage
    {
        IBluetoothLE ble;
        IAdapter adapter;
        ObservableCollection<IDevice> deviceList;
        Label label;

        public Page6()
        {
            ble = CrossBluetoothLE.Current;
            adapter = CrossBluetoothLE.Current.Adapter;
            deviceList = new ObservableCollection<IDevice>();

            label = new Label { Text = "Welcome to Xamarin.Forms!" };
            var buttonList = new Button() { Text = "Devices List" };
            buttonList.Clicked += ButtonList_Clicked;

            Content = new StackLayout
            {
                Children = {
                    label,
                    buttonList,
                }
            };
        }

        private async void ButtonList_Clicked(object sender, EventArgs e)
        {
            deviceList.Clear();
            adapter.ScanTimeout = 30000;
            adapter.DeviceDiscovered += (s, a) =>
            {
                deviceList.Add(a.Device);
            };

            await adapter.StartScanningForDevicesAsync(); // сканирование

            label.Text = deviceList.Count.ToString();
        }
    }

enter image description here

3 个答案:

答案 0 :(得分:0)

正如Emil所说,您必须添加明确的许可请求。请查看以下链接:

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=vswin

答案 1 :(得分:0)

https://stackoverflow.com/a/44471774/3603237

根据对本机Android问题的此答案,您必须启用位置服务才能进行扫描。为我解决了这个问题。

另外,请参阅此文章,了解Android 7中更多未记录的更改: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983

答案 2 :(得分:0)

从您的AndroidManifest.xml中的using-sdk标记中删除android:targetSdkVersion,一切正常。项目中的完整标签:

<uses-sdk android:minSdkVersion="15" />