简体   繁体   English

无法从Android读取BLE字符

[英]Cannot read BLE Characterstic from Android

Important to note, I am using Xamarin to develop Cross-platform apps. 请注意,我正在使用Xamarin开发跨平台应用程序。 For bluetooth I have to develop platform specfic, therefore the following code is all part of the android library, but coded in C#, since that is the language of xamarin. 对于蓝牙,我必须开发特定于平台的软件,因此以下代码是android库的一部分,但均使用C#编码,因为这是xamarin的语言。 It is relatively easy to understand, even if you do not know C# or Xamarin. 即使您不了解C#或Xamarin,也相对容易理解。

My problem is very similiar to this one, with the difference that I am completely sure that I have permission to read, since it does work from iOS. 我的问题与非常相似,不同之处在于,我可以完全确定自己具有阅读权限,因为它确实可以在iOS上运行。

I work with a BLE heart rate monitor. 我使用BLE心率监测器。 I connect to it, then set notifications for the heart rate characteristic and then read it. 我连接到它,然后设置有关心率特征的通知,然后阅读它。 From iOS this works perfectly fine. 在iOS上,此功能完全正常。 From android however it tells me I do not have the permissions to read the characterstic. 从android但是它告诉我我没有阅读特征的权限。 This is obviously wrong, since it works from iOS. 这显然是错误的,因为它可以在iOS上运行。 What could be the cause here? 这可能是什么原因? Do I maybe need to reload the permissions after connecting to a device? 连接到设备后是否可能需要重新加载权限? I didn't find a function for that. 我没有为此找到功能。

This is the code I use to connect to the device. 这是我用来连接设备的代码。

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

Then in the connection status changed I start service discovery. 然后在连接状态更改后,我开始服务发现。 The handleConnectionSuccess() and handleDisconnect() go back to the UI to show the progress to the user. 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);
    }
}

Here I always get the output: Characteristic is readable: False Permissions: 0 Later on gatt.ReadCharacteristic() returns false, which according to the sources of android is because I do not have the permissions to read. 在这里,我总是得到输出: Characteristic is readable: False Permissions: 0稍后在gatt.ReadCharacteristic()返回false,根据android的消息来源,这是因为我没有读取权限。 (see in the referenced question ) (请参阅所引用的问题

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

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

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