[MOD] NFC With the Screen Off

Search This thread

Geniusdog254

Retired Recognized Developer
Jan 2, 2009
1,110
169
St. Louis
Wow. It's been a long time since I made anything useful. Not being able to use NFC with the screen off has always been a pet peeve of mine with my Nexus, so I figured I'd fix it ;)

This is a modified Nfc.apk, which is the system NFC service that runs at boot. It allows you to scan NFC tags either with the screen totally off, or with the screen on but still at the lockscreen. You get to choose which one you want to flash. Just download one of the zips below, and flash it via recovery (tested with ClockworkMod). To restore to stock, just pick the stock version.

The screen off version means you can ALWAYS scan an NFC tag. Don't have to turn the screen on or anything. Just touch the phone to the tag.

The lockscreen version lets you scan a tag whenever the screen is on, but it doesn't have to be unlocked. You can't scan while the screen is off, but you can just touch the power button to get to the lockscreen and then scan. If you have a passcode, no need to unlock your phone to scan a tag.

This has been compiled against CyanogenMod and tested on my own self-built tree based off CyanogenMod source. There is nothing here that should be specific to that ROM.

Devices
  • Sprint Galaxy Nexus (toroplus)
    • Fully working on CyanogenMod based ROMs
    • Fully functional on most AOKP ROMs. Throws random errors on some, but everything still works.
    • Not functional on some stock ROMs
  • GSM Galaxy Nexus (maguro)
    • Fully working on CyanogenMod based ROMs
    • Fully functional on most AOKP ROMs. Throws random errors on some, but everything still works.
    • Not functional on some stock ROMs
  • Verizon Galaxy Nexus (toro)
    • Untested as far as I know. If you test it let me know
    • Should be similar to other Galaxy Nexus SKUs
  • HTC One X
    • The user StephanSch has created a version for the One X. Get it in this post.
    • Only screen off seems to work, not lockscreen.
    • Don't know if this is for the Qualcomm S4 based One X or the Tegra 3 base. Not sure about carrier versions either.
  • Samsung Galaxy SIII
    • The user mayhemer has created a version for the SIII. Get it in this post.
    • I'm not aware of which version of the SIII this is for, now which ROM. As soon as I have info, I'll update it here.
  • Nexus S
    • Reported working from at least one user. Don't know GSM or CDMA version, nor which ROM.
    • Probably working on any CM9 build for either SKU, and possibly stock or AOKP.

It probably will not work on any manufacturer based skins on any other device.

If you try it on any other device, please let me know how it goes! I'll update the OP with your results!

Also of note, I do not know if this will allow you to use Wallet any differently than you can right now. I don't have any way to test it, and don't personally use Wallet. If you do, let me know whether it works or not so I can update this post!

Liability Disclaimer: As always, I hereby absolve myself of responsibility for any damage you cause to your phone by flashing this. There is inherent risk in modding your device, and by flashing anything in this thread you are taking that risk into your own hands. If something goes wrong, I will try and help, but I am not God.

Source Code Disclaimer: The only code modified here was given under the Apache v2 license. That means that I am not required to release source for this. It is an incredibly simple mod at this point, but I plan on taking it into the next level and creating a patch that makes this an option in settings, rather than a zip you have to flash. I will release source with this, but I want to wait until I clean it up and submit it to the CyanogenMod Gerrit as a full feature, rather than a cheap hack! Sorry if that disappoints anyone, I just wanted to get this out ASAP.

In the meantime, I explain what I'm doing in this post. That should be enough info if you're compiling from source, and might be enough if you are using a Smali edit. If you're critically concerned about it, shoot me a PM or an email and I'll try and help more.

How things stand for Jellybean:
I put this on hold for a while. The further I got into making this a toggleable option in settings, the deeper things ran into the system code. With the big change to Jellybean around the corner, I want to wait until Jellybean becomes the main CM branch so I can avoid the breaks that all the code churn will cause. I'm not done with this, but there's no point in putting the work in to make it fully functional when CM10 is around the corner to break it all. I'll get on it as soon as CM10 becomes closer to release. Before the code freeze for CM10.

In the mean time, @PHtQuE got ahold of me on Twitter and I told him what was happening. I gave him a couple pointers and he got it compiling on Jellybean at the same level it is right now on ICS. I may be wrong, but I believe this is his post right here. Regardless, this is a working screenoff version for JB.
 

