找回密码
 立即注册
查看: 3949|回复: 0

NFC:管理近场无线通信应用开发

[复制链接]
发表于 2015-8-8 22:50:43 | 显示全部楼层 |阅读模式
NFC API
本教程演示了如何管理近场通信(NFC)和发送和接收NFC数据交换格式(NDEF)消息使用NFC的点对点(P2P)模式。
热身
熟悉NFC API通过学习基本知识:

初始化NFC
初始化NFC使用。

启用和禁用NFC
启用或禁用NFC使用切换应用程序。

使用NFC
NFC异步进行初始化,设置NFC标记过滤器,并注册所有必要的NFC回调。

缓存的NFC的消息
初始化同步NFC和NFC缓存消息

NFC P2P
初始化NFC P2P
初始化NFC P2P使用。

通过NFC P2P发送和接收消息
2之间使用NFC P2P NDEF消息交换设备。



初始化NFC

近场通信(NFC)是一个国际标准( ISO / IEC 18092 ),指定一个接口和一个紧密耦合的简单无线互连设备的协议。 有3组为NFC应用场景:

设备接近无线标签
设备接近另一个设备(目标)
设备接近销售点终端
在NFC教程 NFC API用于编写应用程序初始化NFC适配器和注册回调函数称为每个连接的场景中,因此用户可以使用手动NFC。 通过连接你的设备到另一个NFC点(标签、设备或销售终端),您可以调用指定的回调注册。 本教程的第二个场景展示如何得到消息缓存NFC。

初始化NFC:

使用的函数和数据类型 NFC API,包括 < nfc.h > 头文件在您的应用程序:
  1. <font size="4">#include <nfc.h></font>
复制代码

检查设备是否你想使用支持NFC。 这可以通过调用完成 nfc_Manager_is_supported () 功能。 不需要参数,它只是回报 true 如果支持NFC设备和 false 否定。
  1. <font size="4">void Network_NFC_startup(void)
  2. {
  3.    gmainloop = g_main_loop_new(NULL, FALSE);
  4.    bool is_nfc_supported = nfc_manager_is_supported();
  5.    if (!is_nfc_supported)
  6.       dlog_print(DLOG_INFO, LOG_TAG, "is_nfc_supported NOT SUPPORTED");
  7. }</font>
复制代码

gmainloop 这里被创造出来,它是用来等待调用异步函数的结果。
NFC的工作是完成时, nfc_manager_deinitialize () 函数必须调用清理环境。
  1. <font size="4">void Network_NFC_cleanup(void)
  2. {
  3.    g_main_loop_unref (gmainloop);
  4.    nfc_manager_deinitialize();
  5. }</font>
复制代码




启用和禁用NFC

启用或禁用NFC:

NFC API不包含启用或禁用NFC功能。 使用NFC切换应用程序见下图(从屏幕左边和右边的屏幕上)。

图:NFC切换应用程序

  1. <font size="4">#include <app_control.h>
  2. #include <dlog.h>

  3. int nfc_onoff_operation(void)
  4. {
  5.    int ret = 0;
  6.    app_control_h service = NULL;
  7.    app_control_create(&service);
  8.    if (service == NULL)
  9.    {
  10.       dlog_print(DLOG_INFO, LOG_TAG, "service_create failed!\n");

  11.       return 0;
  12.    }
  13.    app_control_set_operation(service,  "http://tizen.org/appcontrol/operation/setting/nfc");
  14.    app_control_add_extra_data(service,  "type", "nfc");
  15.    ret = app_control_send_launch_request(service, NULL, NULL);

  16.    app_control_destroy(service);
  17.    if (ret == APP_CONTROL_ERROR_NONE)
  18.    {
  19.       dlog_print(DLOG_INFO, LOG_TAG, "Succeeded to NFC On/Off app!\n");

  20.       return 0;
  21.    }
  22.    else
  23.    {
  24.       dlog_print(DLOG_INFO, LOG_TAG, "Failed to relaunch NFC On/Off app!\n");

  25.       return -1;
  26.    }

  27.    return 0;
  28. }</font>
复制代码




使用NFC

使用手动NFC,您需要:

初始化NFC
注册通知
使用NFC手动
清理结束时
初始化NFC

初始化NFC:

