0

需要注意的是,我使用Xamarin开发跨平台应用程序。对于蓝牙,我必须开发平台specfic,因此下面的代码是android库的所有部分,但用C#编码,因为这是xamarin的语言。即使你不了解C#或Xamarin,它也相对容易理解。无法从Android读取BLE特征

我的问题与this非常相似,区别在于我完全确定自己有权读取,因为它可以在iOS上使用。

我使用BLE心率监视器。我连接到它,然后设置心率特征的通知,然后阅读它。从iOS开始,这很好。不过,它告诉我,我没有权限阅读这些字符。这显然是错误的,因为它是从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,根据机器人的来源是因为我没有权限读取。 (见in the referenced question

+0

我不确定这是否相关,但为什么您对服务和特征都有相同的uuid? – Emil

+0

这是正确的,我在iOS中使用它也只是检查。但也想知道^^, – findusl

+0

不知道为什么会发生这种情况,但放在一边 - 您是否使用BLE标准心率测量特性? https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.heart_rate_measurement.xml – SJoshi

回答

0

原来,即使认为我不允许从特性中读取值自动更新而不需要readCharacteristics()需要被调用,我可以直接调用getValue()它工作。