Specific Cases¶
This section provides solutions for specific cases you could encounter
Android¶
Check/Start Service¶
If you find yourself in a situation where you are unsure if the Android service
is running you can proceed as below using an instance of Capture Helper CaptureHelper capture = new CaptureHelper()
.
But first, don’t forget to set the App context:
#. Set Android App Context to enable the usage of Android Service methods with
CaptureHelper.SetAndroidServiceContext(Android.App.Application.Context)
method. No instance of Capture Helper is
needed to set the context (this can be done in MainActivity.cs
under OnCreate(...)
).
Check if Companion is installed with
IsAndroidServiceInstalled()
method. Without Companion the service cannot exist.Check if the Service is running with
IsAndroidServiceRunning()
method.Start the service with
StartAndroidService()
method.
Here is an example:
private async Task AndroidService()
{
if (Device.RuntimePlatform == Device.Android)
{
switch (capture.IsAndroidServiceInstalled())
{
case true:
if (capture.IsAndroidServiceRunning() == false) capture.StartAndroidService();
break;
case false:
// Make sure that Companion is installed
break;
}
}
// Add time to let the Service start
await Task.Delay(1000);
}
The AndroidService()
method can be used as follow making sure that the Service is running before using OpenAsync()
method:
AndroidService().ContinueWith(res =>
{
capture.OpenAsync(appId, developerId, appKey)
.ContinueWith(result =>
{
// Events...
});
});
Deep Sleep/Idle/Doze Mode¶
After some time of inactivity, the Android device will fall into deep sleep mode. This mode breaks
all communications between the application and the Android Service. To re-enable the communication
you can use the lifecycle of the application OnResume()
triggering CloseAsync()
and OpenAsync()
methods from a Capture Helper instance CaptureHelper capture = new CaptureHelper()
.
Here is an example, in App.xaml.cs
use OnResume()
:
MainPage rootPage;
public App()
{
InitializeComponent();
rootPage = new MainPage();
MainPage = rootPage;
}
protected override void OnResume()
{
rootPage.ReEnableConnection();
}
The example above in App.xaml.cs
is valid for Xamarin but an extra has to be applied to Maui:
MainPage rootPage;
public App()
{
InitializeComponent();
MainPage = new AppShell();
rootPage = new MainPage(true);
}
protected override void OnResume()
{
rootPage.ReEnableConnection();
}
Note
In the provided Maui Sample App, MainPage(true)
is empty. It is using static variables populated by
the MainPage()
. In this case, the credentials (appId, developperID, appKey), the instance of Capture
Helper and the isFirstLaunch
Boolean should be static to be re-used.
Note
OnResume() will be triggered when the application switches from being in the backgroud to being in the foreground. This action is also valid coming back from deep sleep mode.
Then add the following method:
public void ReEnableConnection()
{
// List will be repopulated on OpenAsync()
_deviceList.Clear();
capture.CloseAsync().ContinueWith(result =>
{
if (SktErrors.SKTSUCCESS(result.Result))
{
isFirstLaunch = false;
Open();
}
});
}
Note
_deviceList
is the list of devices saved using the Device Arrival event. In the example provided, it
is used to select and trigger a scan via a button. Make sure that _deviceList.Clear()
is called
before Open()
and outside of CloseAsync()
.
The Open()
method refers to the following:
public void Open()
{
capture.OpenAsync(appId, developerId, appKey)
.ContinueWith(result =>
{
System.Diagnostics.Debug.Print("Open Capture returns {0}", result.Result);
if (SktErrors.SKTSUCCESS(result.Result))
{
if (isFirstLaunch)
{
capture.DeviceArrival += Capture_DeviceArrival;
capture.DeviceRemoval += Capture_DeviceRemoval; ;
capture.DecodedData += Capture_DecodedData;
// (Android-iOS) Check if SocketCam is enabled to set the Switch
GetSocketCamStatusInit();
}
}
});
}
Note
If you are re-using the instance of Capture Helper, already registered events should not be re-registered
upon using ReEnableConnection(). This is prevented by using the Boolean isFirstLaunch
.