第一个函数使用 nfc_manager_initialize () 。
  1. <font size="4">int error_code = NFC_ERROR_NONE;

  2. error_code = nfc_manager_initialize();
  3. if (NFC_ERROR_NONE != error_code) // Error occurred

  4. g_timeout_add(1000, timeout_func, gmainloop);
  5. g_main_loop_run(gmainloop);</font>
复制代码


后调用 nfc_manager_initialize () 函数运行 gmainloop 等待初始化的结果。 它是关闭的时候设置的 g_timeout_add 函数白白的流逝了。 这一次是在毫秒 timeout_func 1秒过后。

2.在初始化完成后,调用 nfc_manager_set_activation_changed_cb () 功能。 每次调用函数注册回调,NFC的激活状态的变化。 的参数是:

激活状态改变回调
数据传递给回调函数
  1. <font size="4">error_code = nfc_manager_set_activation_changed_cb(on_nfc_activation_changed, NULL);</font>
复制代码

代码 on_nfc_activation_changed 回调很简单,如下所示:
  1. <font size="4">static void on_nfc_activation_changed(bool activated, void *user_data)
  2. {
  3.    if (activated)
  4.       dlog_print(DLOG_INFO, LOG_TAG, "NFC activated");
  5.    else
  6.       dlog_print(DLOG_INFO, LOG_TAG, "NFC deactivated");
  7. }</font>
复制代码

只在我们的例子中,这个回调通知用户,激活状态已经改变了。

3.在这一步中, nfc_manager_set_tag_filter () 使用函数。 它声明标签过滤选项。 使用位操作的类型 nfc_tag_filter_e 指定类型的过滤。 默认值是 NFC_TAG_FILTER_ALL_ENABLE ,这意味着所有的标记类型启用。
  1. <font size="4">nfc_manager_set_tag_filter(NFC_TAG_FILTER_ALL_ENABLE);</font>
复制代码


4.你需要注册回调函数为标签接收发现通知,NDEF,点对点,安全元素,元素和安全事务的事件。 为接收标记发现通知注册一个回调函数,使用 nfc_manager_set_tag_discovered_cb () 功能。 第一个参数是 on_nfc_tag_discovered () 回调函数描述后面的教程。 第二个参数是一个用户数据参数。 在我们的例子中,参数是不需要的,你可以通过 空 价值函数。 同样,NDEF注册,点对点,安全元素,元素和安全事务事件通知使用:

使用 nfc_manager_set_ndef_discovered_cb () 函数来注册一个NDEF事件通知的 on_nfc_ndef_discovered () 回调函数
nfc_manager_set_p2p_target_discovered_cb () 注册对等的事件通知 on_nfc_p2p_target_discovered () 回调函数
nfc_manager_set_se_event_cb () 注册安全元素的事件通知 on_nfc_se_event () 回调函数
nfc_manager_set_se_transaction_event_cb () 注册安全事务事件通知的元素 on_nfc_se_transaction_event () 回调函数
  1. <font size="4">error_code = nfc_manager_set_tag_discovered_cb(on_nfc_tag_discovered, NULL);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred

  3. error_code = nfc_manager_set_ndef_discovered_cb(on_nfc_ndef_discovered, NULL);
  4. if (NFC_ERROR_NONE != error_code) // Error occurred

  5. error_code = nfc_manager_set_p2p_target_discovered_cb(on_nfc_p2p_target_discovered, NULL);
  6. if (NFC_ERROR_NONE != error_code) // Error occurred

  7. error_code = nfc_manager_set_se_event_cb(on_nfc_se_event, NULL);
  8. if (NFC_ERROR_NONE != error_code) // Error occurred

  9. error_code = nfc_manager_set_se_transaction_event_cb(NFC_SE_TYPE_ESE, on_nfc_se_transaction_event, NULL);
  10. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码


5.检查系统处理标签和目标是否发现启用或禁用。 默认情况下是启用的。 使用 nfc_manager_is_system_handler_enabled () 函数来检查当前状态。 如果函数返回 假 使用,使系统处理 nfc_manager_set_system_handler_enable () 功能和通过 真正的 值作为输入参数。
  1. <font size="4">if (nfc_manager_is_system_handler_enabled() != true)
  2.    nfc_manager_set_system_handler_enable(true);</font>
