Android: Logcat string for Bluetooth ON - android

My application will switch on Bluetooth. I want to wait till bluetooth is switched on.
I will look for string
MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1
in logcat and then proceed.
I want to know if this method is correct or i should be looking for some other string. What is the best way to know whether i am looking for right string in logcat. Is there any collection/document to learn what all info can be gathered using logcat

You have to be careful with below as logcat may prevent your app from responding. You should either run this piece of code or your own app in a seperate thread to keep it responsive. Below code asks logcat to send logs to your app and you can do investigate the logs as you see fit.
private static final String SEARCH_STRING = "MESSAGE_BLUETOOTH_SERVICE_CONNECTED=1";
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
boolean didFind = false;
while ((line = bufferedReader.readLine()) != null && !didFind) {
log.append(line);
didFind = line.toUpperCase().contains(SEARCH_STRING))
}
}
catch (IOException e) {}
I hope it helps.

Related

Read logs from code programmatically and matching with a string in android app

How can we read logs(verbose,debug etc) programmatically from android class and then search for a string or matching with a provided string in android.
Sometimes we need to handle some system or kernel layer related event. But as we have limited access of those code we can't handle them. We can see the logcat via adb and also see some log comes from kernel/framework layer.
Now the question is, How we can override or handling some event in our app based on those logs?
Here is a solution to this from any android app:
We need to make a class with some code like below:
public class LogsUtil {
private static final String processId = Integer.toString(android.os.Process
.myPid());
public static StringBuilder readLogs() {
StringBuilder logBuilder = new StringBuilder();
try {
String[] command = new String[] { "logcat", "-d", "threadtime" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(processId)) {
logBuilder.append(line);
//Code here
}
}
} catch (IOException e) {
}
return logBuilder;
}
}
Then need to write below code in our activity from where we need to check the logs string:
//read the logs
StringBuilder logs = LogsUtil.readLogs();
if(logs.toString().contains("your_text"))
//your code
else //your code

How to collect LogCat messages in an Android application

I have an application and I'd like to collect the LogCat messages of a specified level and tag.
Can I somehow get the accumulated messages at some point? I don't want to collect the messages one by one, it should be the sum of them like when I use adb to read the actual log. Is this possible?
Try this: Note that in Android 4 you will only see the log messages that were written by your own app unless you have root access.
public static String getLog(Context c) {
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
return log.toString();
} catch (IOException e) {
return null;
}
}
Why not just write them to a file instead? LogCat is really for real-time logs. There are lots of good quality logging packages that can log to a file if that's what you want to do.
Just as an example:
How to write logs in text file when using java.util.logging.Logger

Rooted Android Background onTouchListener

I'm wonder if there is any way (on rooted phone) to use onTouch method from background, do some think and then dispatch this touch to foreground application.
Create a process and throw this at it: getevent
Multiple new lines will come in every time the screen is touched. Must have root since it contains sensitive touch position information.
Something like this:
try {
Process process = Runtime.getRuntime().exec("su getevent"); //su to get root access
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
//A new line came in. So a touch event came in.
}
}
catch (IOException e) {}
Note: I haven't tested it but it should work. Maybe minor tweaks are necessary.

Read logcat from app not working correctly

I am trying to read from the logcat output in my app. I am able to read in correctly, but it goes on reading it in endless loop. Somehow there seems no way to detect the end of stream.
Not sure what I am doing wrong.
Here is my code:
String baseCommand = "logcat -v time MyTag:D *:S";
Process process = null;
try {
process = Runtime.getRuntime().exec(baseCommand);
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
Log.d("SomeOtherTag", line); //This line executes endlessly
}
} catch (IOException e) {
Log.e(DEBUG_TAG, "error in logging");
e.printStackTrace();
}
Logcat doesn't exit so the buffer is blocked.
Use 'logcat -d' in order to dump the log and then exit.
Hope this still helps, Yaron
Not positive but I believe you need to pass the logcat call if it has args in a String[] so it would be something like
String[] baseCommand = {"logcat", "-v", "time", "MyTag:D", "*:S"};
then the rest of your code.
The single string call is just the program name, not the args.

Reading Logcat within the app returns null

I read the other posts and can't figure out the "trick".
I looked at Log Collector but can't use a separate APK. I'm basically using the same approach and I consistently get nothing back on the processes inputstream.
I have READ_LOGS in the manifest.
From within my default activity, I'm able to get the log, but if I move the logic to another activity or utilize an asynctask, no output is returned.
this code is from my default activity... inline, i dump it to the log
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
Log.d(LOGTAG, "Logcat: " +log.toString());
} catch (IOException e) {}
if i wrap it in an asynctask or just inline it in another activity, it returns nothing
ArrayList<String> commandLine = new ArrayList<String>();
//terminate on completion and suppress everything except the filter
commandLine.add("logcat -d -s");
...
//replace asynctask with inline (could not get log in asynctask)
showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
final StringBuilder log = new StringBuilder();
BufferedReader bufferedReader = null;
try{
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(MangoApp.LINE_SEPARATOR);
}
sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
dismissProgressDialog();
dismissMainDialog();
finish();
}
catch (IOException e){
dismissProgressDialog();
showErrorDialog(getString(R.string.failed_to_get_log_message));
Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ignore) {}
}
}
Can anyone spot the diff or explain the magic? I'm pretty sure the commandline is right in the second version so scratching my head. I'm using 2.1 SDK 7 on the emulator.
Thanks
Hope this will be helpful, you don't have to create file by your self just execute the below command, to get the error info.
Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");
Logcat parameters options:
-r <size in kilobytes> -> for specifying the size of file
-f <filename> -> file to which you want to write the logs.
Can you try it without the ArrayList. Just pass the command String
I have implemented it in the following way (without the ArrayList). It works for me.
String baseCommand = "logcat -v time";
baseCommand += " MyApp:I "; // Info for my app
baseCommand += " *:S "; // Silence others
ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);

Categories

Resources