I have a specific platform service which is being registered using a platform initializer
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance<IBlueToothService>(new BlueToothService_Android());
}
}
As you see I am registering an instance as I want it to be the same instance.
My main page VM get this service injected in its constructor:
public MainPageViewModel(INavigationService navigationService, IBlueToothService btService)
: base(navigationService)
{
RefreshBlueTooothDevicesCommand = new DelegateCommand(RefreshBlueToothExecuted, RefreshBlueToothCanExecute);
_blueToothService = btService;
_blueToothService.DeviceAdded += BlueToothServiceDeviceAdded;
_blueToothService.ScanFinished += () => ToggleRunningState();
}
I am using this service also in a broadcast receiver:
public override void OnReceive(Context context, Intent intent)
{
IBlueToothService service = Xamarin.Forms.DependencyService.Get<IBlueToothService>();
string action = intent.Action;
if (action == BluetoothDevice.ActionFound)
{
BluetoothDevice newDevice = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
service.AddDevice(new Models.BluetoothDevice(newDevice.Name, newDevice.Address));
}
if(action == BluetoothAdapter.ActionDiscoveryFinished)
{
service.RaiseScanFinishedEvent();
}
}
I am not getting the same instance. but 2 different ones. I am pretty sure that each one of them resolves them from a different container but I cant figure our how to point all of them to the same one.
Thanks.