2016-12-01 3 views
0

Я пытаюсь прочитать характеристику Bluetooth LE от приложения Xamarin для Android.Характеристики Bluetooth LE от Xamarin Android

m_characteristicReady = new SemaphoreSlim(1); 
m_characteristicChanged = new SemaphoreSlim(1); 



public async Task<BluetoothGattCharacteristic> GetCharecteristic(int timeout,BluetoothGattCharacteristic characteristic) 
     { 
      EnableCharacteristicNotification(characteristic); 
      //Once notifications are enabled for a characteristic, 
      //an onCharacteristicChanged() callback is triggered if the characteristic changes on the remote device: 
      m_characteristicChanged.Wait(); 

      //We serialize all requests and timeout only on requests themself cand not their predesesors 
      m_characteristicReady.Wait(); 
      //todo check result ??? 
      m_gatt.ReadCharacteristic(characteristic); 
      if (await m_characteristicReady.WaitAsync(timeout) == true || 
       await m_characteristicChanged.WaitAsync(timeout) == true) 
      { 
       m_characteristicChanged.Release(); 
       m_characteristicReady.Release(); 
       return m_characteristic; 
      } 
      return null; 
     } 

    public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, [Android.Runtime.GeneratedEnum] GattStatus status) 
    { 
     m_characteristic = characteristic; 
     m_characteristicReady.Release(); 
    } 

    public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 
    { 
     m_characteristic = characteristic; 
     m_characteristicChanged.Release(); 
    } 

Мой вопрос в моей public async Task<BluetoothGattCharacteristic> GetCharecteristic(int timeout,BluetoothGattCharacteristic characteristic) функции

1) is there a possiblity of a deadlock?

2) Is there a way for me to check if the attribute is (notifiable) before enabling notification

ответ

1

является ли Possiblity из тупика?

Если оба m_gatt.readCharacteristic(gattCharacteristic); и m_gatt.writeCharacteristic(gattCharacteristic); методы вызываются асинхронно, это может привести к тупиковой ситуации. Потому что вы можете изменить m_characteristic в то же время, используя два SemaphoreSlim. Используйте один SemaphoreSlim может решить затор как следующий код:

public async Task<BluetoothGattCharacteristic> GetCharecteristic(int timeout, BluetoothGattCharacteristic characteristic) 
    { 
     EnableCharacteristicNotification(characteristic);    
     m_gatt.ReadCharacteristic(characteristic);     
     return m_characteristic; 
    } 

    public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, [Android.Runtime.GeneratedEnum] GattStatus status) 
    { 
     m_SemaphoreSlim.Wait(); 
     m_characteristic = characteristic; 
     m_SemaphoreSlim.Release(); 
    } 

    public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 
    { 
     m_SemaphoreSlim.Wait(); 
     m_characteristic = characteristic; 
     m_SemaphoreSlim.Release(); 
    } 

Есть ли способ для меня, чтобы проверить, если атрибут (уведомлению) перед включением уведомления о

Вы можете использовать возврат значение setCharacteristicNotification, как следующий код, чтобы проверить, если атрибут (уведомлению):

 boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 
     if (isEnableNotification) 
     { 
     //..... 
     } 

Но прежде чем позвонить setCharacte risticNotification нет способа проверить, является ли атрибут (уведомляемым).

Смежные вопросы