Libnfc

原文地址::http://nfc-tools.org/index.php?title=Libnfc



Public platform independent Near Field Communication (NFC) library

libnfc is the first libre low level NFC SDK and Programmers API released under the GNU Lesser General Public License
It provides complete transparency and royalty-free use for everyone. 
This list shows the current supported features
All major operating systems are supported, including GNU/Linux, Mac OS X and Windows. Compilation should work out of the box on POSIX-systems. 
This library supports various NFC hardware devices: dongles, flat and OEM devices. 
The library currently supports modulations for ISO/IEC 14443 A and B, FeliCaJewel/Topaz tags and Data Exchange Protocol (P2P) as target and as initiator.

Contents

 [hide

Download

Stable release

Stable releases can be found at libnfc download section.

Development version

Fetch current version using Git:

git clone https://github.com/nfc-tools/libnfc.git

Note: If you want all libnfc hardware drivers, you will need to have libusb (library and headers) plus on *BSD and GNU/Linux systems, libpcsclite (library and headers).

Under MacOSX, GNU/Linux, *BSD and probably a lot of POSIX systems you can compile development version using:

autoreconf -vis
./configure --enable-doc
make
sudo make install

If you want to (re)generate documentation:

make doc

Third party packages

Installation

The following sections describe per platform how to install and use libnfc.


Because some dependencies (e.g. libusb and optionnal PCSC-Lite) are used, the requirements per platform can differ.

POSIX systems

Manual installation

First make sure you have required compilation dependencies, ie. you will need headers from:

  • libusb

Download and extract libnfc archive

$ wget https://bintray.com/artifact/download/nfc-tools/sources/libnfc-x.x.x.tar.bz2
$ tar -xvzf libnfc-x.x.x.tar.gz

Configure and compile

$ cd libnfc-x.x.x
$ ./configure --prefix=/usr --sysconfdir=/etc
$ make

Please note that ./configure have an installation path prefix set as /usr/local and sysconfdir set as $PREFIX/etc as default. Use --prefix and sysconfdiroption, if you want another setup (see ./configure --help).

Make sure you have required run-time dependencies.

  • libusb

Plug in NFC device, and test if libnfc detect it

$ libnfc/utils/nfc-scan-device -v

Place a tag on it and show it

$ libnfc/utils/nfc-list

Install libnfc into your system

$ sudo make install

Note: if you have any troubles, please read README and browse this wiki before asking (dumb) question on forum and/or maling-lists...

Debian / Ubuntu

First make sure you have required compilation dependencies.

 $ sudo apt-get install libusb-dev

Download and extract libnfc archive

git clone https://github.com/nfc-tools/libnfc.git
cd libnfc
git checkout libnfc-1.7.1
git clean -d -f -x
#rm ../libnfc*.deb
git remote|grep -q anonscm||git remote add anonscm git://anonscm.debian.org/collab-maint/libnfc.git
git fetch anonscm
git checkout remotes/anonscm/master debian
git reset
dpkg-buildpackage -uc -us -b

Make sure you have required run-time dependencies.

$ sudo apt-get install libusb-0.1-4

Install libnfc into your system

$ sudo dpkg -i ../libnfc*.deb

Plug in NFC device, place a tag on it and test your installation

$ nfc-list

FreeBSD

Romain Tartière contributed a FreeBSD port of libnfc, thanks to him.

  • To install the port:
 cd /usr/ports/devel/libnfc/ && make install clean
  • To add the package:
 pkg_add -r libnfc


Gentoo

Thanks to Mike Autylibnfc have a ebuild to be easily installed on Gentoo using usual way:

 $ emerge libnfc

Fedora 12+

Thanks to François Koomanlibnfc library is now included in Fedora 12+ distribution.

Using command line interface
 # yum install libnfc libnfc-devel libnfc-examples
  • libnfc-devel contains development headers and libraries, its only necessary if you want to

compile your own applications requiring libnfc

  • libnfc-examples contains the example tools like nfc-list.
Using graphical tools
  • System -> Administration -> Add/Remove Software
  • Search for libnfc and select the appropriate packages for installation.

Mac OS X

For installing dependencies, MacPorts download manager is used (available at: http://distfiles.macports.org/MacPorts/)

Install MacPorts following instructions from http://www.macports.org/install.php

Install pkg-config:

 $ sudo port install pkgconfig

Install glib:

 $ sudo port install glib

Install glib:

 $ sudo port install libusb

Make sure it's the x86_64 version (when on the latest OS X):

 $ lipo -info /usr/local/lib/libusb.dylib

As the libnfc's MacPort is not up-to-date, now follow Manual installation instructions to compile libnfc using usual autotools way.

Warning: We have previously posted a link to darwinports .com, according to MacPorts project: they are an impostor / phishing site designed to get donations from unsuspecting users. Donations made to that site do not benefit MacPorts project and the instructions shown there for installing software are incorrect. To know more about this issue: http://trac.macports.org/wiki/DarwinPorts

Official MacPorts' site: http://www.macports.org/ If you are looking for how to use and install MacPorts, please visit: http://guide.macports.org/

Windows

See Libnfc:Windows

Configuration

Libnfc can be tuned by a config file as well as with environment variables. 
See the configuration page.

Usage

See Examples how to create code using libnfc.

For a list of projects using this library, see Related links.

A few of them have their page on this wiki, check Software category

Troubleshooting

Having trouble? Check the FAQs and consult the Troubleshooting page. 
Make sure you've checked the configuration page too.

ChangeLog

ChangeLog is directly available from libnfc's repository: https://github.com/nfc-tools/libnfc/blob/master/ChangeLog

If you want more detailed change log, you can use the repository log: https://github.com/nfc-tools/libnfc/commits/master

Contribute

There are many ways to contribute to libnfc, including but not limited to:

Sponsoring

You can also sponsor us with hardware donations, to do so please contact romuald@libnfc.org.

If you are manufacturer and you want your device to be supported in libnfc, feel free to send a mail to romuald@libnfc.org.

Thanks to manufacturers which already done it, see sponsor page.


### 关于 libnfc 库的介绍 libnfc 是一个开源的近场通信 (NFC) 协议栈实现,支持多种 NFC 设备。该库允许开发者创建应用程序来读取和写入 NFC 标签、与其他 NFC 设备交换数据以及执行其他基于 NFC 的操作。 #### 安装 libnfc 为了安装 libnfc,在大多数 Linux 发行版上可以通过包管理器完成: 对于 Debian/Ubuntu 用户: ```bash sudo apt-get update && sudo apt-get install libnfc-dev nfc-tools ``` 对于 Fedora 用户: ```bash sudo dnf install libnfc-devel nfc-utils ``` 如果想要从源码编译最新版本,则需先克隆仓库并按照官方说明进行构建过程[^3]。 #### 配置 libnfc 默认情况下,`/etc/nfc/libnfc.conf` 文件包含了所有必要的配置选项。通常只需要修改 `device.name` 和 `allow_autoscan` 参数即可满足基本需求。具体参数可以根据实际使用的硬件设备调整[^4]。 #### 使用示例 下面是一个简单的 Python 脚本例子,展示了如何利用 pyserial 来连接到 PN532 USB 适配器并与 MIFARE Classic 类型标签交互: ```python import nfc from time import sleep def on_connect(tag): print(f"Connected to {tag}") try: idm, pmm = tag.polling(system_code=0xFE00) manufacturer_data = bytes([idm[7], idm[8]]) serial_number = ''.join(['{:02X}'.format(x) for x in idm[:7]]) print(f"Manufacturer data: {manufacturer_data.hex()}") print(f"Serial number: {serial_number}") service_codes = [(0x09 << 6 | i & 0x0f, 0) for i in range(1)] block_numbers = list(range(0, 1)) sc, bc = service_codes, block_numbers response = tag.read_without_encryption(sc, bc) hex_response = ' '.join('{:02X}'.format(c) for c in response) print(f"Data read from card:\n{hex_response}\n") except Exception as e: print(e) clf = nfc.ContactlessFrontend('usb') while True: clf.connect(rdwr={'on-connect': on_connect}) sleep(1) ``` 此脚本会持续扫描附近的卡片直到检测到一张Mifare卡,并尝试从中读取一些基本信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值