NFC p2p tag interception - android

I am building an Android app using Eclipse and Android SDK.
I would like to implement an NFC p2p function in my app in the sense that when you place the 2 phones back to back, both send a string and receive a string automatically. This would of course happen in a separate activity. I have managed to send a custom tag (String) but have been unable to intercept it and use it afterwards in the app code. How can i do this?
This is what i have so far.
public class MainActivity extends Activity {
public NfcAdapter mAdapter;
PendingIntent mPendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = NfcAdapter.getDefaultAdapter(this);
NdefRecord rec = NdefRecord.createUri("www.stackoverflow.com");
NdefMessage ndef = new NdefMessage(rec);
mAdapter.setNdefPushMessage(ndef, this);
}
I have spent a lot of time trying to find and understand solutions to intercept the tag. Unsuccessfully.
Thank you for your help.

Related

how to send intent data between two activities using MVVM

I want to send data from activity to another activity using MVVM but did't know what's the best way to get it in the receiver activity , is it right to use it like that, I see it totaly like mvc
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
ArrayList<Partner> partners = intent.getParcelableArrayListExtra("partners");
}

Logcat is not Printing

I am trying to Print some messages using Log.i but it doesn't print anything, the problem starts after I updated the Android studio.
How can I solve the problem?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("check","**************checking");
setContentView(R.layout.activity_main);
}
}
An alternate solution, but it's not the best.
System.out.print();
Make sure you select the device you are using, you can do it on the top left side of the Logcat.
for example if you used an emulator and than a device make sure the device is selected.
Try using the tag constant
public static final string TAG = "MainActivity"
Then do
Log.d(TAG, YOURMESSAGE)
Also as an alternative you can use System.out

Get parameters from android to unity

I have a problem, i want to send a parameter (int) from an app of build in android studio and receive it in an app build it in unity.
The apps work like this, you open the android app, press a botom and open the second app, i send and intent when i open the unity app but idkhow to recieve the param.
I know that are some questions about this but didn't work or maybe idk how to implement it to my code.
This is how i send the param
Java app
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.bodyar");
if(user!=null){
launchIntent.putExtra(Constant.TYPE,user.getLearningStyleId());
}else{
launchIntent.putExtra(Constant.TYPE,Constant.VISUAL);
}
startActivity(launchIntent);
but i dont know how to receive the value. I tried the stuff of UnityPlayer.UnitySendMessage(), but u just get an error from that
And the funciton of the Script that it´s attached to GameObjectUI is this
public void TipoTest(string token){
prueba.text = token;
}
And when i export the unity project to android, in the method OnCreate I recieve the params like this
public class UnityPlayerActivity extends Activity {
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
#Override protected void onNewIntent(Intent intent) {
// To support deep linking, we need to make sure that the client can get access to
// the last sent intent. The clients access this through a JNI api that allows them
// to get the intent set on launch. To update that after launch we have to manually
// replace the intent with the one caught here.
setIntent(intent);
}
}
And It works but idk how to pass the variable to unity script

Host-Card Emulation Reader Model

From reading the HCE developers guide here HCE Developer's Guide seems it is possible to use an android phone as a reader. I put card information on an NFC tag, and then read it with my phone. I want to have the phone act as the reader. Do you know if this is possible? I have created a sample project with the following lines of code in it:
import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
public class MyHostAPDUService extends HostApduService{
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
...
}
#Override
public void onDeactivated(int reason) {
...
}
}
I don't know where to go next.
I have achieved using the phone as a reader but through a slightly different approach:
Using an activity as the base I created a simple reader application where I sent a select and then some following APDUs to a card
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private String[][] mTechLists;
private IsoDep card;
onCreate(){
...
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = pendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
mTechLists = new String[][]{new String[]{IsoDep.class.getName()},new String[]{IsoDep.class.getName()}};
...
}
onPause(){
mAdapter.disableForegroundDispatch(this);
}
onResume(){
mAdapter.enableForegroundDispatch(this,mPendingIntent,null,mTechLists);
}
onNewIntent(Intent intent){
Tag theTag = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
card = IsoDep.get(theTag);
...
card.connect();
...
byte[] response = card.transceive(apduBytes);
}
I haven't done much more work on this as it was just a proof of concept but I hope it's helpful.

How do I keep an application running after exiting it?

I wrote an application that has 2 activities: one activity is the main activity and the other will be called by the main through an Intent. In the main activity I will connect to an Arduino board through Bluetooth. However, I want the connection to continue when I call the sub activity, but it disconnects when I push the button of the phone to escape the application (wherever in the main or sub activity) and go to the applications screen of the phone. So, please give me some ideas.
The main activity:
public class BackgroundActivity extends Activity {
private static final String DEVICE_ADDRESS = "00:06:66:43:9B:57";
private Button Living_Room;
private Intent L_intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Amarino.connect(this, DEVICE_ADDRESS);// CONNECT TO ARDUINO BOARD
Living_Room = (Button) findViewById(R.id.living);
Living_Room.setBackgroundColor(Color.TRANSPARENT);
Living_Room.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
L_intent = new Intent(view.getContext(), LivingRoom.class);
startActivityForResult(L_intent, 0);
}
});
}
#Override
protected void onStop(){
super.onStop();
//Amarino.disconnect(this, DEVICE_ADDRESS);
}
}
If I understood you correctly, you want to be able to run your "sub activity" while your application is not in the foreground.. This is what Android Service is for! So try changing your "sub activity" to a service.
Check it out here: http://developer.android.com/reference/android/app/Service.html
It's similar to an Activity, but it runs in the background and doesn't have a GUI.

Categories

Resources