Xamarin Android的蓝牙LE特性

时间:2016-12-01 01:19:52

标签: c# android bluetooth xamarin.android bluetooth-lowenergy

我正在尝试从Xamarin Android应用程序中读取蓝牙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)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 个答案:

答案 0 :(得分:1)

  

是否存在死锁的可能性?

如果异步调用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) { //..... } 的返回值作为以下代码来检查属性是否(通知):

{{1}}

但在调用setCharacteristicNotification之前,没有方法可以检查属性是否(通知)。