android oreo/pie - paho mqtt Unable to start activity in background - mqtt

I'm trying to solve paho mqtt running on android pie(9.0).
I'm running my mqtt connection in Service class (return START_STICKY) and connecting to UI with bind... All is working perfectly fine but WHAT IS NOT WORKING is
when app is killed, Service and mqtt connection stops. Mqtt
AlarmPingSender stop working.. Tried to test it on Nougat(7.0) and it's working perfectly fine.
Is there a solution to make mqtt paho library working on new version of Android?
LOGCAT:
2019-03-22 02:35:08.854 12651-12651/com.example.dominik.smarthome2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dominik.smarthome2, PID: 12651
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dominik.smarthome2/com.example.dominik.smarthome2.MainActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.dominik.smarthome2/.MyService }: app is in background uid UidRecord{ee07604 u0a297 TPSL idle procs:1 proclist:12651, seq(0,0,0)}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.dominik.smarthome2/.MyService }: app is in background uid UidRecord{ee07604 u0a297 TPSL idle procs:1 proclist:12651, seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666)
at android.app.ContextImpl.startService(ContextImpl.java:1611)
at android.content.ContextWrapper.startService(ContextWrapper.java:677)
at com.example.dominik.smarthome2.MainActivity.onCreate(MainActivity.java:122)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3088)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3251) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7045) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 

Android 8.0 has introduced Background Execution Limits and thus you should use startForegroundService() instead of startService() to run a Foreground Service with visible notification:

Related

GRPC IOS Client Connection Lost -Objective C

