Reputation: 3647
Making my Bluetooth application I need access to some broadcast actions on the Android side of my code.
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 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
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?
My class implementing my interface
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
}
}
Upvotes: 1
Views: 608
Reputation: 66882
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.
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.
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.
Upvotes: 1