0

我成功地为我的 android 应用程序编写了一个代码,该代码旨在从我的 BLE 设备读取特征值,然后激活与此特征相关的通知,以便在我的应用程序中显示更新的值。该应用程序运行良好。

但是当我尝试激活第二个特征的通知时,通知仅适用于一个特征,而不适用于两者。

所以让我们称第一个特征'Date_time_Characteristic'和第二个特征'nbr_sample_Characteristic'

我注意到以下内容:如果我订阅了“Date_time_Characteristic”的通知,然后我订阅了“nbr_sample_Characteristic” ,则只有第一个订阅的会生成通知。在这种情况下是'Date_time_Characteristic'

            // set notification for Date time Characteristic
        Console.WriteLine("Charac Uuid =  " + _charac_DateTime.Uuid);
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].SetCharacteristicNotification(_charac_DateTime, true);
        BluetoothGattDescriptor descriptor = _charac_DateTime.GetDescriptor(Descr_UUID);
        Console.WriteLine("descriptor : " + descriptor.Uuid);
        descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].WriteDescriptor(descriptor);


        // set notification for nbr sample Characteristic
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].SetCharacteristicNotification(_charac_NbrSample, true);
        BluetoothGattDescriptor descriptor2 = _charac_NbrSample.GetDescriptor(Descr_UUID);
        descriptor2.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].WriteDescriptor(descriptor2);

如果我做相反的事情并首先订阅“nbr_sample_Characteristic”,则只有“nbr_sample_Characteristic”会生成通知并更新它的值。

            // set notification for nbr sample Characteristic
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].SetCharacteristicNotification(_charac_NbrSample, true);
        BluetoothGattDescriptor descriptor2 = _charac_NbrSample.GetDescriptor(Descr_UUID);
        descriptor2.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].WriteDescriptor(descriptor2);

        // set notification for Date time Characteristic
        Console.WriteLine("Charac Uuid =  " + _charac_DateTime.Uuid);
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].SetCharacteristicNotification(_charac_DateTime, true);
        BluetoothGattDescriptor descriptor = _charac_DateTime.GetDescriptor(Descr_UUID);
        Console.WriteLine("descriptor : " + descriptor.Uuid);
        descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
        BluetoothLEManager.Current.ConnectedDevices[App.Current.State.SelectedDevice].WriteDescriptor(descriptor);

我该如何解决这个问题并让这两个特征都通知应用程序?

4

0 回答 0