Domanda

Rendere la mia applicazione Bluetooth ho bisogno di accedere ad alcune azioni di trasmissione sul lato Android del mio codice.

Tutto ciò è eseguito nel mio nucleo, quindi ho un mirino che chiamerà attraverso la mia interfaccia

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

    //Events
    event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;

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

RunIntent richiede all'utente di attivare Bluetooth (potrebbe essere un'altra tecnologia) e vorrei quindi avere un trigger di eventi quando lo stato Bluetooth viene modificato

Android.Bluetooth.BluetoothAdapter.ActionStateChanged
.

E anche quando cerco nuovi dispositivi ho bisogno

Android.Bluetooth.BluetoothDevice.ActionFound
.

Ma non posso mettere il [Android.Runtime.Register ("Action_Found")] e [Android.Runtime.Register ("Action_State_Changed")]] Sulla mia classe, questo funziona solo se cerco di metterlo sulla mia opinione,Ma se lo faccio ho bisogno della logica a mio avviso?

È possibile metterlo nel nucleo in qualche modo?

La mia classe implementa la mia interfaccia

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
}
}
.

È stato utile?

Soluzione

Disclaimer: non ho familiarità con questa parte di Android - Non ho mai usato lo stack Bluetooth.

Dalla tua descrizione Sembra che tu sappia già la risposta: questi attributi devono andare sui metodi all'interno dell'attività / vista.

Certo, una volta che sono stati aggiunti all'attività / vista, è facile instradare queste chiamate del metodo attraverso il mirino - basta utilizzare la proprietà ViewModel all'interno della vista.


.

È probabilmente più facile cercare di ottenere questa parte del tuo lavoro come un campione autonomo prima - e quindi allenarsi come renderlo cross-platform e / o mvvm.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top