在android上运行python脚本

发布于 2024-11-25 18:11:58 字数 2489 浏览 4 评论 0原文

我正在努力运行一个 python 脚本,该脚本应该允许我使用 nfcpy 将文本写入标签。代码是从 https://code.launchpad.net/nfcpy

我已经在 Android 设备上安装了 LS4A,但是当我尝试运行该程序时,shell 告诉我我错过了 import nfc 包。我尝试过谷歌,但没有帮助。那么有人可以告诉我应该如何解决这个问题吗?

#!/usr/bin/python
# -*- coding: latin-1 -*-
# -----------------------------------------------------------------------------
# Copyright 2011 Stephen Tiedemann <[email protected]>
#
# Licensed under the EUPL, Version 1.1 or - as soon they 
# will be approved by the European Commission - subsequent
# versions of the EUPL (the "Licence");
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://www.osor.eu/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
# -----------------------------------------------------------------------------

import os
import sys
import time

sys.path.insert(1, os.path.split(sys.path[0])[0])

import nfc
import nfc.ndef
import nfc.ndef.Text

def main():
    clf = nfc.ContactlessFrontend()

    print "Please touch a tag to send a hello to the world"
    while True:
        tag = clf.poll()
        if tag and tag.ndef:
            break

    text_en = nfc.ndef.Text.TextRecord( ("en", "Hello World") )
    text_de = nfc.ndef.Text.TextRecord( ("de", "Hallo Welt") )
    text_fr = nfc.ndef.Text.TextRecord( ("fr", "Bonjour tout le monde") )

    message = nfc.ndef.Message( [text_en, text_de, text_fr] )

    tag.ndef.message = message.tostring()

    print "Remove this tag"
    while tag.is_present:
        time.sleep(1)

    print "Now touch it again to receive a hello from the world"
    while True:
        tag = clf.poll()
        if tag and tag.ndef:
            break

    message = nfc.ndef.Message( tag.ndef.message )
    for record in message:
        if record.type == "urn:nfc:wkt:T":
            text = nfc.ndef.Text.TextRecord( record )
            print text.language + ": " + text.text

if __name__ == '__main__':
    main()

提前致谢!

I'am struggling running a python script which should allow me to write a text to a tag by using the nfcpy.The code is downloaded from https://code.launchpad.net/nfcpy

I have installed LS4A on my android device, but when I try to run the program the shell tells me that I miss the import nfc package. I tried google, but to no help. So can anybody tell me how I should solve this problem?

#!/usr/bin/python
# -*- coding: latin-1 -*-
# -----------------------------------------------------------------------------
# Copyright 2011 Stephen Tiedemann <[email protected]>
#
# Licensed under the EUPL, Version 1.1 or - as soon they 
# will be approved by the European Commission - subsequent
# versions of the EUPL (the "Licence");
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://www.osor.eu/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
# -----------------------------------------------------------------------------

import os
import sys
import time

sys.path.insert(1, os.path.split(sys.path[0])[0])

import nfc
import nfc.ndef
import nfc.ndef.Text

def main():
    clf = nfc.ContactlessFrontend()

    print "Please touch a tag to send a hello to the world"
    while True:
        tag = clf.poll()
        if tag and tag.ndef:
            break

    text_en = nfc.ndef.Text.TextRecord( ("en", "Hello World") )
    text_de = nfc.ndef.Text.TextRecord( ("de", "Hallo Welt") )
    text_fr = nfc.ndef.Text.TextRecord( ("fr", "Bonjour tout le monde") )

    message = nfc.ndef.Message( [text_en, text_de, text_fr] )

    tag.ndef.message = message.tostring()

    print "Remove this tag"
    while tag.is_present:
        time.sleep(1)

    print "Now touch it again to receive a hello from the world"
    while True:
        tag = clf.poll()
        if tag and tag.ndef:
            break

    message = nfc.ndef.Message( tag.ndef.message )
    for record in message:
        if record.type == "urn:nfc:wkt:T":
            text = nfc.ndef.Text.TextRecord( record )
            print text.language + ": " + text.text

if __name__ == '__main__':
    main()

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-12-02 18:11:58

假设您运行了此处显示的代码,并且没有 import android 等。

无论如何,sl4a api尚不支持nfc。

Assuming that you ran the code as it appears here, you did not import android and so on.

In any case, the sl4a api does not support nfc yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文