【问题标题】:Xamarin Forms Bluetooth LE not showing nearby discoverable devicesXamarin Forms 蓝牙 LE 未显示附近的可发现设备
【发布时间】:2018-04-30 20:41:26
【问题描述】:

我正在制作一个使用蓝牙功能的应用程序,例如扫描设备等。我检查了扫描标志并返回 true,但没有显示我正在测试的可发现设备。

我使用 Samsung J7 Pro 作为我的应用测试设备,并将 Samsung J7 作为我想在已发现设备列表中查看的设备。

J7 已设置为可发现并开启蓝牙。

我的代码基于Monkey.BluetoothLE

这是我所拥有的:

声明

ObservableCollection<BluetoothViewModel> vm = new ObservableCollection<BluetoothViewModel>();
Android.Bluetooth.BluetoothManager _blManager;
Android.Bluetooth.BluetoothManager _blManager;
Robotics.Mobile.Core.Bluetooth.LE.Adapter _bleAdapter;

函数

public BluetoothPage()
{
    InitializeComponent();
    lvInfo.ItemsSource = vm;

    var appContext = Android.App.Application.Context;
    _blManager = (Android.Bluetooth.BluetoothManager)appContext.GetSystemService("bluetooth"); 
    _blAdapter = _blManager.Adapter;
    _bleAdapter = new Robotics.Mobile.Core.Bluetooth.LE.Adapter();

    _bleAdapter.DeviceDiscovered += _bleAdapter_DeviceDiscovered;
    _bleAdapter.ScanTimeoutElapsed += _bleAdapter_ScanTimeoutElapsed;
}

private void btnScanStopBluetooth_Click(object sender, EventArgs e)
{
    if (!_bleAdapter.IsScanning)
    {
        if (!_blAdapter.IsEnabled)
        {
            _blAdapter.Enable();
            DisplayInformation("Turning on bluetooth...");
            while (!_blAdapter.IsEnabled)
            {
                //do nothing until enabled
            }
        }
        vm.Clear();
        btnScan.Text = "Stop Scan";
        _bleAdapter.StartScanningForDevices();
    }
    else
    {
        btnScan.Text = "Start Scan";
        _bleAdapter.StopScanningForDevices();
    }
}

private void _bleAdapter_DeviceDiscovered(object sender, Robotics.Mobile.Core.Bluetooth.LE.DeviceDiscoveredEventArgs e)
{
    count++;
    vm.Add(new BluetoothViewModel
    {
        Name = e.Device.Name,
        ID = e.Device.ID.ToString(),
        RSSI = e.Device.Rssi.ToString()
    });
}

private void _bleAdapter_ScanTimeoutElapsed(object sender, EventArgs e)
{
    DisplayInformation("Scan Timeout");
    _bleAdapter.StopScanningForDevices();
    btnScan.Text = "Start Scan";
}

private void DisplayInformation(string line)
{
    lblStatus.Text = line;
}

列表视图绑定到“vm”,它将显示发现的设备。 它不显示任何内容,并且计数始终为零,但我使用 _bleAdapter.IsScanning 检查了扫描标志,它返回 true。

编辑: 我尝试了其他用于蓝牙的开源示例程序,例如

xamarin-bluetooth-le(BLE 浏览器)

Bluetooth-Xamarin.Forms(演示蓝牙)

他们似乎都没有列出该设备。当我在设置下使用我的内置蓝牙应用程序时,它会列出设备。我在这里错过了什么?

【问题讨论】:

    标签: c# android xamarin mobile bluetooth


    【解决方案1】:

    您是否已授予蓝牙和位置权限?

    您必须在清单/或设置中授予权限,并且根据 sdk (23+) 还要求用户提供额外权限。 https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多