Attachments

  • nfcmod_stock.zip
    340 KB · Views: 2,790
  • nfcmod_screenoff.zip
    340 KB · Views: 4,876
  • nfcmod_lockscreen.zip
    339.9 KB · Views: 3,090
Last edited:

Geniusdog254

Retired Recognized Developer
Jan 2, 2009
1,110
169
St. Louis
Nice!! So the screen of one is only if the screen is off without a lockscreen? :confused:

No. Sorry, I'll update the OP to be a little more clear.

Screen off means you can ALWAYS scan an NFC tag. Don't have to turn the screen on or anything. Just touch the phone to the tag.

The lockscreen version lets you scan a tag whenever the screen is on, but it doesn't have to be unlocked. You can't scan while the screen is off, but you can just touch the power button to get to the lockscreen and then scan. If you have a passcode, no need to unlock your phone to scan a tag.


This is nice! Thanks! One thing that I thought would be useful is to have a notification pull down toggle for NFC. so you think that's possible?

It's definitely possible. I'm working on getting the option in Settings now, and should have it working by the end of the day. Once there's an option in Settings, it should be super simple to add a notification bar toggle option as well. Haven't messed with that part before, but it's not too dissimilar from WiFi toggling, so I've got code to look at.

I was hoping to get the settings mod done in time to submit it to CM before they started the code freeze for CM9. Well, they went into code freeze this morning so I missed that. I'll still get it submitted ASAP, but don't expect to see it in the initial CM9 official build unless you use the nightlies. I'll of course update everything once I submit the patch and if it gets accepted.
 
Last edited:

BeerChameleon

Senior Member
Aug 21, 2008
16,212
1,172
Tucson,Arizona.
No. Sorry, I'll update the OP to be a little more clear.

Screen off means you can ALWAYS scan an NFC tag. Don't have to turn the screen on or anything. Just touch the phone to the tag.

The lockscreen version lets you scan a tag whenever the screen is on, but it doesn't have to be unlocked. You can't scan while the screen is off, but you can just touch the power button to get to the lockscreen and then scan. If you have a passcode, no need to unlock your phone to scan a tag.




It's definitely possible. I'm working on getting the option in Settings now, and should have it working by the end of the day. Once there's an option in Settings, it should be super simple to add a notification bar toggle option as well. Haven't messed with that part before, but it's not too dissimilar from WiFi toggling, so I've got code to look at.

I was hoping to get the settings mod done in time to submit it to CM before they started the code freeze for CM9. Well, they went into code freeze this morning so I missed that. I'll still get it submitted ASAP, but don't expect to see it in the initial CM9 official build unless you use the nightlies. I'll of course update everything once I submit the patch and if it gets accepted.

Darn it LOL i would love to have one that works with both :D

Great work tho, i will probaly get the lockscreen one :D
 

Rumble6059

Senior Member
Nov 12, 2008
502
56
Nottinghamshire
This is just what I've been looking for (FOR AGES) I know that it doesn't come like this from stock, due to the potential security risks, but sod it! Lol, i love it.
Thanks for the fantastic work :)

Rumble

Sent from my Galaxy Nexus using xda premium
 

Smurph82

Senior Member
Mar 17, 2011
249
45
Greensboro, NC
I think that this is a great feature. But I thought that the reason
the NFC only worked with the screen on and phone unlocked was
a security reason. Meaning someone could not just walk up to
you and scan your phone without you knowing. you would have to
have the phone on for anything to work. I think that the settings
idea is great that way if you want to open then you can have it open
if you want it closed then you can keep it like stock. Great work. Worked
good on my GNexus (Sprint).
 

Geniusdog254

Retired Recognized Developer
Jan 2, 2009
1,110
169
St. Louis
im curious as to the effect on battery life. how noticeable is the difference?

I can't speak for the battery life, as my phone is plugged in either for tethering or testing code all day. Shouldn't be too noticeable.

I think that this is a great feature. But I thought that the reason
the NFC only worked with the screen on and phone unlocked was
a security reason. Meaning someone could not just walk up to
you and scan your phone without you knowing. you would have to
have the phone on for anything to work. I think that the settings
idea is great that way if you want to open then you can have it open
if you want it closed then you can keep it like stock. Great work. Worked
good on my GNexus (Sprint).

