I've a problem when I try to write a NFC A tag on Android 2.3.6 (nexus S). I use the code from this example:
http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/
More precisely, when I do the Ndef.get(mytag) I get null so I cannot write my tag.
Here is the code from which I get a null references (the 'tag' value is not null), only the ndef.
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
return true;
}
Thank you for you help !!!
Your tag may not yet be formatted for NDEF message storage or may not be able to store NDEF messages at all.
Check whether NdefFormatable.get(tag) returns something unequal to null.
Then use NdefFormatable.format(message) to try to write your message.
If NdefFormatable.get(tag) returns null, then either Android has no means to format the tag or the tag is incompatible to NDEF storage.
(Alternatively, you may want to use TagWriter, https://market.android.com/details?id=com.nxp.nfc.tagwriter to format and write your tag.)
Try NfcA.get(tag) instead. Not sure why you are getting the error though, but trying the other class might work.
Related
I'm trying to learn kotlin with a media player project in Android studio.
So I create a media player using create method.
val mediaplayer : MediaPlayer
mediaplayer= MediaPlayer.create(this,R.raw.song)
mediaplayer.start()
But the problem come when I use this:
mediaplayer.setOnPreparedListener(object: MediaPlayer.onPreparedListener{
override fun onPrepared(MediaPlayer mp){
val max = mp.max
Seekbar.max = max
textview.text = convertToSeconde(max) // a function I define
mp.start()
}
})
The problem is that, when I run the app my implementation of onpreparedlistener have never been called. And any error is raise to tell me what the cause.
Can you help me on this please?
No. It is not possible. But, why do you want to do so? It is prepared immediately after that file is created. But, in some cases it might not get prepared due to some issue with the file or uri. So, you can use this alternate after the .create() function:
if(mediaplayer != null){
// it was not created or prepared.
}else{
// good news. it got prepared successfully
}
I am writing my first Android app which includes the NotificationListenerService for the purpose of notification mirroring to other devices. I noticed for messaging applications subsequent messages from the same sender post with the same ID/key and only the latest message text appears in the "android.text" extra field.
My goal is to grab the previous unread messages which look to be attached to the Notification under "android.messages" extra. My issue is that this does not read out to a char sequence or string array, instead it looks like some class which implements Parcellable is stored here.
I'm hoping there is some standard Android class used here that I'm missing which I can use to deserialize to in order to get the previous messages.
Some code:
val pArray: Array<Parcelable>? = bundle.getParcelableArray("android.messages") // Parcelable[6]#17975
println(pArray?.get(0)?.describeContents()) // 1 == CONTENTS_FILE_DESCRIPTOR
val charSequence: Array<CharSequence>? = bundle.getCharSequenceArray("android.messages") // null
val list = bundle.getStringArrayList("android.messages") // null
val arr = bundle.getStringArray("android.messages") // null
If anyone stumbles upon this thread, I figured out how to do it:
val pArray: Array<Parcelable> = bundle.getParcelableArray("android.messages") as Array<Parcelable>
val messages = Notification.MessagingStyle.Message.getMessagesFromBundleArray(pArray)
I'm trying to realize a simple Android application to read from an NFC tag.
I followed the official documentation (https://developer.android.com/guide/topics/connectivity/nfc) to actually realize an application that is almost equal to the one created by "codexpedia" -> Source code.
When a tag is near the smartphone the onNewIntent method is call, but, when I try to get data through the method getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES), I always obtain null.
You can find the code that is causing the problem at the following direct link link
Is there anyone that knows why it happens?
NdefFormatable technology means the card is capable of storing a Ndef message once it has been formatted.
So basically there is no Ndef message on this card, once it has been formatted and a Ndef message put on it then the code will be able to read this card.
You can use Apps like NFC tools or NXP TagWriter App to Format and add an Ndef message
Once formatted it should show the NfcV, Ndef technologies.
The bug in the code https://github.com/codexpedia/android_nfc_read_write/blob/master/app/src/main/java/com/example/peng/nfcreadwrite/MainActivity.java
Line 80 - IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
Should be
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
As it assumes that all cards have a Ndef message to read from, this is not the case for unformatted cards, bank cards, A lot of Transport cards, etc
Also line 91 to 93 - if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Should be
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
In my Android app I use Picasso to load images. This normally works perfectly well.
Today I tried loading a static image from the google maps api, but this doesn't seem to work. When I open the example link as provided on their info page, I get to see the static map image perfectly well. When I load it in my Android app using the line below, I get nothing at all.
Picasso.with(getContext()).load("http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=370x250&maptype=roadmap%20&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318%20&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false").into(mapView);
I also tried to download the image and uploading it to my personal webspace, from which it loads perfectly well, but somehow, it doesn't seem to load directly from the direct google API url.
Does anybody know why this is so, and how I can solve it?
The only programmatic point-of-failure that comes to mind is in parsing the URI. Looking at the current Picasso code (https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Picasso.java) I see the following:
public RequestCreator load(String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
So I'd first debug
Uri.parse("http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=370x250&maptype=roadmap%20&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318%20&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false")
and see what that Object looks like. Does it drop or confuse any of your parameters?
If that doesn't lead you anwhere, try downloading the file manually using a HttpClient [or similar]. Then at least you can fully debug the request/response.
Also, I know Google maps has some limits -- are you sure you haven't reached them?
replace http with https
replace | with %7C
add api key
The .loadMap() function has many declared variables. This is the heart of the whole process.
So what is required for the static maps API to give us an image is that we make an http request with a given url, for which an image response (URL) is received. Let us run through the meaning and utility of these variables. Yes, all of them have a completely different meaning!
The mapUrlInitial variable is always the same while making an API call. It has a query of center ( ?center ) which specifies that we want the location to be centered in the map.
The mapUrlProperties variable contains a string where you control the actual zooming of the image response you will get, the size ofthe image and the color of the marker which will point out our place.
The mapUrlMapType variable is a string where you can actually determine the marker size you want and the type of the map. We are using a roadtype map in the app.
Finally latLong is a string which concatenates the latitude and the longitude of the place we want to pinpoint!
We then concatenate all of these strings to form a feasible Url. The Url is then loaded as we have seen above, in the Picasso code. One thing we can notice is that an event object is always required for all of this to happen, because we are able to fetch the position details using the event object! Final Code:-
fun loadMap(event: Event): String{
//location handling
val mapUrlInitial = “https://maps.googleapis.com/maps/api/staticmap?center=”
val mapUrlProperties = “&zoom=12&size=1200×390&markers=color:red%7C”
val mapUrlMapType = “&markers=size:mid&maptype=roadmap”
val latLong: String = “” +event.latitude + “,” + event.longitude
return mapUrlInitial + latLong + mapUrlProperties + latLong + mapUrlMapType
}
//load image
Picasso.get()
.load(loadMap(event))
.placeholder(R.drawable.ic_map_black_24dp)
.into(rootView.image_map)
I'm sending a username and password to a website for authentication purposes, after all is said and done and I've retrieved the results from the server, I've placed the results in a variable called 'response' To this point everything is working correctly
response = sb.toString();
Toast.makeText(this,"Returned Value: "+ response,0).show();
The value seen in the above Toast is the value being returned by the php script. I've used both a valid user and an invalid user and the Toast displayed above shows the correct value (i.e. "Good Login" or "Login Failed") returned by the server. I want to test for those results so I can start the appropriate activity so I've put in some test "if" statements
if("Good Login".equals(response)){
Toast.makeText(this, "Registered User" + mUsername, 0).show();
}
if("Login Failed".equals(response)){
Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
}
I'm getting nothing from either one.
I've also tried
if(response.equals("Good Login")){
Toast.makeText(this, "Registered User" + mUsername, 0).show();
}
if(response.equals("Login Failed")){
Toast.makeText(this, "Sorry You're Not A Registered Subscriber",0).show();
}
With the same results. Not sure what else to test for. Is there a better way to test for success or failure?
Thanks
Debug (or print) the exact value of the response variable.
It is likely that there are whitespaces, so you may need to have response = response.trim()
I would return an integer error code rather than some string to check the error response.
Make sure you are returning the correct case, otherwise use equalsIgnoreCase
The Java string equals function is fully case/spacing sensitive compare.
So if:response = "Good Login " or if it contains extra-spaces or non-printing characters then it will fail the test.
It would be a good idea to strip all whitespace, even the internal ones. Here's a SO question about doing just that. Also use String.equalsCaseInsesitive() when doing that actual comparison.