在哪里收听MVVMCross的广播操作

在哪里收听MVVMCross的广播操作

本文介绍了在哪里收听MVVMCross的广播操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

制作我的蓝牙应用程序,我需要在代码的Android端访问某些广播操作.

Making my Bluetooth application I need access to some broadcast actions on the Android side of my code.

所有这些都在我的Core中运行,所以我有一个ViewModel,它将通过我的界面调用

All this is run in my Core, so I have a ViewModel that will call through my interface

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

    //Events
    event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;

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

RunIntent提示用户打开蓝牙(可能是另一种技术),然后我想在蓝牙状态更改时触发事件

RunIntent prompts the user to turn on Bluetooth (Could be another technology) and I would then like to have an event trigger when Bluetooth state is changed

Android.Bluetooth.BluetoothAdapter.ActionStateChanged

当我搜索新设备时,也需要

And also when I search for new devices I need

Android.Bluetooth.BluetoothDevice.ActionFound

但是我不能将[Android.Runtime.Register("ACTION_FOUND")]和[Android.Runtime.Register("ACTION_STATE_CHANGED")]放在我的课程中,这只有在我尝试将其放在View上时才有效,但是如果我这样做,我需要逻辑吗?

But I cant put the [Android.Runtime.Register("ACTION_FOUND")] and [Android.Runtime.Register("ACTION_STATE_CHANGED")] on my class, this only works if I try to put it on my View, but if I do that I need logic in my view?

是否有可能以某种方式将其置于核心?

Is it possible to put it in the core somehow?

我的类实现了我的界面

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的这一部分不熟悉-我从未使用过蓝牙堆栈.

Disclaimer: I'm not familiar with this part of Android - I've never used the Bluetooth stack.

在您的描述中,听起来您已经知道答案了-这些属性需要在活动/视图"中的方法上进行.

From your description it sounds like you already know the answer - these Attributes need to go on methods within the Activity/View.

当然,一旦将它们添加到活动/视图"中,就很容易将这些方法调用路由到ViewModel-只需在View中使用ViewModel属性即可.

Of course, once they've been added to the Activity/View then it is easy to route these method calls through to the ViewModel - just use the ViewModel property within the View.

首先尝试将这部分工作作为独立的示例,然后找出如何使其跨平台和/或mvvm的方法,可能会更容易.

It's probably easier to try to get this part of your working as a standalone sample first - and then work out how to make it cross-platform and/or mvvm.

这篇关于在哪里收听MVVMCross的广播操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:03