mvvmcross로 방송 액션을 듣는 곳
-
12-12-2019 - |
문제
My Bluetooth 응용 프로그램 만들기 코드의 Android 측면에서 일부 방송 작업에 액세스해야합니다.
이 모든 것은 내 코어에서 실행되므로 인터페이스 을 통해 호출 할 ViewModel이 있습니다.
public interface IConnectionService
{
//Properties
string IntentName { get; }
//Events
event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
//Methods
void RunIntent();
void SearchConnection();
void Connect(string macAddress);
}
.
런트 ent는 사용자에게 Bluetooth를 켜라는 메시지를 표시합니다 (다른 기술 일 수 있음). 블루투스 상태가 변경되면 이벤트 트리거를 사용하고 싶습니다
Android.Bluetooth.BluetoothAdapter.ActionStateChanged
.
및 또한 새로운 장치를 검색 할 때 가 필요합니다.
Android.Bluetooth.BluetoothDevice.ActionFound
.
그러나 나는 [Android.Runtime.Register ( "action_found")]와 [Antroll.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
}
}
. 해결책
Disclaimer : Android 의이 부분에 익숙하지 않습니다. 블루투스 스택을 사용한 적이 없습니다.
귀하의 설명에서 답변을 알고있는 것처럼 들리 었음 -이 속성은 활동 /보기에서 메서드를 계속해야합니다.
물론 활동 /보기에 추가되면 이러한 메소드 호출을 viewModel로 연결하는 것이 쉽습니다.보기 내에서 ViewModel
속성을 사용하십시오.
아마도 독립 실행 형 샘플로서의이 부분을 먼저 얻는 것이 더 쉽고, 플랫폼 간 및 / 또는 MVVM을 교차시키는 방법을 일으킨다.