mvvmcrossを使用したブロードキャストアクションを聞く場所
-
12-12-2019 - |
質問
私のBluetoothアプリケーションを作成するコードのAndroid側でのブロードキャストアクションにアクセスする必要があります。
これは私のコアで実行されているので、私は私のインターフェイスを通呼するビューモデルを持っています
public interface IConnectionService
{
//Properties
string IntentName { get; }
//Events
event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
//Methods
void RunIntent();
void SearchConnection();
void Connect(string macAddress);
}
.
runintentは、Bluetoothをオンにするようにユーザーに(別のテクノロジーになる可能性があります)、Bluetoothの状態が変更されたときにイベントトリガーが好きです
Android.Bluetooth.BluetoothAdapter.ActionStateChanged
.
また、私が必要な新しい機器を検索するとき、
Android.Bluetooth.BluetoothDevice.ActionFound
.
しかし、私は[Android.Runtime.Register( "Action_found")]と[Android.Runtime.Register( "Action_State")]を私のクラスに置くことができません。しかし、私が私のビューでロジックが必要なのならば、私は私のビューにロジックが必要ですか?
どういうわけかコアに入れることは可能ですか?
マイクラス私のインタフェースを実装する
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のこの部分に慣れていません - 私はBluetoothスタックを使用したことがない。
あなたの説明からそれはあなたがすでに答えを知っているように聞こえます - これらの属性は、アクティビティ/ビュー内のメソッドを実行する必要があります。
もちろん、アクティビティ/ビューに追加されたら、次にこれらのメソッド呼び出しをviewModelにルーティングするのが簡単です。ビュー内のViewModel
プロパティを使用するだけです。
最初にスタンドアロンのサンプルとして働くあなたの働き側のこの部分を手に入れようとするのがおそらく簡単です - そしてそれをクロスプラットフォームおよび/またはMVVMの作り方を調べます。