I'm literally struggling to implement a NFC tag reading functionality in my native app.
As it is, it's a very simple app with a list and some rest calls to interface with my server.
Now I would like to be able, as soon as my app is "on screen" to read the content of a compatible NFC TAG. It's really frustrating.
Can you please help me?
The reference I'm following:
- - sort of guide: guides/native-application/connectivity-and-wireless/nfc
- - NFC manager API
- - NFC tag API
First problem: in the "example code" in the guide, gmainloop is used but.. GMainLoop is not a valid identifier! So I had to comment out all the lines referring to that. Quite crazy.
Second problem: should I start listening for NFC events in my app_resume, while cleaning up everything NFC related in app_suspend?
Third problem: how do you print a meaningful error, if that error is an enum?
My actual code:
static void
on_nfc_ndef_discovered(nfc_ndef_message_h message, void *user_data)
{
int count;
int i;
unsigned int size;
unsigned char * payload;
nfc_ndef_record_h record;
nfc_ndef_message_get_record_count(message, &count);
dlog_print(DLOG_INFO, LOG_TAG, "on_nfc_ndef_discovered %d", count);
for (i = 0; i < count; i++) {
nfc_ndef_message_get_record(message, i, &record);
nfc_ndef_record_get_payload(record, &payload, &size);
dlog_print(DLOG_INFO, LOG_TAG, "Record Number: %d, Payload: %s", i, payload);
}
}
static void on_nfc_tag_discovered(nfc_discovered_type_e type, nfc_tag_h tag, void *user_data) {
// (nfc_tag_h tag, nfc_tag_type_e *type)
//int tagType = nfc_tag_get_type(tag, type);
dlog_print(DLOG_INFO, LOG_TAG, "on_nfc_tag_discovered, tagType: %d", 1);//=tagType);
}
void nfcInit(void) {
int error_code = NFC_ERROR_NONE;
error_code = nfc_manager_initialize();
if (NFC_ERROR_NONE != error_code) {
/* Error occurred */
dlog_print(DLOG_ERROR, LOG_TAG, "(NFC) nfcInit error");
}
//g_timeout_add(1000, timeout_func, gmainloop);
//g_main_loop_run(gmainloop);
nfc_manager_set_tag_filter(NFC_TAG_FILTER_ALL_ENABLE);
error_code = nfc_manager_set_ndef_discovered_cb(on_nfc_ndef_discovered, NULL);
if (NFC_ERROR_NONE != error_code)
dlog_print(DLOG_ERROR, LOG_TAG, "(NFC) nfcInit nfc_manager_set_ndef_discovered_cb error");
error_code = nfc_manager_set_tag_discovered_cb(on_nfc_tag_discovered, NULL);
if (NFC_ERROR_NONE != error_code)
dlog_print(DLOG_ERROR, LOG_TAG, "(NFC) nfcInit nfc_manager_set_tag_discovered_cb error, %i", error_code);
dlog_print(DLOG_ERROR, LOG_TAG, "(NFC) nfcInit COMPLETE");
}
void
Network_NFC_startup(void)
{
//GMainLoop* gmainloop = g_main_loop_new(NULL, FALSE);
bool is_nfc_supported = nfc_manager_is_supported();
if (!is_nfc_supported) {
dlog_print(DLOG_INFO, LOG_TAG, "(NFC) is_nfc_supported NOT SUPPORTED");
}
else {
dlog_print(DLOG_INFO, LOG_TAG, "(NFC) is_nfc_supported SUPPORTED");
nfcInit();
}
}
void
Network_NFC_cleanup(void)
{
//g_main_loop_unref(gmainloop);
nfc_manager_deinitialize();
}
static void
app_pause(void *data)
{
/* Take necessary actions when application becomes invisible. */
dlog_print(DLOG_INFO, LOG_TAG, "app_pause\n");
Network_NFC_cleanup();
}
static void
app_resume(void *data)
{
/* Take necessary actions when application becomes visible. */
dlog_print(DLOG_INFO, LOG_TAG, "app_resume\n");
Network_NFC_startup();
}
I get:
(NFC) is_nfc_supported SUPPORTED
But the error is: I can't even get to register the callback for nfc_manager_set_tag_discovered_cb:
(NFC) nfcInit nfc_manager_set_tag_discovered_cb error, -1073741822
(Please, let me know how to print "error" in some human readable format..).
This is *VERY FRUSTRATING*. I'm not that good with C, but I really think that Tizen doc should be able to help a little more..
Can you please help me?