复制代码




使用NFC手动

初始化NFC之后,您可以开始使用NFC设备上。 您可以连接到其他设备,导致启动注册回调函数的代码。

使用NFC标签

当发现一个标签, on_nfc_tag_discovered () 调用回调注册之前。 函数的第一个参数是NFC发现的类型。 第二个标记处理,最后一个是用户数据。 在上述例子中, on_nfc_tag_discovered () 回调打印出的信息发现标签。

这个回调之初,NFC发现类型检查。 它可以 NFC_DISCOVERED_TYPE_ATTACHED 如果标签和连接到设备 NFC_DISCOVERED_TYPE_DETACHED 如果标签是分离的。 如果标签附呈,你可以打印出信息读取它。

标签的类型, nfc_tag_get_type () 函数必须调用。 它需要2参数。 第一个是标签的处理,第二个是一个指针( nfc_tag_type_e )变量,存储的类型标记。
  1. <font size="4">nfc_tag_get_type(tag, &tag_type);</font>
复制代码

你也可以NDEF消息的大小通过调用存储在标签 nfc_tag_get_ndef_size () 功能。 上述参数是类似的,但不是 tag_type 变量的指针传递给一个无符号 int 变量存储在当前标签的大小。
  1. <font size="4">nfc_tag_get_ndef_size(tag, &size);</font>
复制代码

nfc_tag_get_maximum_ndef_size () 函数可以用来获取最大NDEF消息大小,可以存储在标签。 它还需要2个参数,第一个是标记处理,第二个是一个无符号的指针 int 标签的最大大小的存储位置。
  1. <font size="4">nfc_tag_get_maximum_ndef_size(tag, &maximum_ndef_bytes_size);</font>
复制代码

获得基本的标签信息后,通过调用检索所有剩余的标签信息 nfc_tag_foreach_information () 功能。 此函数接受三个参数。 第一个是操作的标记。 第二个参数是一个回调是呼吁每个发现一对键和值的标签。 最后一个是用户数据,可以传递给回调,但不需要通过任何数据, NULL 是过去了。
  1. <font size="4">error_code = nfc_tag_foreach_information(tag, on_nfc_tag_information_event, NULL);

  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码


on_nfc_tag_information_event () 回调在本教程是很简单的。 它只是打印出发现键和值。
  1. <font size="4">on_nfc_tag_information_event(const char *key, const unsigned char *value, int value_size, void *user_data)
  2. {
  3.    dlog_print(DLOG_INFO, LOG_TAG, "Title: %s, Value: %s", key, (char*)value);

  4.    return true;
  5. }</font>
复制代码

true 返回继续迭代所有其他双键和值。 如果 false 返回,然后迭代停止。

这些操作后,检查nfc标记过滤器并打印出来。 这样做可以确保我们,回调是打电话告诉我们什么样的标签现在过滤器设置。 例如,如果回调没有印刷任何关于标签的信息,但它打印出标签过滤器 NFC_TAG_FILTER_ALL_DISABLE ,那么你知道过滤器需要设置为适当的值读取特定的标记类型。 检查当前标记过滤器,您可以使用 nfc_manager_get_tag_filter () 功能。
  1. <font size="4">filter = nfc_manager_get_tag_filter();</font>
复制代码

最后留下的回调是描述 nfc_tag_read_ndef () 功能。 它使阅读标签数据。 使用3个参数:

标签处理
阅读完成时调用的回调
用户数据传递给回调函数
  1. <font size="4">error_code = nfc_tag_read_ndef(tag, on_nfc_tag_read_completed, NULL);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

读取标签信息后, on_nfc_tag_read_completed () 调用回调。 它有三个参数:

error_code ——定义读取消息的状态
message ( nfc_ndef_message_h )- - -从标签读取消息
user_data -数据传递给回调, 空 如果没有数据需要传递
  1. <font size="4">static void on_nfc_tag_read_completed(int error_code, nfc_ndef_message_h message, void *user_data)
  2. {
  3.    if (!found_error(NFC_ERROR_NONE, "nfc_manager_set_activation", error_code))
  4.       nfc_ndef_message_read_cb(clone_message(message));
  5. }</font>
