繁体   English   中英

Xamarin Android的蓝牙LE特性

[英]Bluetooth LE characteristic from Xamarin Android

我正在尝试从Xamarin Android应用程序读取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) is there a possiblity of a deadlock?

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

有没有可能发生僵局?

如果两者都为m_gatt.readCharacteristic(gattCharacteristic); m_gatt.writeCharacteristic(gattCharacteristic); 方法被异步调用,这可能导致死锁。 因为您可以使用两个SemaphoreSlim同时更改m_characteristic 使用一个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)
       {
       //.....
       }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM