简体   繁体   English

Xamarin Android的蓝牙LE特性

[英]Bluetooth LE characteristic from Xamarin Android

I am trying to read Bluetooth LE characteristic from Xamarin Android Application. 我正在尝试从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();
    }

My question is inside my public async Task<BluetoothGattCharacteristic> GetCharecteristic(int timeout,BluetoothGattCharacteristic characteristic) function 我的问题在我的public async Task<BluetoothGattCharacteristic> GetCharecteristic(int timeout,BluetoothGattCharacteristic characteristic)函数内部

1) is there a possiblity of a deadlock? 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 2) Is there a way for me to check if the attribute is (notifiable) before enabling notification

is there a possiblity of a deadlock? 有没有可能发生僵局?

If both m_gatt.readCharacteristic(gattCharacteristic); 如果两者都为m_gatt.readCharacteristic(gattCharacteristic); and m_gatt.writeCharacteristic(gattCharacteristic); m_gatt.writeCharacteristic(gattCharacteristic); methods are invoked asynchronously, it may lead a deadlock. 方法被异步调用,这可能导致死锁。 Because you may change m_characteristic at the same time by using two SemaphoreSlim . 因为您可以使用两个SemaphoreSlim同时更改m_characteristic Use one SemaphoreSlim can solve the deadlock as the following code: 使用一个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();
    }

Is there a way for me to check if the attribute is (notifiable) before enabling notification 在启用通知之前,有没有办法让我检查属性是否(可通知)

You can use the return value of setCharacteristicNotification as following code to check if the attribute is (notifiable): 您可以使用setCharacteristicNotification的返回值作为以下代码,以检查该属性是否为(可通知的):

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

But before you call setCharacteristicNotification there is no method to check if the attribute is (notifiable). 但是,在调用setCharacteristicNotification之前,没有方法可以检查该属性是否为(可通知的)。

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

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