c# - Xamarin/Android 和可怕的蓝牙 LE 错误 133 (GATT_ERROR)

标签 c# android xamarin bluetooth

我正在尝试编写一个跨平台的 Xamarin Forms 应用程序(在 C# 中)来与蓝牙 LE 设备通信。我已经下载了几个包(Plugin.BLE 和 Acr.Ble),但没有一个有效(它们都可以扫描但无法连接),所以我想我会尝试直接使用 Android API 看看是否可以帮助我理解什么失败了。顺便说一句,我在运行 Android 版本 6.0.1 的 Nexus 7 平板电脑上运行。

我正在成功扫描设备:

BluetoothManager bluetoothManager = (BluetoothManager)Forms.Context.GetSystemService (Android.Content.Context.BluetoothService);
m_adapter = bluetoothManager.Adapter;
if ((m_adapter == null) || (!m_adapter.IsEnabled))
    return false;
m_scanCallback = new BlueCallback (this);
m_adapter.BluetoothLeScanner.StartScan (m_scanCallback);

并且我看到了我想要与之通话的设备(在本例中,TI 开发板 MSP-EXP430F5438 在服务器模式下运行他们的 SPPLE 演示应用程序)。所以我停止扫描:

m_adapter.BluetoothLeScanner.StopScan (m_scanCallback);

然后我连接到所需的设备:

m_gattCallback = new BlueGattCallback ();
m_gatt = m_selectedDevice.ConnectGatt (Forms.Context, false, m_gattCallback);

我几乎立即接到回电说连接失败:

BlueGattCallback.OnConnectionStateChange(gatt, status=133, newState=Disconnected)

我读了this Google bug report所以在我的回调中,我尝试在我的回调中直接调用 Connect():

if ( ((int)status == 133) && (numRetries < 10) )
{
    numRetries++;
    bool connect = gatt.Connect ();
    Debug.WriteLine ("   gatt.Connect() returned " + connect);
}

此代码反复失败并出现错误 133(所有 10 次重试大约需要 3 秒)。

知道这里出了什么问题吗?

最佳答案

由于这取决于每个供应商开发的 BLE 堆栈, 该错误通常在三星设备上发生的次数多于任何其他类型,Android 6 是最不稳定的。

所以对于遇到 133 错误并因此而失眠的人来说。我建议使用 Sweetblue 包装器,但是您需要自己包装库以便在 C# 中使用。 它抽象了 BLE 的许多不稳定部分,并提供了良好的重试机制以及在某些情况下的优雅降级。

但最终这并不能解决所有问题,您将需要自己处理一些不稳定性。

关于c# - Xamarin/Android 和可怕的蓝牙 LE 错误 133 (GATT_ERROR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40872742/

相关文章:

C# Gzip 在服务器端错误地压缩字节数组

android - 如何在surfaceview android上绘制图像

c# - CustomWebViewRenderer DownloadManager 与 CopyBackForwardList 的问题

iphone - xamarin.IOS 中的错误 AggregateException : One or more errors occurred.

c# - ReplicationManager 在打开连接时引发异常

c# - 尝试验证时 Instagram 出错

c# - 读取 "Large"文件时内存不足异常

c# - 在调用 SaveChanges() 之前,在 foreach 循环中添加或更新实体需要太长时间?

java - App Engine 端点库 - NoClassDefFound 适用于我的 Api 及其 $Builder

android - 封闭alpha测试轨道如何区分两个测试轨道?