Important to note, I am using Xamarin to develop Cross-platform apps. 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. It is relatively easy to understand, even if you do not know C# or 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.
I work with a BLE heart rate monitor. I connect to it, then set notifications for the heart rate characteristic and then read it. From iOS this works perfectly fine. From android however it tells me I do not have the permissions to read the characterstic. This is obviously wrong, since it works from 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.
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. (see in the referenced question )
原来,甚至以为我不允许从特征读取值,而无需调用readCharacteristics()
即可自动更新值,而我只需调用getValue()
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.