5

In short:
On the Huawei Watch 2 it seems like NFC is available and enabled but NFC_FEAUTURE is not, hence nfc is not working properly.

Trying to develop the ability to simply read and display NFC tags on the Huawei Watch 2 raises some difficulties:

mNfcAdapter.enableForegroundDispatch(this,nfcPendingIntent, nfcIntentFilter, null);  

raises the error

java.lang.UnsupportedOperationException  

That implies the FEATURE_NFC is not available.
In MainActivity onCreate():

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);  //NfcAdapter mNfcAdapter
if (mNfcAdapter != null) {
  // Check if device supports NFC
  Log.i("NFC","Your device supports NFC");
}
// Check if NFC is enabled
if (mNfcAdapter.isEnabled()) {
    Log.i("NFC","NFC is Enabled");
}
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
// Device compatible for NFC support
   Log.i("NFC", "Device compatible for NFC support"); 
}

shows in console

... I/NFC: Your device supports NFC
... I/NFC: NFC is Enabled

but not

... I/NFC: Device compatible for NFC support

In other words

mNfcAdapter !=null and
mNfcAdapter.isEnabled() == true but (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC) == false

How is this possible?

Btw. my AndroidManifest.xml:

<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" android:required="true" />

see also Android NFC Tutorial and a similar post NFC Android wear (Huawei watch 2.0)

What am I doing wrong? Is NFC on the Huawei Watch 2 somehow locked or disabled?

Thanks in advance for helping.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • I am trying something similar on the LG Sport Watch (with Google Pay and NFC)... would you mind sharing your test code so I can quickly verify? Maybe we can bring a case forward to Google together. – Billy Jul 18 '18 at 11:27
  • For your reference... on LG Watch Sport, NfcAdapter.getDefaultAdapter(this) returns null, and getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC) return false. No luck there. – Billy Jul 19 '18 at 00:40

2 Answers2

2

Based on this documentation, NFC feature is supported in Huawei Watch 2 and it also features Android Wear 2.0.

However, the error java.lang.UnsupportedOperationException means that the method isn't implemented yet by the framework authors, so you might contact the support team for clarifications. See this link.

abielita
  • 13,147
  • 2
  • 17
  • 59
  • Thanks for your quick answer. Im about to get in contact with the Huawei developer support. I was hoping that there is someone else with the same problem. I will post the result of the support process. – Robin Böhm Jan 29 '18 at 11:59
  • 1
    The Huawei developer support has no phone number and doesn't answer emails. Anybody else, please? – Robin Böhm Feb 08 '18 at 11:11
  • @RobinBöhm have you managed to make any progress? I'm now facing the same problem. – Sintho Oct 02 '18 at 19:22
  • @sintho sadly not. Did you? I'm trying to face the issue again atm. – Robin Böhm Nov 26 '18 at 11:58
  • @RobinBöhm not with the Huawei Watch 2. I've switched to the Sony SmartWatch 3 SWR50, for which a patched kernel with enabled NFC exists. For my small study project this is good enough, but it's obviously not a solution for 'real-world' projects. – Sintho Nov 26 '18 at 15:23
1

In case you want to create a prototype which makes use of the NFC feature on a Huawei Watch 2 you could circumvent this bug by forcing the feature flag to true.

First create a function which is able to set a static property through reflection:

fun setStaticValue(className: String, fieldName: String, newValue: Any) {
    val field = Class.forName(className).getDeclaredField(fieldName)

    field.setAccessible(true)

    val oldValue = field.get(Class.forName(className));

    field.set(oldValue, newValue);
}

Then use the function just before you are calling a method which tests for the feature flag like this:

setStaticValue("android.nfc.NfcAdapter", "sHasNfcFeature", true)

I don't think this hack will be accepted for released apps though, but I was able to recognize tags using this method.

I've also send a bug report to Huawei, so let's hope they will fix it.

For more information using reflection see: http://blog.sevagas.com/?Modify-any-Java-class-field-using-reflection

rhalff
  • 181
  • 2
  • 4