2016-12-01 8 views
0

Xamarin 안드로이드 응용 프로그램에서 Bluetooth LE 특성을 읽으려고합니다.Xamarin 안드로이드의 Bluetooth LE 특성

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) 교착 상태의 possiblity가이 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

가 내부에?

m_gatt.readCharacteristic(gattCharacteristic);m_gatt.writeCharacteristic(gattCharacteristic); 메서드를 모두 비동기 적으로 호출하면 교착 상태가 발생할 수 있습니다. 2 개의 SemaphoreSlim을 사용하여 m_characteristic을 동시에 변경할 수 있습니다. 통지를

당신은 반환을 사용할 수를 사용하기 전에 속성이 (신고 대상) 인 경우 나 확인 할 수있는 방법이 있나요

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(); 
    } 

다음 코드로 교착 상태를 해결할 수있는 하나의 SemaphoreSlim를 사용하여 속성이 (신고 대상) 인 경우 코드를 다음과 같이 setCharacteristicNotification의 값은 확인 :

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

하지만 당신은 setCharacte를 호출하기 전에 risticNotification 속성이 (통지 할 수 있는지) 확인할 방법이 없습니다.

관련 문제