For the IOS App the connection works , but after the phone is idle for a while , the connection is lost, as such the RPC calls hangs, without any response.
I had the same issue in JAVA, there I added a DeadLine and rebuilt the channel when the Deadline Exceeded, something like below.
stub.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS).execute(input, new StreamObserver<AgentGuardStringResponse>() { ... }
Then on error (deadline exceeded)
mChannel.shutdown();
This works fine.
For objective-c/ios
I set a time out for the RPC calls as so
[call setTimeout:timeout];
call.requestHeaders[#"sessionId"] = sessionId;
[call start];
And Try to rebuild ,
_serviceClient = [[AgentGuardService alloc] initWithHost:GRPCMetadata.shared.uri];
This does not seem to work as the app stays idle for a while after starting and eventually the Requests start flowing through.
Any pointers/guides will be highly helpful.
Thanks

Altbeacon and bootstrap always defaults to default beacon format

I have used the Alt-Beacon reference application as a starting point on how to implement bootstrap regions. The application restarts if killed but the beacon format is always being set to Alt-Beacon and not iBeacon.
Logcat on startup:
D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
Bootstrap code:
beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().clear();
beaconManager
.getBeaconParsers()
.add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.setBackgroundBetweenScanPeriod(5000l);
beaconManager.setBackgroundScanPeriod(1000l);
beaconManager.setRegionStatePersistenceEnabled(false);
Region region = new Region("backgroundRegion",null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
BackgroundPowerSaver backgroundPowerSaver = new BackgroundPowerSaver(this);
I would expect that when the app was restarted I should see:
m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24
I am using 2.15.2 of AltBeacon in Android Studio 3.1.3 with test device of Samsung S5 Mini running Android 6.0.1
Further information: When the phone is rebooted and the USB connected I would expect to see the app start and listen for iBeacon packets. It appears to restart but is killed by the OS.
Logcat:
12-07 17:32:08.132 8359-8359/net.simplesdktest I/InstantRun: starting instant run server: is main process
12-07 17:32:08.212 8359-8359/net.simplesdktest I/BeaconManager: BeaconManager started up on pid 8359 named 'net.simplesdktest' for application package 'net.simplesdktest'. isMainProcess=true
12-07 17:32:08.357 4889-4889/? I/TaskManager:PackageInfo:
getRunningAppProcesses() - proc:net.simplesdktest, pkg:net.simplesdktest, uid:10346, pid:8359
12-07 17:32:12.822 2390-3458/? I/ActivityManager: Killing 8359:net.simplesdktest/u0a346 (adj 15): DHA:empty #21
setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
This one detects ibeacon format BLE beacons doesn't it?
Source
It appears that when the beacon manager is first created it adds the AltBeacon format by default. I have managed to clear this out and set iBeacon which is now being detected.

How to keep .NET Core console app alive in Docker container

I am testing a .NET Core 2.0 app which uses the Service Bus SDK to retrieve messages from an Event Hub. I setup a console app to do that, and intend to run the app as a Docker container.
This method creates the Event Host Processor which will read the messages:
private static async Task MainAsync(string[] args)
{
Console.WriteLine("Registering EventProcessor...");
var eventProcessorHost = new EventProcessorHost(
EhEntityPath,
PartitionReceiver.DefaultConsumerGroupName,
EhConnectionString,
StorageConnectionString,
StorageContainerName);
// Registers the Event Processor Host and starts receiving messages
Console.WriteLine("Retrieving messages");
await eventProcessorHost.RegisterEventProcessorAsync<EventProcessor>();
Console.WriteLine("Sleeping");
Thread.Sleep(Timeout.Infinite);
}
As the event processor implemented in class EventProcessor will be the one handling the events, I am trying to prevent the console app to exit when the registration of the Processor is finished.
However, I can't find a reliable way to keep the app alive. If I run this container as-is, all I see in the output window is:
Registering EventProcessor...
Retrieving messages
Sleeping
and no messages are ever received.
Thanks all for the suggestions.
I followed those articles but eventually ended up with this, which applies specifically to .NET Core apps:
https://github.com/aspnet/Hosting/issues/870
I've tested it and the app can shutdown gracefully when it receives a termination signal from the Docker runtime.
UPDATE: this is the relevant sample from the GH issue link above:
public class Program
{
public static void Main(string[] args)
{
var ended = new ManualResetEventSlim();
var starting = new ManualResetEventSlim();
AssemblyLoadContext.Default.Unloading += ctx =>
{
System.Console.WriteLine("Unloding fired");
starting.Set();
System.Console.WriteLine("Waiting for completion");
ended.Wait();
};
System.Console.WriteLine("Waiting for signals");
starting.Wait();
System.Console.WriteLine("Received signal gracefully shutting down");
Thread.Sleep(5000);
ended.Set();
}
}

Error running the initialised React Native project in Xcode

After installing react-native and running the newly created Xcode project I am getting the following output:
2015-03-26 15:35:05.865 React[1427:7950] CLTilesManagerClient: initialize, sSharedTilesManagerClient
2015-03-26 15:35:05.865 React[1427:7950] CLTilesManagerClient: init
2015-03-26 15:35:05.866 React[1427:7950] CLTilesManagerClient: reconnecting, 0x7fac1b289e40
RCTJSLog> "Running application "React" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF"
RCTJSLog> "Error:
stack:
React_render index.ios.bundle:955
_renderValidatedComponentWithoutOwnerOrContext index.ios.bundle:16453
_renderValidatedComponent index.ios.bundle:16479
ReactCompositeComponent__renderValidatedComponent index.ios.bundle:4860
mountComponent index.ios.bundle:15913
ReactCompositeComponent_mountComponent index.ios.bundle:4860
mountComponent index.ios.bundle:4929
mountComponentIntoNode index.ios.bundle:15386
perform index.ios.bundle:5992
batchedMountComponentIntoNode index.ios.bundle:15405
perform index.ios.bundle:5992
batchedUpdates index.ios.bundle:13507
batchedUpdates index.ios.bundle:4524
renderComponent index.ios.bundle:15457
ReactMount__renderNewRootComponent index.ios.bundle:4860
render index.ios.bundle:1155
renderApplication index.ios.bundle:33797
run index.ios.bundle:33743
runApplication index.ios.bundle:33765
jsCall index.ios.bundle:7149
_callFunction index.ios.bundle:7396
applyWithGuard index.ios.bundle:877
guardReturn index.ios.bundle:7198
callFunctionReturnFlushedQueue index.ios.bundle:7405
URL: http://localhost:8081/index.ios.bundle
line: 955
message: undefined is not a function (evaluating 'React.createElement(Text, {style: styles.welcome},
"Welcome to React Native!"
)')"
Any help figuring out why this is failing would be appreciated.
Thanks!
To run on a device (which is your second problem), take a look at these comments from the React Native examples:
https://github.com/facebook/react-native/blob/master/Examples/Movies/Movies/AppDelegate.m
To run on device, change localhost to the IP address of your
computer, and make sure your computer and iOS device are on the same
Wi-Fi network.
I think this is because the application needs to connect to the packager. See if that helps.

MQ Connection - 2009 error

am connectting the MQ with below code. I am able connected to MQ successfully. My case is i place the messages to MQ every 1 min once. After disconnecting the cable i get a ResonCode error but IsConnected property still show true. Is this is the right way to check if the connection is still connected ? Or there any best pratcices around that.
I would like to open the connection when applicaiton is started keep it open for ever.
public static MQQueueManager ConnectMQ()
{
if ((queueManager == null) || (!queueManager.IsConnected)||(queueManager.ReasonCode == 2009))
{
queueManager = new MQQueueManager();
}
return queueManager;
}
The behavior of the WMQ client connection is that when idle it will appear to be connected until an API call fails or the connection times out. So isConnected() will likely report true until a get, put or inquire call is attempted and fails, at which point QMgr will then report disconnected.
The other thing to consider here is that 2009 is not the only code you might get. It happens to be the one you get when the connection is severed but there are connection codes for QMgr shutting down, channel shutting down, and a variety of resource and other errors.
Typically for a requirement to maintain a constant connection you would want to wrap the connect and message processing loop inside a try/catch block nested inside a while statement. When you catch an exception other than an intentional exit, close the objects and QMgr, sleep at least 5 seconds, then loop around to the top of the while. The sleep is crucial because if you get caught in a tight reconnect loop and throw hundreds of connection attempts at the QMgr, you can bring even a mainframe QMgr to its knees.
An alternative is to use a v7 WMQ client and QMgr. With this combination, automatic reconnection is configurable as a channel configuration.

Resources