【问题标题】:Xamarin.Forms Bluetooth Interface choicesXamarin.Forms 蓝牙接口选择
【发布时间】:2018-10-08 05:49:37
【问题描述】:

我正在尝试编写带有接口的依赖注入方法,以便我能够真正为 Android 和 UWP 提供一个用户界面。

一次处理和测试一项功能。架构正在工作,但我的问题是在 UWP 方面,大多数功能是异步的,而它们不在 Android 上。

所以我的问题是,我应该在 android 端“伪造”异步函数吗?如果是,如何?

这是我的例子:

using System.Collections.Generic;
using System.Threading.Tasks;

namespace XamarinBTArduinoLed
{
    public interface IBlueTooth
    {
        // as a first test, I will try to get a list of paired devices in both Android and UWP
        List<string> PairedDevices();
    }
}

这适用于 Android,但对于 UWP,它需要是

public interface IBlueTooth
    {
        // as a first test, I will try to get a list of paired devices in both Android and UWP
        Task<List<string>> PairedDevices();
    }

这不适用于我当前的 Android 实现。那么,假设它是最佳选择,我应该如何修改它以“伪造”异步方法?或者有没有其他我没有想到的方法?

[assembly: Xamarin.Forms.Dependency(typeof(XamarinBTArduinoLed.Droid.BlueToothInterface))]
    namespace XamarinBTArduinoLed.Droid
    {

        public class BlueToothInterface : IBlueTooth
        {       
            public List<string> PairedDevices()
            {
                List<string> BTItems = new List<string>();

                BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
                if (adapter == null) throw new Exception("No BlueTooth Adapter Found.");
                if (!adapter.IsEnabled)
                {
                    adapter.Enable();
                }
                //if (!adapter.IsEnabled)
                //{
                //    throw new Exception("BlueTooth adapter is NOT enabled.");
                //}
                foreach (var item in adapter.BondedDevices)
                {     

                    BTItems.Add(item.Name + " - " + item.Type.ToString());
                }
                return BTItems;
            }
        }
    }

【问题讨论】:

    标签: dependency-injection interface xamarin.android android-bluetooth xamarin.uwp


    【解决方案1】:

    我发现似乎是最好的方法:

    -1- 我更改了界面以获取任务返回:

    public interface IBlueTooth
        {
            // as a first test, I will try to get a list of paired devices in both Android and UWP
            Task<List<string>> PairedDevices();
        }
    

    -2- 我相应地修改了 Android 实现:

    public Task<List<string>> PairedDevices()
            {
                List<string> BTItems = new List<string>();
    
                BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
                if (adapter == null) throw new Exception("No BlueTooth Adapter Found.");
                if (!adapter.IsEnabled)
                {
                    adapter.Enable();
                }
                var t = Task.Factory.StartNew(() =>
                    {
                        foreach (var item in adapter.BondedDevices)
                        {
    
                            BTItems.Add(item.Name);
                        }
                        return BTItems;
                    });
                return t;
            }
    

    -3- 我实现了 INotifyPropertyChanged 以在 XAML 中显示

    现在一切正常,我从 Android 和 Windows UWP 中获取了我的 USB/串行设备列表。为这两个平台创建整个过程肯定需要一段时间,但至少看起来我是一个良好的开端。

    如果你有任何cmet,建议不要犹豫,也许是更好的方法.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2021-04-09
      • 2012-03-31
      • 2019-11-26
      相关资源
      最近更新 更多