复制代码

如果没有错误( error_code = = NFC_ERROR_NONE ),您可以使用检索到的信息。 展示如何使用其他NFC API功能,2额外的功能了。 首先,克隆消息通过调用 clone_message () 功能:
  1. <font size="4">static nfc_ndef_message_h clone_message(nfc_ndef_message_h msg)
  2. {
  3.    unsigned char *rawdata;
  4.    unsigned int rawdata_size;
  5.    nfc_ndef_message_h msg_cp;

  6.    nfc_ndef_message_get_rawdata(msg, &rawdata, &rawdata_size);
  7.    nfc_ndef_message_create_from_rawdata(&msg_cp, rawdata, rawdata_size);
  8.    free(rawdata);

  9.    return msg_cp;
  10. }</font>
复制代码

可以看到,这里使用2 NFC API函数。 第一个是 nfc_ndef_message_get_rawdata () 功能。 它的一个副本NDEF消息的字节数组。 需要三参数:

NDEF消息的处理
( out ] rawdata ——二维字节数组
( out ] rawdata_size ——大小的字节数组
( out ] 参数变量,必须在调用函数传递给应验。

最后, rawdata 必须发布免费的()。

第二个功能是 nfc_ndef_message_create_from_rawdata () ,它创建一个NDEF消息处理从原始串行字节。 使用它来创建一个副本从标签检索的消息。 三个参数需要提供:

(out)处理消息( nfc_ndef_message_h )
rawdata
rawdata_size
现在让我们来讨论第二个函数准备显示NFC API的功能。 需要作为一个参数标记消息的处理。 通过克隆消息。
  1. <font size="4">nfc_ndef_message_read_cb(clone_message(message));</font>
复制代码

首先,获得数量的记录存储在标签信息。 要做到这一点,使用 nfc_ndef_message_get_record_count () 功能。 它需要处理的信息操作和一个整数的指针实现的功能和记录在消息的数量。
  1. <font size="4">error_code = nfc_ndef_message_get_record_count(message, &count);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

遍历所有记录在消息和获得所有信息存储在每个记录。 第一个调用的函数 nfc_ndef_message_get_record () 。 这个函数从消息中获取一个记录的索引。 它返回一个指针记录如果你更改记录,它直接影响到NDEF消息。
  1. <font size="4">error_code = nfc_ndef_message_get_record(message, i, &rec);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码


参数:

NDEF消息的句柄
索引的记录
( out ] 处理记录
现在,当指针具体记录存在,应记录数据:

记录ID通过调用 nfc_ndef_record_get_id ()
  1. <font size="4">error_code = nfc_ndef_message_get_record(message, i, &rec);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

记录类型使用 nfc_ndef_record_get_type () 函数
  1. <font size="4">error_code = nfc_ndef_record_get_payload(record, &payload, &payload_len);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

  1. <font size="4">error_code = nfc_ndef_record_get_type(rec, &type_str, &type_len);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

记录TNF(类型名称格式) nfc_ndec_ndef_record_get_tnf(rec, &tnf);
if (NFC_ERROR_NONE != error_code) // Error occurred[/code]
记录有效载荷通过调用 nfc_ndef_record_get_payload () 函数
  1. <font size="4">error_code = nfc_ndef_record_get_payload(record, &payload, &payload_len);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

从标记来获得更多的信息,指定什么类型的标签信息处理。 如果有消息 类型 = “T” 和TNF NFC_RECORD_TNF_WELL_KNOWN ,那么它可以得到以下数据:
记录文本 nfc_ndef_record_get_text () 函数
  1. <font size="4">error_code = nfc_ndef_record_get_text(record, &text);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

通过使用记录文本语言代码 nfc_ndef_record_get_langcode () 函数
  1. <font size="4">error_code = nfc_ndef_record_get_langcode(record, &language);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

通过调用记录文本编码类型 nfc_ndef_record_get_encode_type ()
  1. <font size="4">error_code = nfc_ndef_record_get_encode_type(record, &encode);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

如果有消息 类型 = “U” 和TNF也是 NFC_RECORD_TNF_WELL_KNOWN ,可以使用URI nfc_ndef_record_get_uri () 功能:
  1. <font size="4">error_code = nfc_ndef_record_get_uri(record, &uri);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

最后,如果TNF的记录 NFC_RECORD_TNF_MIME_MEDIA ,那么它可以获得创纪录的mime类型。
  1. <font size="4">error_code = nfc_ndef_record_get_mime_type(record, &mime);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

使用NFC NDEF消息

它是相同的与所述NFC标记NDEF消息。 在注册回调 on_nfc_ndef_discovered () 消息中,记录的数量,然后遍历这些记录。 后让处理这些记录,这些记录的有效载荷:
  1. <font size="4">static void on_nfc_ndef_discovered(nfc_ndef_message_h message, void *user_data)
  2. {
  3.    int count, i;
  4.    unsigned int size;
  5.    unsigned char * payload;
  6.    nfc_ndef_record_h record;

  7.    nfc_ndef_message_get_record_count(message, &count);
  8.    dlog_print(DLOG_INFO, LOG_TAG, "on_nfc_ndef_discovered %d", count);

  9.    for (i=0; i<count; i++)
  10.    {
  11.       nfc_ndef_message_get_record(message, i, &record);
  12.       nfc_ndef_record_get_payload (record, &payload, &size);
  13.       dlog_print(DLOG_INFO, LOG_TAG, "Record Number : %d, Payload : %s", i, payload);
  14.    }
  15. }</font>
复制代码




使用NFC P2P的目标

本节讨论上调用的回调点对点目标的发现。 当设备被连接到一些P2P目标,可以交换NDEF数据与对等的目标。 它允许:

发送NDEF消息
接收NDEF消息
在本教程中,我们对这样的连接就称为注册回调 on_nfc_p2p_target_discovered () 。 它有以下参数:

NFC发现的类型( nfc_discovered_type_e )
处理的目标
用户数据
首先,检查第一个参数( 类型 type )。 如果类型 NFC_DISCOVERED_TYPE_ATTACHED ,就意味着远程设备被连接到设备。
  1. <font size="4">if (type == NFC_DISCOVERED_TYPE_ATTACHED)
  2. {
  3.    // Remote device is attached; execute code
  4. }</font>
复制代码

使用 nfc_p2p_set_data_received_cb () 调用函数来注册一个回调,当收到任何数据连接的目标。
  1. <font size="4">error_code = nfc_p2p_set_data_received_cb(target, on_nfc_p2p_read_completed, 0);
  2. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

注册的代码 on_nfc_p2p_read_completed () 回调是下面的:
  1. <font size="4">static void on_nfc_p2p_read_completed(nfc_p2p_target_h target, nfc_ndef_message_h message, void *user_data)
  2. {
  3.    int count;
  4.    dlog_print(DLOG_INFO, LOG_TAG, "on_nfc_p2p_read_completed");

  5.    nfc_ndef_message_get_record_count(message, &count);
  6.    nfc_ndef_message_read_cb(clone_message(message));
  7. }</font>
复制代码

阅读来自远程设备的消息之后,检查其使用的记录数量 nfc_ndef_message_get_record_count () 功能和得到更多的详细信息通过调用的消息 nfc_ndef_message_read_cb () 前面描述的函数。

使用NFC安全元素

收到事件通知通过安全元素 on_nfc_se_event () 功能。 第一个参数定义了事件类型。 第二个是用户数据——在本例中,它是不习惯。 根据事件类型,您可以采取一些额外的行动。
复制代码

安全元素事务事件给你4信息参数和用户数据。 你可以得到应用程序ID(ISO / IEC 7816 - 4)中指定的第一个参数,尺寸的援助从第二个参数,参数列表中指定(ISO / IEC 8825 - 1)的第三个参数,参数的大小从下一个函数的输入参数。
  1. <font size="4">static void on_nfc_se_transaction_event(nfc_se_type_e se_type, unsigned char *aid, int aid_size, unsigned char *param, int param_size, void *user_data)
  2. {
  3.   dlog_print(DLOG_INFO, LOG_TAG, "Secure Element(SIM/UICC(Universal Integrated Circuit Card)) transaction event data");
  4. }</font>
复制代码




缓存的NFC的消息

得到一个缓存的NFC的信息:

1.初始化NFC。
初始化NFC, nfc_manager_initialize () 可以使用函数,如图所示 初始化NFC 。

