Xamarin BluetoothLE插件无法正常工作

时间:2018-03-23 17:14:30

标签: xamarin

我创建了一个新的Xamarin.Forms应用来测试基本的蓝牙功能。我将此插件下载到Android项目和共享项目中:

https://github.com/aritchie/bluetoothle

我在共享项目中编写了此函数,并在我的Android项目中从我的启动活动的OnCreate调用它:

public static async Task BroadcastBluetooth()
{
    // (I do not await this function when I call it)

    try
    {
        await Task.Delay(5000); // just to make sure we give enough time for all initialization to complete

        _server = CrossBleAdapter.Current.CreateGattServer();

        // exception thrown on this line
        await _server.Start(new AdvertisementData
        {
            LocalName = "TestServer",
            ServiceUuids = new List<Guid>()
        });
    }
    catch (Exception e)
    {

    }
}

它引发了这个异常:

  

{System.NullReferenceException:对象引用未设置为   对象的实例。在   Plugin.BluetoothLE.Server.GattServer.StartAdvertising   (Plugin.BluetoothLE.Server.AdvertisementData adData)[0x00095] in   C:\ dev的\ ACR \ bluetoothle \ Plugin.BluetoothLE.Android \服务器\ GattServer.cs:135   在Plugin.BluetoothLE.Server.GattServer.Start   (Plugin.BluetoothLE.Server.AdvertisementData adData)[0x00011] in   C:\ dev的\ ACR \ bluetoothle \ Plugin.BluetoothLE.Android \服务器\ GattServer.cs:70   在App2.App + d__7.MoveNext()[0x00097]中   C:\ Projects \ app2 \ App2 \ App2 \ App.xaml.cs:49}

我只是在做一些非常基本的事情,所以我必须遗漏一些东西?例外是引用插件开发人员机器的目录路径(C:\ dev \ acr ...)所以要么这个插件坏了,要么我做错了什么?

1 个答案:

答案 0 :(得分:0)

我在我当前的项目中使用相同的插件,它工作正常。所以我怀疑问题是在插件中。

关于可能导致问题的一些想法:

  1. 您是否在能够执行BLE广告的真实设备上测试此代码?
  2. 您是否在Android项目中相应地设置了权限?
  3. 使用原生android apis时,BLE广告是否有效?
  4. 如果您可以附加我可以重现问题的存储库,那么它也会有所帮助。

相关问题