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
andmNfcAdapter.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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With