How to set time in Dragonboard programmatically in Android

Hi @doitright,
I had changed it to following

<!--Allows applications to set the system time -->
<uses-permission  android:name="android.permission.SET_TIME" />
<uses-permission  android:name="android.permission.SET_TIME_ZONE" />

and now I am getting following warning in Manifest file.

permission is only granted to system apps

Although, it compiled and installed the application but terminated immediately with following logcat

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timesetting, PID: 7887
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timesetting/com.example.timesetting.MainActivity}: java.lang.SecurityException: setTime: Neither user 10099 nor current process has android.permission.SET_TIME.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
 Caused by: java.lang.SecurityException: setTime: Neither user 10099 nor current process has android.permission.SET_TIME.
    at android.os.Parcel.readException(Parcel.java:1546)
    at android.os.Parcel.readException(Parcel.java:1499)
    at android.app.IAlarmManager$Stub$Proxy.setTime(IAlarmManager.java:213)
    at android.app.AlarmManager.setTime(AlarmManager.java:545)
    at com.example.timesetting.MainActivity.onCreate(MainActivity.java:36)
    at android.app.Activity.performCreate(Activity.java:6033)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 
I/Process: Sending signal. PID: 7887 SIG: 9
Application terminated.

Please check updated code at github.

Are you just installing it as a regular user application? You can’t set time from a user application. Imagine if you installed an application on your phone and it decided to change the system time… that would be very bad since it has the potential to screw up meeting notifications, alarm clocks, etc. I thought that you would understand this, since all of the reference links you have in your original post said this clearly.

Your only option for this, if it must be done, is to install the application AS a system application, which means copy the apk file into /system/app/ and reboot.

Hi @doitright,
Yes, I do understand it. I had generated .apk then pushed into /system/app folder thereafter I had rebooted it but It did not work. I had tried almost all available options. Can you please check in your board and let me know if it is working there.

Did you uninstall it from the user partition first? Or factory reset? And check the logcat to see what it says upon installation.

Note: I do NOT work for Linaro. I don’t have easy access to a db410c for testing/debugging other user’s issues, so I won’t be testing or debugging your software for you. Frankly, I wouldn’t expect anyone at Linaro to do that either, since your issue really is a general Android thing rather than a db410c specific thing.

What I would ask you, however, is for an explanation as to WHY you are trying to set the system time from an Android application. There is probably a better way to accomplish what you are trying to do.

Hi @doitright ,
I do not have internet support in DB410c so I am sending timestamp via BLE at startup to set time so that in future DB410c should give right timestamp as per current date. Otherwise, via internet it was automatically updating the time.

I will try to figure it out. Although, I had tried almost all possible way, I might have missed something somewhere.

Have you considered putting an RTC on it?

Hi @doitright ,
This is how, currently, I have made it working temporarily. RTC had added extra cost and a little complexity that’s why I am not preferring to add that.

Ok, so I’m understanding then that you are trying to manufacture a product that includes a db410c, and that it is connecting up to something else via BTLE, and that other thing has rtc or network. Is that correct?

Yes. You have got it @doitright. Although, end product would not include db410c. I already have working custom board with Snapdragon. It is 100% portable from db410c to custom board.

Well heck, if you’re using a custom board, then you don’t need to add an external RTC – it already has one, you just need to add a battery for it.

@doitright exactly but the problem is currently don’t have RTC/battery support in the current version.

Don’t tell me that you’ve gone ahead and ordered a bulk print of your board before you have your product fully shaken down… Just add the changes to the design so that next run has rtc. For this run, use external rtc and call it good. There is no sense in implementing weird hacks that you will have to support long term, I’m sure it will end up costing a lot more to do that in the long run.

Anyhow, if you really want to do it without using RTC, then there are a number of options;

  1. Get that application running properly as a system application,
  2. Start a daemon from an init script that waits for connection from your application (i.e., bypass the system security),
  3. network over bluetooth to use NTP.

Can you provide more details about the bluetooth device you are connecting to?

@doitright Nah, just got few boards for internal testing. Next rev would have RTC and battery support on board but that is at least 2 months away and I can’t wait till then. Therefore, I am trying to make it working so that other things should not stuck because of that.

It is connecting with the mobile device over the BLE.

Phone should be able to do Bluetooth classic, which makes it trivial to set up a pan network for NTP.

You can even automate it.

On your phone run this:

On the dragonboard run this:

Note that it was built for Android 9, so it would probably need some work to make it work with 5.1.

As soon as the pan network connects, it will update the time over NTP.

@doitright Thanks for sharing insights. I would try this once I get a microphone working. I meantime, one of my colleagues is trying to install AOSP after that It might run without a problem.

BTW did you try Linaro AOSP instead of this old no more supported one.

Hi Loic,
I am trying to build that. It tooks almost 70GB internet bandwidth. I had not realised that. Will update you once I get that back to working.

@Loic
In meantime, can you please let me know how can I install this patch on my current version of Android 5.1.1.

Patch Link: https://www.codeaurora.org/patches/quic/la/104166_LA.BR.1.2.4-00310-8x16.0_410C_06162015.tar.gz
Patch Post Link: Audio and Microphone on Android OS for DragonBoard™ 410c - #9 by doitright

These patches seem to apply in different AOSP sub-projects, I’m affraid you need to look at patch title or path to deduct in which project you need to apply them (with patch or git apply command).

Ok @Loic. I will see it.