Próbuję włączyć powiadomienia dla więcej niż jednej charakterystyki BLE za pomocą Xamarin/Androida, ale wydaje się, że nie można tego zrobić. Wygląda na to, że aplikacja przestaje otrzymywać jakiekolwiek zdarzenia BLE, jeśli spróbuję włączyć więcej niż jedną naraz.Jak włączyć wielokrotne powiadomienia BLE na Xamarin/Android?
Czy ktoś może potwierdzić, czy jest to możliwe za pomocą Tamarin/Android. Mamy natywną aplikację na iOS, która działa dobrze z włączonymi wieloma powiadomieniami. Podstawowe kroki, których używamy, są następujące:
- skanowania dla urządzenia
- połączyć się z urządzeniem
- Odkrywanie usługi
- Dla każdej odkrytej iteracyjne usług poprzez charakterystykę i włączyć te, które są wymagane
- Process każde asynchroniczne zdarzenie zwrotne w wywołaniu zwrotnym BLE
Za każdym razem, gdy próbujemy włączyć powiadomienia na więcej niż jedna cecha nie otrzymujemy już żadnych zdarzeń.
Nie znalazłem również przykładów, w których włączono więcej niż jedną cechę.
Mam nadzieję, że po prostu przegapiłem coś fundamentalnego na temat korzystania z interfejsów API Xamarin/Android.
public override void OnServicesDiscovered (BluetoothGatt gatt, GattStatus status)
{
base.OnServicesDiscovered (gatt, status);
foreach (BluetoothGattService service in gatt.Services) {
string uuid = service.Uuid.ToString().ToUpper();
if (uuid.Equals (BLEServices.HRService.ToUpper())) {
_Adap.LogMessage ("HRService discovered");
foreach(BluetoothGattCharacteristic characteristic in service.Characteristics) {
string c_uuid = characteristic.Uuid.ToString().ToUpper();
_Adap.LogMessage (" HRCharacteristic: " + c_uuid);
if (c_uuid.Equals(_Adap.useCharacteristic.ToUpper())) {
_Adap.LogMessage (" enabling HRCharacteristic");
gatt.SetCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
characteristic.AddDescriptor (descriptor);
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray());
gatt.WriteDescriptor (descriptor);
_Adap.StartTimer();
}
}
} else if (uuid.Equals (BLEServices.BatteryService.ToUpper())) {
_Adap.LogMessage ("BatteryService discovered");
foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
string c_uuid = characteristic.Uuid.ToString().ToUpper();
_Adap.LogMessage (" BatteryService: " + c_uuid);
if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper())) {
_Adap.LogMessage (" reading batteryCharacteristic");
// This may only be reported when the battery level changes so get the level first by doing a read
gatt.ReadCharacteristic (characteristic);
//gatt.SetCharacteristicNotification (characteristic, true);
//BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
//characteristic.AddDescriptor (descriptor);
//descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray());
//gatt.WriteDescriptor (descriptor);
}
}
} else if (uuid.Equals (BLEServices.DeviceInfoService.ToUpper())) {
_Adap.LogMessage ("DeviceInfoService discovered");
foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
string c_uuid = characteristic.Uuid.ToString().ToUpper();
_Adap.LogMessage (" DeviceInfoService: " + c_uuid);
if (c_uuid.Equals (BLEServices.kModelNumberCharacteristicUuidString.ToUpper())) {
//gatt.ReadCharacteristic (characteristic);
}
}
} else if (uuid.Equals (BLEServices.kHxM2CustomServiceUuidString.ToUpper())) {
_Adap.LogMessage ("HxM2CustomService discovered");
foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
string c_uuid = characteristic.Uuid.ToString().ToUpper();
_Adap.LogMessage (" HxM2CustomCharacteristic: " + c_uuid);
if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper())) {
_Adap.LogMessage (" enabling HxM2 characteristic: "+_Adap.useCharacteristic);
gatt.SetCharacteristicNotification (characteristic, true);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
characteristic.AddDescriptor (descriptor);
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray());
gatt.WriteDescriptor (descriptor);
// Start a timer to make sure that we can recover if we never receive any data from the device
_Adap.StartTimer();
}
}
} else {
_Adap.LogMessage ("Unknown Service "+uuid+" discovered");
}
}
}
Może ktoś wyjaśnić, co się następujące wiersze są dla
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
characteristic.AddDescriptor (descriptor);
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray());
gatt.WriteDescriptor (descriptor);
Wreszcie znalazłem coś, co działa: http: // stackoverflow. com/questions/17910322/android-ble-api-gatt-notification-not-received http://stackoverflow.com/questions/21278993/android-ble-how-to-read-multiple-characteristics –