Security is exactly the reason it doesn't come like this by default. It's also why I made two versions. If you're worried about security, then flash the lockscreen version and you have to turn the phone on to be able to scan.

Currently, there is no way that I know of to get anything malicious by scanning against someone's will. It doesn't just offer up credit card numbers when you scan it. If you have NFC Task manager on your phone, someone could scan it and make it start an app you have installed or turn off wifi or something mundane like that, but that's about it. Plus they'd have to get within ~5" of your pocket ;)
 

agentdr8

Senior Member
Mar 4, 2007
3,643
1,883
Cowtown, CA
In theory, it should work on any 4.0.4 base ROM, on any NFC device. No guarantees though

It would appear that the One X's Nfc.apk has different hardware support. When I flashed your zip, NFC ReTag couldn't detect NFC hardware any more. And the option for NFC disappeared in Settings. Nothing a restore of the original apk couldn't fix, but it's unfortunate it didn't work.

Any chance you'd be willing to share what edits you made? If it's smali, I can probably tweak the One X's apk to be similar.
 

Geniusdog254

Retired Recognized Developer
Jan 2, 2009
1,110
169
St. Louis
It would appear that the One X's Nfc.apk has different hardware support. When I flashed your zip, NFC ReTag couldn't detect NFC hardware any more. And the option for NFC disappeared in Settings. Nothing a restore of the original apk couldn't fix, but it's unfortunate it didn't work.

Any chance you'd be willing to share what edits you made? If it's smali, I can probably tweak the One X's apk to be similar.

It's actually compiled from source. I've never messed with Smali, so I don't know how useful this will be. The easiest way is to tweak a variable called POLLING_MODE in the NfcService class. There are a couple different settings. 3 is the default, 2 works on lockscreen or unlocked, and 1 works with the screen off, at the lockscreen, or unlocked.

I don't think Smali keeps variable or function names, so that might be of no use at all.. The other method, in a function called applyRouting() there are a couple if/else statements that check if the current state matches POLLING_MODE or not. If you take those out, then it should also work although for some reason that only seems to enable it to the lockscreen level.

You should be able to see a copy of the file here, if that helps.
 

Smurph82

Senior Member
Mar 17, 2011
249
45
Greensboro, NC
Security is exactly the reason it doesn't come like this by default. It's also why I made two versions. If you're worried about security, then flash the lockscreen version and you have to turn the phone on to be able to scan.

Currently, there is no way that I know of to get anything malicious by scanning against someone's will. It doesn't just offer up credit card numbers when you scan it. If you have NFC Task manager on your phone, someone could scan it and make it start an app you have installed or turn off wifi or something mundane like that, but that's about it. Plus they'd have to get within ~5" of your pocket ;)

