繁体   English   中英

无法从Android读取BLE字符

[英]Cannot read BLE Characterstic from Android

请注意,我正在使用Xamarin开发跨平台应用程序。 对于蓝牙,我必须开发特定于平台的软件,因此以下代码是android库的一部分,但均使用C#编码,因为这是xamarin的语言。 即使您不了解C#或Xamarin,也相对容易理解。

我的问题与非常相似,不同之处在于,我可以完全确定自己具有阅读权限,因为它确实可以在iOS上运行。

我使用BLE心率监测器。 我连接到它,然后设置有关心率特征的通知,然后阅读它。 在iOS上,此功能完全正常。 从android但是它告诉我我没有阅读特征的权限。 这显然是错误的,因为它可以在iOS上运行。 这可能是什么原因? 连接到设备后是否可能需要重新加载权限? 我没有为此找到功能。

这是我用来连接设备的代码。

bleGatt = device.ConnectGatt(Android.App.Application.Context, false, new MyGattCallback(this));

然后在连接状态更改后,我开始服务发现。 handleConnectionSuccess()handleDisconnect()返回到UI,以向用户显示进度。

public override void OnConnectionStateChange(BluetoothGatt gatt, 
            [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
{
    switch(newState)
    {
    case ProfileState.Connected:
        reference.handleConnectionSuccess();
        gatt.DiscoverServices();
        break;
    case ProfileState.Disconnected:
        reference.handleDisconnect();
        break;
    }
}

public override void OnServicesDiscovered(BluetoothGatt gatt, [GeneratedEnum] GattStatus status)
{
    if (status == GattStatus.Success) {
        BluetoothGattService service = bleGatt.GetService(UUID.FromString("CDEACB80-5235-4C07-8846-93A37EE6B86D"));
        if (service == null)
            return;
        BluetoothGattCharacteristic characteristic = service.GetCharacteristic(UUID.FromString("CDEACB80-5235-4C07-8846-93A37EE6B86D"));
        bleGatt.SetCharacteristicNotification(characteristic, true);
        bool readable = ((characteristic.Permissions & GattPermission.Read) != 0);
        Debug.WriteLine("Characteristic is readable: " + readable + " Permissions: " + characteristic.Permissions);
    } else 
        Debug.WriteLine("Service Discovery ended with not success status: " + status);
    }
}

在这里,我总是得到输出: Characteristic is readable: False Permissions: 0稍后在gatt.ReadCharacteristic()返回false,根据android的消息来源,这是因为我没有读取权限。 (请参阅所引用的问题

原来,甚至以为我不允许从特征读取值,而无需调用readCharacteristics()即可自动更新值,而我只需调用getValue()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM