Skip to content

Instantly share code, notes, and snippets.

@tito
Created June 24, 2013 17:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tito/9e2308a4c942ddb2342b to your computer and use it in GitHub Desktop.
Save tito/9e2308a4c942ddb2342b to your computer and use it in GitHub Desktop.
NFC P2P mode tests
[app]
# (str) Title of your application
title = NFC P2P Chat
# (str) Package name
package.name = nfcp2pchat
# (str) Package domain (needed for android/ios packaging)
package.domain = com.meltingrocks
# (str) Source code where the main.py live
source.dir = .
# (list) Source files to include (let empty to include all the files)
source.include_exts = py,png,jpg,kv,atlas
# (list) Source files to exclude (let empty to not excluding anything)
#source.exclude_exts = spec
# (str) Application versionning (method 1)
version.regex = __version__ = '(.*)'
version.filename = %(source.dir)s/main.py
# (str) Application versionning (method 2)
# version = 1.2.0
# (list) Application requirements
requirements = kivy
# (str) Presplash of the application
#presplash.filename = %(source.dir)s/data/presplash.png
# (str) Icon of the application
#icon.filename = %(source.dir)s/data/icon.png
# (str) Supported orientation (one of landscape, portrait or all)
orientation = landscape
# (bool) Indicate if the application should be fullscreen or not
fullscreen = 1
#
# Android specific
#
# (list) Permissions
android.permissions = NFC
# (int) Android API to use
#android.api = 14
# (int) Minimum API required (8 = Android 2.2 devices)
#android.minapi = 8
# (int) Android SDK version to use
#android.sdk = 21
# (str) Android NDK version to use
#android.ndk = 8c
# (str) Android NDK directory (if empty, it will be automatically downloaded.)
#android.ndk_path =
# (str) Android SDK directory (if empty, it will be automatically downloaded.)
#android.sdk_path =
# (str) Android entry point, default is ok for Kivy-based app
#android.entrypoint = org.renpy.android.PythonActivity
# (str) python-for-android branch to use, if not master, useful to try
# not yet merged features.
#android.branch = master
# (str) file to include as an intent filters in <activity> tag
android.manifest.intent_filters = nfc_filter.xml
#
# iOS specific
#
# (str) Name of the certificate to use for signing the debug version
#ios.codesign.debug = "iPhone Developer: <lastname> <firstname> (<hexstring>)"
# (str) Name of the certificate to use for signing the release version
#ios.codesign.release = %(ios.codesign.debug)s
[buildozer]
# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 1
__version__ = '1.0'
from jnius import autoclass, cast
from android.runnable import run_on_ui_thread
from android import activity
from kivy.app import App
from kivy.lang import Builder
NfcAdapter = autoclass('android.nfc.NfcAdapter')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
IntentFilter = autoclass('android.content.IntentFilter')
PendingIntent = autoclass('android.app.PendingIntent')
NdefRecord = autoclass('android.nfc.NdefRecord')
NdefMessage = autoclass('android.nfc.NdefMessage')
class NfcApp(App):
def nfc_init(self):
print 'nfc_init()'
print 'configure nfc'
self.j_context = context = PythonActivity.mActivity
self.nfc_adapter = NfcAdapter.getDefaultAdapter(context)
self.nfc_pending_intent = PendingIntent.getActivity(context, 0,
Intent(context, context.getClass()).addFlags(
Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
print 'p2p filter'
self.ndef_detected = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
self.ndef_detected.addDataType('text/plain')
self.ndef_exchange_filters = [self.ndef_detected]
def on_new_intent(self, intent):
print 'on_new_intent()', intent.getAction()
if intent.getAction() != NfcAdapter.ACTION_NDEF_DISCOVERED:
print 'unknow action, avoid.'
return
rawmsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
print 'raw messages', rawmsgs
if not rawmsgs:
return
for message in rawmsgs:
message = cast(NdefMessage, message)
print 'got message', message
payload = message.getRecords()[0].getPayload()
print 'payload: {}'.format(''.join(map(chr, payload)))
def nfc_disable(self):
print 'nfc_disable()'
activity.unbind(on_new_intent=self.on_new_intent)
def nfc_enable(self):
print 'nfc_enable()'
activity.bind(on_new_intent=self.on_new_intent)
@run_on_ui_thread
def nfc_enable_ndef_exchange(self):
print 'create record'
ndef_record = NdefRecord(
NdefRecord.TNF_MIME_MEDIA,
'text/plain', '', 'hello world')
print 'create message'
ndef_message = NdefMessage([ndef_record])
print 'enable ndef push'
self.nfc_adapter.enableForegroundNdefPush(self.j_context, ndef_message)
print 'enable dispatch', self.j_context, self.nfc_pending_intent
self.nfc_adapter.enableForegroundDispatch(self.j_context,
self.nfc_pending_intent, self.ndef_exchange_filters, [])
@run_on_ui_thread
def nfc_disable_ndef_exchange(self):
self.nfc_adapter.disableForegroundNdefPush(self.j_context)
self.nfc_adapter.disableForegroundDispatch(self.j_context)
def on_pause(self):
self.nfc_disable()
return True
def on_resume(self):
self.nfc_enable()
def build(self):
self.nfc_init()
return Builder.load_string('''
BoxLayout:
Button:
text: 'Enable NDEF exchange'
on_press: app.nfc_enable_ndef_exchange()
Button:
text: 'Disable NDEF exchange'
on_press: app.nfc_disable_ndef_exchange()
''')
NfcApp().run()
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
@pedefe
Copy link

pedefe commented Jun 1, 2016

I do excatly (i hope) your sample (mainly the line 'android.permissions = NFC' in buildozer.spec), but on line : self.nfc_adapter.enableForegroundNdefPush(self.j_context, ndef_message), I have an exception : JVM exception occured: NFC permission required: Neither user 10087 nor current process has android.permission.NFC. Do you have an idea (Running on Samsung S4). Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment