NFC C语言 VCard 生成示例

需要libnfc库,用nfc-emulate-forum-tag4.c测试。

电脑接usb nfc设备。调用main后向usb发送vcard。android手机可以接收到vcard。

typedef struct{
char fn[30];
char tel_work[20];

} VCard_t;

char header[]={ 0xc2, 0x0c, 0x00, 0x00, 0x01, 0x05, 0x74, 0x65, 0x78,
0x74, 0x2f, 0x78, 0x2d, 0x76, 0x43, 0x61, 0x72, 0x64};
char begin[]= {"BEGIN:VCARD\r\nVERSION:3.0"};
//N:Employee;Joe;Google;;
char fn[]={"\r\nFN:"};
char tel_work[]={"\r\nTEL;TYPE=WORK;TYPE=PREF:"};
//EMAIL;TYPE=X-INTERNET:support@google.com
//ADR;TYPE=WORK:;;1600 Amphitheatre Parkway;94043 Mountain View;;;
//ORG:Google
//TITLE:Software Engineer
//URL:http://www.google.com
char end[]= {"\r\nEND:VCARD\r\n"};

void updateNDEFFileLengh(char* p, int ndef_length) {
p[0] = (uint8_t)(ndef_length >> 8);
p[1] = (uint8_t)(ndef_length);
printf("===vCard ndef file length:%d<<8 + %d .====\n", p[0], p[1]);
}
void updatePayloadLengh(char* p, int payload_length) {
p[0] = (uint8_t)(payload_length >> 24);
p[1] = (uint8_t)(payload_length >> 16);
p[2] = (uint8_t)(payload_length >> 8);
p[3] = (uint8_t)(payload_length);

}
int generateVCard(VCard_t *vCard, uint8_t* buffer) {

uint8_t* p = buffer+2;
memcpy(p, header, sizeof(header));
p+=sizeof(header);
strcat(p,begin);
p+=strlen(begin);
strcat(p, fn);
p+=strlen(fn);
strcat(p, vCard->fn);
p+=strlen(vCard->fn);
strcat(p, tel_work);
p+=strlen(tel_work);
strcat(p, vCard->tel_work);
p+=strlen(vCard->tel_work);
strcat(p,end);
p+=strlen(end);
updatePayloadLengh(buffer+2+2, p-buffer);
updateNDEFFileLengh(buffer, p-buffer+sizeof(header));

return 1;

}

 

main中调用:

nfc_context *context;
nfc_init(&context);

pnd = nfc_open(context, NULL);

if (pnd == NULL) {
printf("Unable to open NFC device\n");
//exit(EXIT_FAILURE);
sleep(2);
// continue;
}

signal(SIGINT, stop_emulation);


VCard_t vCard;
strcpy(vCard.fn,"zhibin");
strcpy(vCard.tel_work,"15313050202");
printf("Gen vcard.\n");
memset(ndef_file, 0, sizeof(ndef_file));
if(!generateVCard( &vCard, ndef_file)){
//continue;;
}
int file_len =(ndef_file[0] << 8)
+ ndef_file[1];
print_hex(ndef_file, file_len);

printf("Will send....\n");
if (0 != nfc_emulate_target(pnd, &emulator, 0)) { // contains already nfc_target_init() call
nfc_perror(pnd, "nfc_emulate_target");
}
printf("write done!\n");

nfc_close(pnd);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值