I was just wondering if it was possible to force a scan for NFC tags on Android and how to do it. I want to know if I can call something like nfc.scan() and have it return the tag info if there is any tag detected. I am kinda new to this Android thing and so far I have only seen it done with intents.
No, Android does not allow you to trigger a scan for NFC tags/devices in range.
In fact, Android continuously polls1 for tags while the screen is on (and unlocked) and there is no currently detected tag. Thus, you can register your app to be informed when a new tag is detected -> this is done through intents.
1) Most Android devices employ some energy saving mode that prevents the device from continuously polling for all kinds of tags. Instead, these devices detect tags by sending a short impulse and measuring detuning effects from inductive coupling.
Related
I would like to record a video and send it to the server. It has to be H.264 mp4, so the server does not have to convert it, and it can be displayed on the web.
This is how I currently create the Intent:
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
It works, but I don't know, how to set the codec to H.264, or how to detect the default codec. I was able to find solutions for MediaRecorder, but I would like to avoid using that, because that is too complex for my needs, and I don't want to build a video recorder gui just for that. The MediaStore.ACTION_VIDEO_CAPTURE would be perfect, because the user can record/replay the video, cancel or retry the recording, without any extra programming (and possible bugs) from my site, I just get back the video uri in onActivityResult.
Unfortunately there does not seem to be a way of specifying the codec to be used. As you can see from https://developer.android.com/reference/android/provider/MediaStore#ACTION_VIDEO_CAPTURE, the supported EXTRAs are related to output Uri, duration and size limits, and a simple quality value (0 low quality, 1 high quality).
This is intentional; sending an Intent will result in starting an unknown (at compile time) app, that may or may not support all the functionality required. Also, since an external app is involved, the fewer parameter value ranges have to be established the easier it is to implement.
As you already realized, in Android operations can usually be performed two ways: a "simple" one, in this case asking for the default camera app to produce a video, usually by means of an Intent, and a "powerful" one, usually by means of an API, in this case the MediaRecorder API.
I'm afraid that if you are unable to successfully use the easy way you'll have to go the hard way; usually some boilerplate code can be readily found online.
Still though, for your specific case, I'd let the external app do the heavy lifting of capturing a video file, and later check if H.264 is the codec that has been used (it will most likely be, imho). If not you could work around that: depending on your use case, ask the user (if he/she is knowledgeable enough) to try changing settings in the recording app, or you could implement some form of video transcoding inside your app via a third-party library.
If your use case is so specific that you do always know which camera app will be used (i.e. you are the manufacturer of the devices, or your use case only allows for one specific model and custom android version of the hardware), and you happen to know that your camera app does indeed support specifying the video codec via some custom EXTRA value, you can also try passing that in the Intent data. Alas, I'm afraid this will not work with a generic smartphone target (i.e. you do not know the camera app that will happen to handle your intent).
Problem
I'm making an app that can detect if current place is a particular place and play the sound if I'm locating in a particular place. So, I'm going to detect the place using wifi names(ssid).
Thus, I want to get ssid list when the app is running on background service. Also, it should always get ssid list because it plays sound when user reached the place.
Question
So my question is, How to always get ssid list and detect the place on background?
First, you should using a service to run a scan task, because of background limitation, my suggestion is foreground service, refer foreground-services
Second, about wifi scan feature, on Android, we can get a list of Wi-Fi access points that are visible from the device, but have few limitations apply to the frequency of scan, each background app (All background apps combined for android 9 and higher) can scan one time in a 30-minute period , and 2-minute period for foreground app.
please refer : wifi-scan-throttling
I'm learning about android development. Let's say I want to be able to listen to spotify music in the background, while simultaneously listening to a spoken word podcast thru some other podcast app. Ive tried creating a Soundbuilder object and changed the maxStreams to 2 when I hit a togglebutton. However, when I run the app it makes no difference. Either spotify has the focus or the podcast app has focus.
Should I be utilizing the AudioManager class instead? To be able to eventually controll the volume of each stream independently? Also, would the phone have to be root to be able to change the maxStreams to 2?
I think You should check this example: MixingAudioInputStream.java
It's example taken from here
Check these out and try mixing both streams into single stream by Yourself - as trying to code new things is best way to learn.
Q. What are your best practices in managing bluetooth connectivity?
I've read the android bluetooth guide & many bluetooth connectivity tutorials. Not helpful with encapsulation-design nor best practices.
When should I open/close the connection?
Is the "connection" with a single bluetooth device called a "socket" connection?
Can a single connection send data while listening? (...or between listening states).
I've never coded connectivity with external devices before. It took two weeks for me to wrap my head around the code that scans for near-by bluetooth devices and throw them into a ListView. Listeners, Broadcasts, and Adapters!
My project will be printing 1-40 receipts every 15 minutes on a bluetooth receipt printer. At the moment, security is not an issue. On the same connection, it will also be receiving data (sending & receiving simultaneously does not appear to be necessary but would be useful). I'm not yet sure how the devices are configured on this single dongle device but I would guess the devices are connected via USB controller to the dongle.
So far, I have 1 object to manage a single I/O connection. Staticly I open an activity to select a connection (to later save the label, mac, and pin in the database). Based on tutorials, I have "open", "listen", "send", and "close" methods. What confuses me is "how" to use these functions. Can I leave a connection open all day (10hrs) and use it every 3mins? Should I open/close the connection when sending or requesting data? Where would I detect the need to reconnect?
sorry for the short answer, but from my practice with the Bluetooth API, I have found that this video describe the things very good (totally personal opinion...)
Video 1
In addition this is useful when you do NOT have any previous experience
Tutorial
And as last check out this question in stackoverflow it has a bunch of good references and examples!!
Again sorry for the shortage, but I believe that if you check these out at least most of your questions and concerns will become answered!
:)
EDIT
So, let me be a bit more descriptive and share some of my experience.
I have written an App that communicates with BLE device that has 3 functions
double sided event driven button (push the button on phone -> event is fired to the device; push the button on the BLE device -> event is fired to the phone)
send request from phone -> BLE device answers with current battery percentage
continuously reading strength signal (as aprox. distance) between the phone and the BLE device
So far so good, now the things is that the basic approach is:
Search for BLE devices (bluetooth search or "discovery" of nearby bluetooth devices)
Here you will need android permissions!
Choose the device you want to connect to
To differ the devices (maybe there are a lot around you :) ) you can use BLE device's name or UUID or ... best - use the name ;)
After both devices connect to each other you can then start the Gatt communication. The approach with state machine is a little too much overkill for me. But anyway the communication is done through bytes (in my case...)
In one of the videos/resources there was something specific and VERY HELPFUL at least for me! To be honest I don't remember it exactly, but the idea was that before any communication it's RECOMMENDED to read/get all the options from the BLE device or something similar...
Maybe it was something like discoverOptions() or something like that
Great thing will be to know your device "communication codes" or at least I call them that way.
Check this link for example: Link
** Now you can see there are tables with the USEFUL INFO! E.g. if you want to read the battery level you navigate to this page and find that in order to read the battery, the service name is UUID XXXXX and you need to send 0x01 to the BLE device and it will "answer" to your call with some data which is again in bytes.
I really hope that this is somehow helpful!
PLEASE NOTE
This is strictly coming from my experience and there could be some mismatches or wrong terms, but that's how I personally see the things and because my project was long ago, I don't remember most of the things exactly.
IMPORTANT:
This is only a summery of STUCI's provided links above. He has since updated his answer and I have not updated/edited this summery. Topics in my summery are not explanatory but provided for reference and help in generating specific questions.
Original Post...
Thank you Stuci! Some of that was helpful:- some not. I thought it best to collect my thoughts and see what has been explained and if anything hasn't.
(I can't post this much in a comment tho, sorry)
PLEASE CALL ME ON ANYTHING THAT IS INCORRECT.
Video of Bluetooth LE
(Covers a bunch of random things)
While I "dont-like" videos of code:- I watched it because it was recommended ... and I am glad I did. While not very helpful it did introduce some concepts I was unaware of. Since I am targeting old android devices (v8+) the LE features are inconsequential.
Pushing Data: [Depending on the source feature-set], one does not need to continually pull data (ex. with a temperature sensor) but some devices can "push" it to the device on change. Seems to use the 'advertisement" design concept.
UUIDs define Services and/or Characteristics of the connected device.
Possibility to write configuration on (to) connected devices.
Characteristics which seem to be simply "settings" that can be assigned over bluetooth. Not sure if this (~19mins) applies to non-gatt connectoins but seems similar to the state-machine that controls
Advertisements which seem to be the "metadata" regarding the devices current state or config (~24mins). Again, not sure if this even applies to non LE Bluetooth.
Leaving Connections Open
Bluetooth connections can indeed remain open; starting at the point which the "startActivityForResult(...) method is successfully called.
Two basic things affect whether or not one would want to maintain an open connection:
Understand the power consumption.
Having the adapter active simply consumes additional power. If one can keep the adapter shut-off while it is not "absolutely-needed" will mearly save battery power.
Accidental disconnects are managed.
Other than leaving the connection continually connected, one could disconnect & reconnect regularly at specified intervals to ensure a connection is up.
In the thread(s) used for I/O, one could check for a disconnect and reconnect (possibly starting a new thread).
I/O Streams pr Connection
A single connection can indeed "have" simultaneous Input & Output streams. I
Since it was suggested, I re-read Android's Bluetooth Guide and under "managing a connection" (talking about a single socket) I noticed this...
Get the InputStream and OutputStream that handle transmissions through the socket, via getInputStream() and getOutputStream(), respectively.
Read and write data to the streams with read(byte[]) and write(byte[]).
...but continues with noting that read & write block each other. Something I still need to look further into. It seems like you cant I/O simultaneously on the same socket???
Max Connections
I also looked into the max connection issue Stuci added and found no documentation on the Android-side. It might exist, I cant find it. However, most people seem to agree that there is a limitation (that could be as low as 4) imposed by whatever hardware you are coding for.
Some notable links:
- How many devices we can pair via Bluetooth of BLE to Android?
- How many maximum device can we pair via Bluetooth to android device at a time?
- https://groups.google.com/forum/#!topic/android-developers/adeBD275u30
I'm sure most of you have used an android phone before and taken a picture. Whenever the user changes the mobile phone's position and holds it steady, the camera focusses automatically. I'm having a hard time replicating this in my app. The autofocus() method is being called only once when the application is being launched. I have been searching for a solution these past 3 days and while reading the google documentation I stumbled upon the sensor method calls (such as when the user tilts the mobile forwards or backwards). I could use this API to achieve what I need but it sounds too dirty and too complicated. I'm sure there's another way around it.
All examples on the internet which I have found only focus when the user presses the screen or a button. I have also gone through several questions on SO to hopefully find what I am looking for but I was unsuccessful. I have seen this question and that String is not compatible with my phone. For some reason the only focussing modes which I can use is fixed and auto.
I was hoping someone here would shed some light on the subject because I am at a loss.
Thankyou very much for your time.
Since API 14 you can set this parameter
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FOCUS_MODE_CONTINUOUS_PICTURE
Yes, camera.autoFocus(callback) is a one-time function. You will need to call it in a loop to have it autofocus continuously. Preferably you would have a motion detection via accelerometer or compass to detect when camera is moved.