I am building an iOS app with Xamarin and MvvmCross the required the use of Bluetooth LE. I am trying to use this plugin:
https://github.com/aritchie/bluetoothle
Here is my code:
var server = CrossBleAdapter.Current.CreateGattServer();
await server.Start(new AdvertisementData()); // throws exception
It throws an exception when trying to start the server:
{System.ArgumentException: Invalid State - Unknown at
Plugin.BluetoothLE.Server.GattServer.Start
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x0005f] in
<4281c4bd57f24525b20baae1afdf610b>:0
Apparently this plugin is easy to use so I must be missing something obvious?
That Exception indicates that the hardware is in invalid or initializing state and can be seen here: https://github.com/aritchie/bluetoothle/blob/master/Plugin.BluetoothLE.Apple.Shared/Server/GattServer.cs#L74
if (this.manager.State != CBPeripheralManagerState.PoweredOn)
throw new ArgumentException("Invalid State - " + this.manager.State);
I believe this is a bug in the code as it does not wait for the delegate to signal that the State has changed.
I found a solution. Instead of placing the code in my Core project, I placed it inside the iOS project itself, in a ViewDidAppear function, and placed a global variable in my UIViewController class:
private IGattServer server = CrossBleAdapter.Current.CreateGattServer();
I think you need to access the adapter with the appropriate thread, or at the appropriate time (after other initialization), hence why it was crashing before. I couldn't find an appropriate place to put this in my Core project, so I'm not sure if I have to put this code in each platform. Anyhow here's a solution for anyone else with this problem.
Related
I've been tasked with maintaining a Xamarin native project using MvvmCross 5.6.2. Not knowing exactly how to approach this, I've decided to update to one major version at a time (6 first, then 7 and 8). I'm not sure why I specifically have chosen 6.4.2, but it was maybe because this was the latest version of the majority of the plugins I was using on Nuget.
So far, the update has been a success and I have been able to fix all build errors. However, when running the application, I've been getting a null reference exception which I can't fully trace.
Based on the limited application output, I've been able to determine that the problem lies somewhere in my Android's setup.cs class (I think). I've been following Nick's .NET Travels advice on MvvmCross debugging. From viewing the MvvmCross 6.4.2. source and pasting in the following code in my own overrides:
public virtual void LoadPlugins(IMvxPluginManager pluginManager)
{
Type pluginAttribute = typeof(MvxPluginAttribute);
IEnumerable<Assembly> pluginAssemblies = GetPluginAssemblies();
foreach (Assembly item in pluginAssemblies)
{
IEnumerable<Type> enumerable = item.ExceptionSafeGetTypes();
foreach (Type item2 in enumerable)
{
if (TypeContainsPluginAttribute(item2))
{
pluginManager.EnsurePluginLoaded(item2);
}
}
}
bool TypeContainsPluginAttribute(Type type)
{
object[] customAttributes = type.GetCustomAttributes(pluginAttribute, inherit: false);
return ((customAttributes != null && customAttributes.Length != 0) ? 1 : 0) > (false ? 1 : 0);
}
}
public virtual IEnumerable<Assembly> GetPluginAssemblies()
{
string mvvmCrossAssemblyName = typeof(MvxPluginAttribute).Assembly.GetName().Name;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
var test = from asmb in assemblies.AsParallel()
where AssemblyReferencesMvvmCross(asmb, mvvmCrossAssemblyName)
select asmb;
return test;
}
I'm able to see that GetPluginAssemblies doesn't return any enumerable, and the LoadPlugins method then produces the NullReferenceException. But I can't see what this NullReference actually is.
I followed the upgrading from 5 to 6 guide https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60.
I looked at the MvvmCross 6 and 6.4.0 release pages:
https://www.mvvmcross.com/mvvmcross-6.0.0-release/
https://www.mvvmcross.com/mvvmcross-6.4.0-release/
And I followed Benjamin Mayrargue's guide: https://medium.com/#bigoudi/upgrading-from-mvvmcross-5-to-mvvmcross-6-7ded83ecb69d
But I have been unable to load my plugins (previously they were bootstraps, but most of the guides say these can be discarded now and that loading plugins is easier).
I also attempted the answer suggested in this question How to use an mvvmcross plugin such as the file plugin.
But to no avail.
So I am asking if anyone knows a good guide or how to use plugins in MvvmCross 6.4.2.
Thank you.
Plugins are just a way to register things in the IoC Container. This is done by MvvmCross during startup using the LoadPlugins method in your Setup file.
Most of the time it should just work. However, there are some caveats.
If the Linker has gone ahead and linked away some of the plugins code, you will have a bad time. What you can do about that is to hint the mono linker to not strip the code away.
Add a LinkerPleaseInclude class and add a Include method in it that looks something like:
new MvvmCross.Plugin.Color.Platforms.Ios.Plugin().Load();
You can do that for every plugin you may want to use.
If LoadPlugins doesn't find the entry Assembly, sometimes it also does not register the plugins. You can override LoadPlugins in your Setup class and just call EnsurePluginLoaded:
public override void LoadPlugins(IMvxPluginManager pluginManager)
{
base.LoadPlugins(pluginManager);
pluginManager.EnsurePluginLoaded<MvvmCross.Plugin.Color.Platforms.Ios.Plugin>();
}
I want to thank Cheesebaron for his plugin support. I think I've fixed my issue and as it turned out, I don't think there is a plugin issue after all (yet).
Thanks to Toolmakersteve also. His suggestion for using a try catch in the OnCreate of my overridden MvxSplashScreenAppCompatActivity surfaced an issue with setting a theme for this activity. In actuality, this class was initially a MvxSplashScreenActivity.
Reverting this line, I then started getting NullReferenceExceptions on specific lines, all relating to IoC and lazy construction of singletons. The class Mvx seemed to be throwing up this error. On a sort of hunch from previous experience with my updating, I removed the MvvmCross.Platform using statement and checked what suggestions Mvx had available to it. It suggested MvvmCross and MvvmCross.Platform, so I tried the former instead. Sure enough, this moved my execution further, throwing up more Null Reference Exceptions. I also encountered one instance of failing to resolve IMvxResourceLoader from MvvmCross.Platform.Platform. Switching that to MvvmCross.Base did the trick.
This was only a chance fix through a bit of guess work. #CheeseBaron, should I add this as a note to this bit of documentation https://www.mvvmcross.com/documentation/upgrading/upgrade-to-mvvmcross-60? As mentioned, I'm as far as 6.4.2 now, so I'm not certain this is the right place for it.
I've got a few bugs with embedded resources to fix now, but if I encounter any more that are relevant to my question, I'll list them here.
I am trying to make my Xamarin Project use MVVM with Prism and DryIoc.
I mostly want to use AutoRegistration like below:
[AutoRegisterForNavigation]
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//Pages
containerRegistry.RegisterForNavigation<NavigationPage>();
//Services
containerRegistry.RegisterSingleton<ILocalDatabase, LocalDatabase>();
containerRegistry.RegisterSingleton<IUserProfileDataStore, UserProfileDataStore>();
containerRegistry.RegisterSingleton<IApplicationSettings, ApplicationSettings>();
containerRegistry.RegisterSingleton<ILogger, Logger>();
containerRegistry.RegisterSingleton<IApiService, ApiService>();
containerRegistry.RegisterSingleton<IUserSession, UserSession>();
containerRegistry.Register<IBrowser, BrowserImplementation>();
containerRegistry.Register<IConnectivity, ConnectivityImplementation();
containerRegistry.Register<IFileSystem, FileSystemImplementation>();
containerRegistry.Register<ICoreServices, CoreServices>();
}
I have also tried Manual Registration:
containerRegistry.RegisterForNavigation<LoginPage, LoginPageViewModel>();
Neither works, It hits the Login Page code behind then breaks with the following error:
Exception - High: Prism.Ioc.ContainerResolutionException:
An unexpected error occurred while resolving 'AppetiteApp.ViewModels.LoginPageViewModel' --->
DryIoc.ContainerException: code: UnableToResolveUnknownService; message: Unable to resolve
Resolution root AppetiteApp.ViewModels.LoginPageViewModel
with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'
I've also tried using a Linker file setting it's build action to "linkdescription"
As for my Login Page here is the declaration
public LoginPageViewModel(ICoreServices coreServices)
: base(coreServices)
The constructor of LoginPageViewModel requires the ICoreServices argument which is registered.
The error message says that the LoginPageViewModel itself is unknown to the IoC - it means the type LoginPageViewModel is not directly registered and not found through dynamic registrations or unknown service resolvers.
I am not a user of the Xamarin Prism so I am not sure about its mechanism for registering the view models.
Btw, this part of error
Resolution root AppetiteApp.ViewModels.LoginPageViewModel
with passed arguments [value(Prism.Navigation.ErrorReportingNavigationService)]
basically means the view-model was resolved via the foollowing call resolver.Resolve(typeof(LoginPageViewModel), args: new[] { errorReportingNavigationService })
Hope it will help you or someone knowledgeable in Xamarin to track the error cause.
So Once I investigated Inside of ICoreServices I commented out each of the dependencies then discovered that IUserSession was the once causing problems then I dug into that and discovered that the dependences for IAppInfo and IVersionTracking were missing in the App.Xaml.cs registr tyepes so I added that and then it worked!
containerRegistry.Register<IAppInfo, AppInfoImplementation>();
containerRegistry.Register<IVersionTracking, VersionTrackingImplementation>();
I created a new Xamarin.Forms app to test basic Bluetooth functionality. I downloaded this plugin into both the Android project and the shared project:
https://github.com/aritchie/bluetoothle
I wrote this function in the shared project and am calling it from the OnCreate of my launch activity in my Android project:
public static async Task BroadcastBluetooth()
{
// (I do not await this function when I call it)
try
{
await Task.Delay(5000); // just to make sure we give enough time for all initialization to complete
_server = CrossBleAdapter.Current.CreateGattServer();
// exception thrown on this line
await _server.Start(new AdvertisementData
{
LocalName = "TestServer",
ServiceUuids = new List<Guid>()
});
}
catch (Exception e)
{
}
}
It throws this exception:
{System.NullReferenceException: Object reference not set to an
instance of an object. at
Plugin.BluetoothLE.Server.GattServer.StartAdvertising
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00095] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:135
at Plugin.BluetoothLE.Server.GattServer.Start
(Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00011] in
C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:70
at App2.App+d__7.MoveNext () [0x00097] in
C:\Projects\app2\App2\App2\App.xaml.cs:49 }
I'm only doing something really basic so I must be missing something? The exception is referencing a directory path of the plugin's developer's machine (C:\dev\acr...) so either this plugin is broken or I'm doing something really wrong?
I am using the same plugin in a current project of mine and it is working fine. So I doubt the issue is within the plugin.
Some thoughts about what could cause the issue:
Are you testing this code on a real device that is capable of performing BLE Advertisement?
Do you have set the permissions accordingly in your android project?
Does BLE Advertising work when you use the native android apis?
It also would be helpful, if you could attach a repository with which I can reproduce the issue.
I am trying to reduce app size and enabled "Link All Assemblies", but my app crashes with following error
MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type Japam.Core.ViewModels.MantraasViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information
I tried
--linkskip=mvvmcross.platform.ios
--linkskip=mvvmcross.core
--linkskip=mvvmcross.platform
but no luck, kept preserve attribute at assembly level of my core pcl project.
Also added a couple of more methods in LinkerPleaseIncludefile
public void Include(MvvmCross.Platform.IoC.MvxPropertyInjection injection)
{
injection = new MvvmCross.Platform.IoC.MvxPropertyInjection();
}
public void Include(MvxDefaultViewModelLocator locator)
{
locator = new MvxDefaultViewModelLocator();
}
but no use, could somebody help me find out what is missing
MvvmCross.Platform.Exceptions.MvxException: Failed to construct and initialize ViewModel for type
Japam.Core.ViewModels.MantraasViewModel from locator
MvxDefaultViewModelLocator - check InnerException for more information
---> MvvmCross.Platform.Exceptions.MvxException: Problem creating viewModel of type MantraasViewModel --->
MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to
construct MantraasViewModel --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
MvvmCross.Platform.Exceptions.MvxIoCResolveException: Failed to
resolve parameter for parameter sqliteConnectionFactory of type
IMvxSqliteConnectionFactory when creating
Japam.Core.Services.DataServices.MantraService
at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.GetIoCParameterValues
(System.Type type, System.Reflection.ConstructorInfo firstConstructor)
[0x00036] in D:\git\MvvmCross\MvvmCross\Platform\Platform\IoC\
Finally I found the issue, it's SQLite problem so I added below three commands and it started working hope this helps someone else
--linkskip=MvvmCross.Plugins.Sqlite
--linkskip=SQLite-net
--linkskip=MvvmCross.Plugins.Sqlite.iOS
I should kept SQLite earlier it self my last project I did kept SQLite but forgot this time
I've created a plugin and registered it using hte registration tool. I've also added a step that is supposed to handle a message of creation of an instance. Sadly, the intended behavior doesn't occur.
My guess is that something inside the plugin crashes but I have no idea on how to debug it. Setting up breakpoints is not going to work agains on-line version, I understand, so I'm not even trying.
For legal and technical reasons, I won't be able to lift over the solution to an on-premise installation, neither. Is guessing my only option?
For server-side (plugins) I'm using ITracingService. For client-side I log everything to console. The downside with the first is that you actually need to crash the execution to get to see anything. The downside with the latter is that plugins sometimes get executed without GUI being invoked at all.
When it comes to heavier projects, I simply set up a WCF web service that I call from the plugin and write to that. That way, on one screen, I'm executing the plugin while on the other, I'm getting a nice log file (or just put the sent information to on the screen).
You could, for instance, start with a very basic update of a field on the instance of your entity that's being created. When you have that working, you can always fall back to the last working version. If you don't even get that to work, it mean, probably, that you're setting up the plugin registration incorrectly.
A very efficient way would be to lift over the solution to an on-premise version where you have full control but I see in your question that it's not en option.
In case you could lift the solution to an on-premise version, here's a link on how to debug plugins.
Don't forget that you also have access to the ITracingService.
You can get a reference to it in your Execute method and then write to it every so often in your code to log variables or courses of action that you are attempting or have succeeded with. You can also use it to surface more valuable information when an exception occurs.
It's basically like writing to a console. Then, if anything causes the plug-in to crash at runtime then you can see everything that you've traced when you click Download Log File on the error shown to the user.
Beware though - unless your plug-in actually throws an exception (deliberate or otherwise) then you have no access to whatever was traced.
Example:
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(
typeof(IPluginExecutionContext));
// Get a reference to the tracing service.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
tracingService.Trace("Getting entity from InputParameters...");
// may fail for some messages, since "Target" is not present
var myEntity = (Entity)context.InputParameters["Target"];
tracingService.Trace("Got entity OK");
// some other logic here...
}
catch (FaultException<OrganizationServiceFault> ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = (FaultException<OrganizationServiceFault>)ex.InnerException;
_trace.Trace(ex.ToString());
}
throw new InvalidPluginExecutionException(
string.Format("An error occurred in your plugin: {0}", ex));
}
catch (Exception ex)
{
_trace.Trace(ex.ToString());
while (ex.InnerException != null)
{
ex = ex.InnerException;
_trace.Trace(ex.ToString());
}
throw;
}
}