2.设置NFC标记过滤器。
设置NFC标记过滤器所示 使用NFC标签 )。 只是一个提醒,设置标记过滤器使用是可能的 nfc_manager_set_tag_filter () 功能。
  1. <font size="4">nfc_manager_set_tag_filter(NFC_TAG_FILTER_ALL_ENABLE);</font>
复制代码


3.使NFC系统处理程序。
之前缓存的信息,使系统处理程序:
  1. <font size="4">if (nfc_manager_is_system_handler_enabled() != true)
  2.    nfc_manager_set_system_handler_enable(true);</font>
复制代码


4.缓存的信息。
通过调用缓存消息 nfc_manager_get_cached_message () 功能。 通过的一个变量 nfc_ndef_message_h 类型,它是实现缓存消息的功能。
  1. <font size="4">nfc_ndef_message_h message = NULL;

  2. error_code = nfc_manager_get_cached_message(&message);
  3. if (NFC_ERROR_NONE != error_code) // Error occurred</font>
复制代码

在得到消息后,得到消息的详细信息就像前面所描述的一样。 要做到这一点,检查是否有任何错误,是否没有消息 NULL 。
  1. <font size="4">if (message != NULL)
  2. {
  3.    on_nfc_ndef_discovered(clone_message(message), NULL);
  4. }</font>
复制代码

5.清理在应用程序结束。



初始化NFC P2P

初始化NFC P2P:

1.确保你有2支持NFC P2P模式的目标设备。 注意,使用NFC设备屏幕应该解锁。

2.使用的函数和数据类型 NFC API,包括 < nfc.h > 头文件在您的应用程序:
  1. <font size="4">#include <nfc.h></font>
复制代码


3.开始使用NFC API,API通过调用初始化 nfc_manager_initialize () 功能:
  1. <font size="4">nfc_manager_initialize();</font>
复制代码


4.初始化后的API经理,确保支持NFC和激活设备。 的 nfc_manager_is_supported () 是否支持NFC功能检查。 的 nfc_manager_is_activated () 功能得到了NFC激活状态。
  1. <font size="4">if (!nfc_manager_is_supported())
  2. {
  3.    // Report error, end the application
  4. }
  5. if (!nfc_manager_is_activated())
  6. {
  7.    // Report error, switch on NFC
  8. }</font>
复制代码

5.在应用程序的生命周期结束时,调用 nfc_manager_deinitialize () 功能。 NFC的它会释放所有资源管理器和断开之间的会话,您的应用程序。
  1. <font size="4">nfc_manager_deinitialize();</font>
复制代码





通过NFC P2P发送和接收消息

使用NFC P2P模式下发送和接收消息(一个简单的NDEF消息包含一张名片(姓名、电话号码和电子邮件地址)设备的所有者是准备和与第二个交换设备):

1.准备NDEF消息。
一个NDEF消息由几种NDEF记录。 记录有效负载类型是由两个值:TNF(类型名称格式)和类型。 有几个NDEF tnf和相关类型的记录,如文本记录,URI记录,和MIME记录。 在本教程中,只使用文本记录。

本教程中的示例消息包含一个名称,电话号码,电子邮件地址设备的所有者。 值可以存储在一个文件或来自应用程序的UI——在本教程中获得值省略了。

创建一个文本记录,使用 nfc_ndef_record_create_text () 功能。 它的参数是一个记录处理,文本存储、语言代码(例如en - us或ko-KR),和编码类型。 下面的示例创建3记录名称、电话号码和电子邮件地址:
  1. <font size="4">nfc_ndef_record_h ndef_name_record = NULL;
  2. nfc_ndef_record_h ndef_phone_record = NULL;
  3. nfc_ndef_record_h ndef_email_record = NULL;

  4. const char *name = "John Doe";
  5. const char *phone = "+82556666888";
  6. const char *email = "john.doe@tizen.org";

  7. nfc_ndef_record_create_text(&ndef_name_record, name, "en-US", NFC_ENCODE_UTF_8);
  8. nfc_ndef_record_create_text(&ndef_phone_record, phone, "en-US", NFC_ENCODE_UTF_8);
  9. nfc_ndef_record_create_text(&ndef_email_record, email, "en-US", NFC_ENCODE_UTF_8);</font>
复制代码


记录被创建的时候,他们应该被加到一个信息。 在此之前,创建并初始化一个NDEF消息使用 nfc_ndef_message_create () 功能。 作为参数,通过创建消息的处理。
  1. <font size="4">nfc_ndef_message_h ndef_message = NULL;
  2. nfc_ndef_message_create(&ndef_message);</font>
复制代码

将创建的记录添加到消息使用 nfc_ndef_message_append_record () 功能。 这个函数附加记录下一个索引。 插入一个记录在指定的索引,使用 nfc_ndef_message_insert_record () 函数来代替。
  1. <font size="4">nfc_ndef_message_append_record(ndef_message, ndef_name_record);
  2. nfc_ndef_message_append_record(ndef_message, ndef_phone_record);
  3. nfc_ndef_message_append_record(ndef_message, ndef_email_record);</font>
复制代码

2.通知应用程序发现了P2P的目标。
使用P2P交换消息,首先注册一个回调对接收通知发现使用P2P目标 nfc_manager_set_p2p_target_discovered_cb () 功能。 P2P的目标是发现时,回调提供了一个句柄,设备和信息是否连接或分离。
  1. <font size="4">nfc_manager_set_p2p_target_discovered_cb(on_target_discovered, NULL);</font>
复制代码


3.通知应用程序接收的数据。
在本教程中,两款手机接收和发送消息给对方,所以当另一个P2P的目标是,注册一个回调对接收通知接收的数据从这个设备。 使用 nfc_p2p_set_data_received_cb () 函数(最好的方法是将这段代码在回调后P2P设备发现)。 指定对等的目标处理——这是以前提供的回调。
  1. <font size="4">nfc_p2p_set_data_received_cb(target, on_p2p_data_received, NULL);</font>
复制代码

4.发送消息到另一个设备。
当另一个P2P设备连接,发送消息给它准备好了。 您可以使用 nfc_p2p_send () 如果你不想检查权限功能。 提供一个目标处理和发送消息处理。 还可以设置回调时调用发送完成。
  1. <font size="4">nfc_p2p_send(target, ndef_message, NULL, NULL);</font>
复制代码


5.接收一条消息来自另一个设备。
调用回调对接收数据时,该装置接收一条消息来自另一个设备。 回调提供了处理接收到的消息和处理消息来源。

记录的数量在收到消息使用 nfc_ndef_message_get_record_count () 功能。 在这个例子中,数量应该是3,因为有3记录名称、电话号码和电子邮件地址。
  1. <font size="4">int count;
  2. nfc_ndef_message_get_record_count(message, &count);</font>
复制代码


获得指定记录的信息,使用 nfc_ndef_message_get_record () 功能。 指定消息处理、记录索引和存储处理获得的记录。 当文本记录,存储文本使用 nfc_ndef_record_get_text () 功能。 在本教程中,有三个文本记录获得。
  1. <font size="4">nfc_ndef_record_h ndef_record;

  2. char *name = NULL;
  3. nfc_ndef_message_get_record(message, 0, &ndef_record);
  4. nfc_ndef_record_get_text(ndef_record, &name);

  5. char *phone = NULL;
  6. nfc_ndef_message_get_record(message, 1, &ndef_record);
  7. nfc_ndef_record_get_text(ndef_record, &phone);

  8. char *email = NULL;
  9. nfc_ndef_message_get_record(message, 2, &ndef_record);
  10. nfc_ndef_record_get_text(ndef_record, &email);</font>
复制代码


然后可以使用获得的值创建,例如,一个新的接触。

注意,根据记录类型,一些获得的值应该被释放,其他的不应该。 例如,你应该免费获得了一个文本的文本记录。 看到文档了解如何获得其他类型的记录的有效负载。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
欢迎来到泰泽网:http://www.tizennet.com/ 泰泽论坛:http://bbs.tizennet.com/ 好没有内涵哦,快到设置中更改这个无聊的签名吧!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|泰泽应用|泰泽论坛|泰泽网|小黑屋|Archiver|手机版|泰泽邮箱|泰泽网 ( 蜀ICP备13024062号-1 )

GMT+8, 2024-3-29 05:20 , Processed in 0.070992 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表