制作蓝牙应用程序我需要访问代码的Android侧的某些广播操作。

所有这些都在我的核心中运行,所以我有一个查看模型,它将通过我的界面打电话

public interface IConnectionService
{
    //Properties
    string IntentName { get; }

    //Events
    event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;

    //Methods
    void RunIntent();
    void SearchConnection();
    void Connect(string macAddress);
}
.

runittent提示用户打开蓝牙(可能是另一个技术),然后我希望在蓝牙状态更改时触发器

Android.Bluetooth.BluetoothAdapter.ActionStateChanged
.

以及当我搜索我需要的新设备时

Android.Bluetooth.BluetoothDevice.ActionFound
.

但我不能把[android.runtime.register(“action_found”)]和[android.runtime.register(“action_state_changed”)]在我的类上,如果我尝试将它放在我的视图上,那么这才仅适用,但如果我这样做,我认为我的观点中需要逻辑?

是否可以以某种方式将其放入核心中?

我的课程实现我的界面

using System;
using Android.Bluetooth;
using Android.Content;
using Cirrious.MvvmCross.Android.Platform.Tasks;
using Test.Core.Interfaces;
using Test.Core.Models;

namespace Test.Core.Android
{
    public class AndroidBluetooth : MvxAndroidTask, IConnectionService
    {
    #region Private Fields

    private readonly BluetoothAdapter _bluetoothAdapter;

    #endregion
    #region Public Fields

    #endregion
    #region Properties

    public string IntentName { get { return "Turn on Bluetooth"; }}

    #endregion
    #region Constructor

    public AndroidBluetooth()
    {
        _bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
    }

    #endregion
    #region Events

    public event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
    private void ItemFound(SearchConnectionItemEventArgs e)
    {
        if(SearchItemFoundEvent != null)
        {
            SearchItemFoundEvent(this, e);
        }
    }

    #endregion

    #region Methods
    public void RunIntent()
    {
        if (_bluetoothAdapter == null)
        {
            //No bluetooth support on phone
        }
        else if(!_bluetoothAdapter.IsEnabled)
        {
            var intent = new Intent(BluetoothAdapter.ActionRequestEnable);
            StartActivity(intent);
        }
    }

    public void SearchConnection()
    {

        if (_bluetoothAdapter == null)
        {
            //No bluetooth support on phone
        }
        else if (!_bluetoothAdapter.IsEnabled)
        {
            //Bluetooth not turned on
            RunIntent();
        }
        else
        {
            FindBondedDevices();
        }
    }

    private void FindBondedDevices()
    {
        var pairedDevices = _bluetoothAdapter.BondedDevices;

        if (pairedDevices.Count <= 0) return;

        foreach (var item in pairedDevices)
        {
            ItemFound(new SearchConnectionItemEventArgs {Name = item.Name, MacAddress = item.Address});
        }
    }

    private void FindNewDevices()
    {
        if (_bluetoothAdapter == null)
        {

        }
        else if (!_bluetoothAdapter.IsEnabled)
        {

        }
        else
        {
            _bluetoothAdapter.StartDiscovery();
            //Bind event for new devices
        }
    }

    public void Connect(string macAddress)
    {

    }
    #endregion
}
}
.

有帮助吗?

解决方案

免责声明:我不熟悉Android的这一部分 - 我从未使用蓝牙堆栈。

从您的描述中听起来您已经知道答案的声音 - 这些属性需要在活动/视图中进行方法。

当然,一旦他们被添加到活动/视图,那么它很容易将这些方法呼叫到ViewModel - 只需在视图中使用ViewModel属性。


首先尝试将这一部分作为独立样本的工作更容易 - 然后解决如何使其跨平台和/或MVVM。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top