Android手机与PN532通信之 Ubuntu 13.04 & libnfc libllcp PN532 环境搭建

转载自飞蚂蚁哥哥的新浪博客:http://blog.sina.com.cn/s/blog_a2ae2da90101o7vn.html

少许无关紧要的内容是个人改动,原文请看上处链接。

环境:
操作系统:Ubuntu 13.04
硬件设备:PN532
 
准备工作:安装需要的依赖
sudo gedit /etc/apt/sources.list
在最后添加一行:
deb http://ubuntu.mirror.cambrium.nl/ubuntu/ lucid main
个人喜好vim,所以:
$ mkdir work
$ sudo apt-get install vim
$ sudo apt-get install libusb-dev libpcsclite-dev
$ sudo apt-get install libusb-0.1-4 libpcsclite1 libccid pcscd
下载和安装libnfc
1) 下载libnfc最新稳定版libnfc-1.7.0-rc7.tar.gz:
https://code.google.com/p/libnfc/downloads/list
2)   安装:
$ tar -xvzf ~/下载/libnfc-1.7.0-rc7.tar.gz -C ~/work/
$ cd  ~/work/libnfc-1.7.0-rc7
$ ./configure --with-drivers=pn532_uart
$ make
$ make install
3.   验证安装是否成功:
请检查/usr/local/etc/目录下是否有nfc文件夹。如果没有,请在/usr/local/etc/目录下新建nfc文件夹,然后在nfc文件夹下新建一个文件,保存为libnfc.conf,文件内容如下:


# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
# allow_autoscan = true
# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
# allow_intrusive_autoscan = true
# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
# log_level = debug
# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "microBuilder.eu"
#device.connstring = "pn532_uart:/dev/ttyAMA0"
device.connstring = "pn532_uart:/dev/ttyUSB0"


保存退出。
将PN532插入PC USB接口,然后进入如下目录
$ cd libnfc-1.7.0-rc7/utils
运行nfc-list示例程序
$ sudo nfc-list
出现以下信息则表示安装成功
NFC device: pn532_uart:/dev/ttyUSB0 opened


在libnfc安装成功的基础上,安装libllcp
1)   下载源码包:
sudo apt-get install git
git clone https://code.google.com/p/libllcp/
2)   安装:
$ cd ~/work/
$ sudo apt-get install libtool
$ sudo apt-get install autoconf
$ cd libllcp/
$ autoreconf -vis
$ ./configure
$ make
$ sudo make install


安装成功后,进入libllcp/examples/npp-server/目录下,修改npp-server.c文件如下:

/*-

 * Copyright (C) 2011, Romain Tartière

 *

 * This program is free software: you can redistribute it and/or modify it

 * under the terms of the GNU Lesser General Public License as published by the

 * Free Software Foundation, either version 3 of the License, or (at your

 * option) any later version.

 *

 * This program is distributed in the hope that it will be useful, but WITHOUT

 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or

 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for

 * more details.

 *

 * You should have received a copy of the GNU Lesser General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>

 */



/*

 * This implementation was written based on information provided by the

 * following documents:

 *

 * Android NDEF Push Protocol (NPP) Specification

 * Version 1 - 2011-02-22

 * http://source.android.com/compatibility/ndef-push-protocol.pdf

 * 修改于2013.08.25
 */

#include "config.h"

#include <err.h>

#include <signal.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <llcp.h>

#include <llc_service.h>

#include <llc_link.h>

#include <mac.h>

#include <llc_connection.h>



struct mac_link *mac_link;

nfc_device *device;



static void stop_mac_link(int sig)

{

	(void) sig;

	if (mac_link && mac_link->device)

		nfc_abort_command(mac_link->device);

}



static void bye(void)

{

	if (device)

		nfc_close(device);

}



static void* com_android_npp_thread(void *arg)

{

	struct llc_connection *connection = (struct llc_connection *) arg;

	uint8_t buffer[1024];

	int len;



	if ((len = llc_connection_recv(connection, buffer, sizeof(buffer), NULL)) < 0)

		return NULL;

	if (len < 10) // NPP's header (5 bytes) and NDEF entry header (5 bytes)

		return NULL;

	printf("npp-server.c length of received data:%d\n--------------------------------------------------\n", len);

	for (int i = 17; i < len; i ++) {

		printf("%c", buffer[i]);

	}

	printf("\n--------------------------------------------------\nSuccess!\n\n");

	// TODO Stop the LLCP when this is reached

	llc_connection_stop(connection);

	//printf("llc_connection_stop\n");

}



int recv();



int main(int argc, char *argv[])

{

	data:recv();

	return 0;

}



int recv()

{

	nfc_context *context;



	nfc_init(&context);

	if (llcp_init() < 0)

		//errx(EXIT_FAILURE, "llcp_init()");

		printf("llcp init failed!\n");

	signal(SIGINT, stop_mac_link);

	atexit(bye); //close the device that before this open.

	if (!(device = nfc_open(context, NULL)))

		//errx(EXIT_FAILURE, "Cannot connect to NFC device");

		printf("connect to NFC device failed!\n");

	struct llc_link *llc_link = llc_link_new();

	if (!llc_link)

		//errx(EXIT_FAILURE, "Cannot allocate LLC link data structures");

		printf("allocate LLC link data structures faled!\n");

	struct llc_service *com_android_npp;

	while (!(com_android_npp = llc_service_new_with_uri(NULL, com_android_npp_thread, "com.android.npp", NULL))) // evaluation of com_android_npp

		//errx(EXIT_FAILURE, "Cannot create com.android.npp service");

		printf("create com.android.npp service failed\n");

	llc_service_set_miu(com_android_npp, 128); // configure com_android_npp

	llc_service_set_rw(com_android_npp, 1);

	//printf("configure com_android_npp ok!\n");

	if (llc_link_service_bind(llc_link, com_android_npp, -1) < 0)

		//errx(EXIT_FAILURE, "Cannot bind service");

		printf("bind service failed!\n");

	mac_link = mac_link_new(device, llc_link);

	if (!mac_link)

		//errx(EXIT_FAILURE, "Cannot create MAC link");

		printf("create MAC link failed!\n");

	printf("Wait...\n\n");

	if ( mac_link_activate_as_target(mac_link) < 0 )

		//errx(EXIT_FAILURE, "Cannot activate MAC link");

		printf("activate MAC link failed!\n");

	void *err;

	mac_link_wait(mac_link, &err);



	return 0;



	//printf("mac_link_free\n");

	mac_link_free(mac_link);

	//printf("llc_link_free\n");

	llc_link_free(llc_link);

	//printf("close device!\n");

	nfc_close(device);

	device = NULL;

	llcp_fini();

	nfc_exit(context);return 0;

	//exit(EXIT_SUCCESS);

}

保存对npp-server.c的修改,然后在终端下进到npp-server目录下,重新编译npp-server.c文件:
$ cd examples/npp-server/
$ make
运行:
$ sudo ./npp-server
运行的时候手机上运行相应程序,然后将打开Android手机中的Beam程序,讲手机背部靠近PN532开发板.可以看到PN532收到了从手机发送过来的数据,如下:

Wait...

npp-server.c length of received data:48
--------------------------------------------------
Beam me up!
Beam Time: 18:39:46

--------------------------------------------------


Success!

(以下内容不知道是什么意思)
进入该目录的.lib目录下,将lt-npp-server复制到当前用户的主目录下,以便浏览器访问jsp页面时,Java Applet能够调用该程序。
$ cd .libs/
$ cp lt-npp-server ~/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值