I did not mean anything by my comment, I think that having to turn the phone
on to use it is a bit much. I like that you have the ability to run it form the lock
screen. I understand that right now you really cannot do much with the NFC,
hacker wise, other than changing settings you are not stealing information by
scanning. But being that NFC is still new(ish) I also feel that all the security
holes have not been discovered. I just wanted people to consider every angle.
I still think that you have created a great feature.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 73
    Wow. It's been a long time since I made anything useful. Not being able to use NFC with the screen off has always been a pet peeve of mine with my Nexus, so I figured I'd fix it ;)

    This is a modified Nfc.apk, which is the system NFC service that runs at boot. It allows you to scan NFC tags either with the screen totally off, or with the screen on but still at the lockscreen. You get to choose which one you want to flash. Just download one of the zips below, and flash it via recovery (tested with ClockworkMod). To restore to stock, just pick the stock version.

    The screen off version means you can ALWAYS scan an NFC tag. Don't have to turn the screen on or anything. Just touch the phone to the tag.

    The lockscreen version lets you scan a tag whenever the screen is on, but it doesn't have to be unlocked. You can't scan while the screen is off, but you can just touch the power button to get to the lockscreen and then scan. If you have a passcode, no need to unlock your phone to scan a tag.

    This has been compiled against CyanogenMod and tested on my own self-built tree based off CyanogenMod source. There is nothing here that should be specific to that ROM.

    Devices
    • Sprint Galaxy Nexus (toroplus)
      • Fully working on CyanogenMod based ROMs
      • Fully functional on most AOKP ROMs. Throws random errors on some, but everything still works.
      • Not functional on some stock ROMs
    • GSM Galaxy Nexus (maguro)
      • Fully working on CyanogenMod based ROMs
      • Fully functional on most AOKP ROMs. Throws random errors on some, but everything still works.
      • Not functional on some stock ROMs
    • Verizon Galaxy Nexus (toro)
      • Untested as far as I know. If you test it let me know
      • Should be similar to other Galaxy Nexus SKUs
    • HTC One X
      • The user StephanSch has created a version for the One X. Get it in this post.
      • Only screen off seems to work, not lockscreen.
      • Don't know if this is for the Qualcomm S4 based One X or the Tegra 3 base. Not sure about carrier versions either.
    • Samsung Galaxy SIII
      • The user mayhemer has created a version for the SIII. Get it in this post.
      • I'm not aware of which version of the SIII this is for, now which ROM. As soon as I have info, I'll update it here.
    • Nexus S
      • Reported working from at least one user. Don't know GSM or CDMA version, nor which ROM.
      • Probably working on any CM9 build for either SKU, and possibly stock or AOKP.

    It probably will not work on any manufacturer based skins on any other device.

    If you try it on any other device, please let me know how it goes! I'll update the OP with your results!

    Also of note, I do not know if this will allow you to use Wallet any differently than you can right now. I don't have any way to test it, and don't personally use Wallet. If you do, let me know whether it works or not so I can update this post!

    Liability Disclaimer: As always, I hereby absolve myself of responsibility for any damage you cause to your phone by flashing this. There is inherent risk in modding your device, and by flashing anything in this thread you are taking that risk into your own hands. If something goes wrong, I will try and help, but I am not God.

    Source Code Disclaimer: The only code modified here was given under the Apache v2 license. That means that I am not required to release source for this. It is an incredibly simple mod at this point, but I plan on taking it into the next level and creating a patch that makes this an option in settings, rather than a zip you have to flash. I will release source with this, but I want to wait until I clean it up and submit it to the CyanogenMod Gerrit as a full feature, rather than a cheap hack! Sorry if that disappoints anyone, I just wanted to get this out ASAP.

    In the meantime, I explain what I'm doing in this post. That should be enough info if you're compiling from source, and might be enough if you are using a Smali edit. If you're critically concerned about it, shoot me a PM or an email and I'll try and help more.

    How things stand for Jellybean:
    I put this on hold for a while. The further I got into making this a toggleable option in settings, the deeper things ran into the system code. With the big change to Jellybean around the corner, I want to wait until Jellybean becomes the main CM branch so I can avoid the breaks that all the code churn will cause. I'm not done with this, but there's no point in putting the work in to make it fully functional when CM10 is around the corner to break it all. I'll get on it as soon as CM10 becomes closer to release. Before the code freeze for CM10.

    In the mean time, @PHtQuE got ahold of me on Twitter and I told him what was happening. I gave him a couple pointers and he got it compiling on Jellybean at the same level it is right now on ICS. I may be wrong, but I believe this is his post right here. Regardless, this is a working screenoff version for JB.
    10
    JellyBean version

    Attached is a version for CM10 based JellyBean ROMs. This should work on CM10, AOKP and similar ROMs. Only the screenoff version is available, lockscreen version doesn't work. Make a backup of your Nfc.apk before applying this one!

    For Stock JB ROMs use the modded apk in http://forum.xda-developers.com/showpost.php?p=30015154&postcount=112

    Install instructions:
    1. backup your /system/app/Nfc.apk
    2. push Nfc_screenoff.apk to /system/app/Nfc.apk or use a root-enabled file manager to replace the file
    3. reboot

    For reference I'm also attaching a .diff of the changes I made. If you try this at home it's important to (bak)smali the classes.dex file and replace it in the original apk, recompiling it as a whole always broke NFC functionality for me.

    edit: cm-10-20120831-NIGHTLY and later use http://forum.xda-developers.com/showpost.php?p=31000269&postcount=138
    10
    CM10 version, updated

    CM10 recently (in cm-10-20120831-NIGHTLY) changed something about Nfc.apk, so here's the new mod
    6
    I just finished to create the packages compatible to the Samsung Galaxy S3. Maybe Geniusdog254 put them into the first post.
    5
    Here's a screenoff mod for CM10.1 for phones using Nfc.apk
    Rename to Nfc.apk and push to /system/app/
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone