thank you for reading my problem, and hopefully I will get help from you guys.
I have LG watch urbane , and the OS is updated to 6.0.1
unfortunately after the update, I have many problems in the applications on my wear. is there any solution that I can rollback or downgrade the Android os to the previous one?
or could anyone help me with this code please?
I have an application that collects the motion data from Accelerometer and gyroscope wear sensors for five min.
I set the timestamp to 20hz per second (so I should get 20 samples per second from each sensor, thus 6000 samples(records) every 5 min). However, after 5 mins, I have got 30,000 samples( records) from Accelerometer sensor, and 6000 from gyroscope sensor, which is exactly what I want , but the problem with Accelerometer sensor. this problem didn't exist with the old OS of the wear. it appeared after the update. before the update of the Wear OS, I was getting the same number of samples from both sensors every five min (6000 records).
bellow is the code for the App
package edu.fordham.cis.wisdm.actipebble;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import java.util.*;
import java.lang.*;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
import com.google.common.collect.Lists;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Handles the collection of data and the transmission of the data back to the phone in batches.
* #author Andrew H. Johnston ajohnston9#fordham.edu
* #version 1.0STABLE
*/
public class WearTrainingActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mGyroscope;
private TextView mPrompt;
private TextView mProgress;
private GoogleApiClient googleApiClient;
private ArrayList<AccelerationRecord> mAccelerationRecords = new ArrayList<AccelerationRecord>();
private ArrayList<GyroscopeRecord> mGyroscopeRecords = new ArrayList<GyroscopeRecord>();
/**
* Tells the Sensor if it should save the records it
*/
private boolean shouldCollect = false;
/**
* Debugging tag
*/
private static final String TAG = "WearTrainingActivity";
/**
* One of the strategies to keep the watch screen on
*/
private PowerManager.WakeLock wakeLock;
/**
* Constant used to add clarity to formulae below
*/
private static final short MILLIS_IN_A_SECOND = 1000;
/**
* Change the second (i.e. 2nd) multiplicand to change the number of seconds of data collected
*/
private int delay = MILLIS_IN_A_SECOND * 300;
/**
* Expressed in a formula so that value makes more sense
*/
private int maxNumRecords = (delay / MILLIS_IN_A_SECOND) * 20;
/**
* Keeps track of the number of records obtained. This is more accurate than a time-based
* approach.
*/
private int recordCount = 0;
/**
* Sample rate, expressed as number of microseconds between samplings
*/
private static final int SAMPLE_RATE = 50000;
/**
* Maximum number of records we can send to the phone in one transmission.
*/
private static final int MAX_RECORDS_SENT_AT_ONCE = 3500;
/**
* Flag that signals the end of data transmission to the phone
*/
private static final String DATA_COLLECTION_DONE = "/thats-all-folks";
private static boolean first = true;
private static long first_time;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_training);
//Easy way to keep watch from sleeping on me
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mProgress = (TextView) findViewById(R.id.txtProgress);
mPrompt = (TextView) findViewById(R.id.txtPrompt);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
mProgress = (TextView) findViewById(R.id.txtProgress);
mPrompt = (TextView) findViewById(R.id.txtPrompt);
}
});
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
//Collect at 20Hz (Once every 50,000 microseconds)
mSensorManager.registerListener(this, mAccelerometer, SAMPLE_RATE);
mSensorManager.registerListener(this, mGyroscope, SAMPLE_RATE);
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
#Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "Connected to phone.");
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "Connection to phone suspended. Code: " + i);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Fuck! Connection failed: " + connectionResult);
}
})
.addApi(Wearable.API)
.build();
googleApiClient.connect();
shouldCollect = true;
Log.d(TAG, "Started collecting");
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"WearTrainingWakelock");
wakeLock.acquire();
}
#Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SAMPLE_RATE);
mSensorManager.registerListener(this, mGyroscope, SAMPLE_RATE);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (first) {
first_time = System.currentTimeMillis();
first = false;
}
if (shouldCollect) {
long temp1 = System.currentTimeMillis();
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
switch(event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
mAccelerationRecords.add(new AccelerationRecord(x,y,z,timestamp));
recordCount++;
break;
case Sensor.TYPE_GYROSCOPE:
GyroscopeRecord gyro = new GyroscopeRecord(x,y,z,timestamp);
//Clean up debugging output a little
if (recordCount % 10 == 0) {
Log.d(TAG, "Record is: " + gyro.toString());
}
mGyroscopeRecords.add(gyro);
}
//recordCount > maxNumRecords
if (shouldCollect && (System.currentTimeMillis() - first_time >= 5*60*1000)) {
shouldCollect = false;
new Thread(new SendDataToPhoneTask()).start();
}
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//Not used but must be overridden
}
class SendDataToPhoneTask implements Runnable {
#Override
public void run() {
Log.d(TAG, "Ending stream");
try {
List<List<AccelerationRecord>> accelLists =
Lists.partition(mAccelerationRecords, MAX_RECORDS_SENT_AT_ONCE);
List<List<GyroscopeRecord>> gyroLists =
Lists.partition(mGyroscopeRecords, MAX_RECORDS_SENT_AT_ONCE);
/* I know the following two for loops look like they could be
* abstracted into a single generic method, but due to type erasure
* of generics I can't do this with a single polymoprhic method.
*/
for (List<AccelerationRecord> list : accelLists) {
Log.d(TAG, "Sending list of acceleration records...");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
ArrayList<AccelerationRecord> tmp = new ArrayList<AccelerationRecord>(list);
Log.d(TAG, "List is of size: " + tmp.size());
oos.writeObject(tmp);
oos.flush();
oos.close();
byte[] data = baos.toByteArray();
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/accel-data");
dataMapRequest.getDataMap().putByteArray("/accel", data);
PutDataRequest request = dataMapRequest.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(googleApiClient, request);
}
//Having a bug where a "that's all" message actually beats the
//data to the phone, so we'll just attach the message to the last
//list of data going out
for (int i = 0; i < gyroLists.size(); ++i) {
List<GyroscopeRecord> list = gyroLists.get(i);
Log.d(TAG, "Sending list of gyroscope records...");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oosG = new ObjectOutputStream(baos);
ArrayList<GyroscopeRecord> tmp = new ArrayList<GyroscopeRecord>(list);
Log.d(TAG, "List is of size: " + tmp.size());
oosG.writeObject(tmp);
oosG.flush();
oosG.close();
byte[] data = baos.toByteArray();
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/gyro-data");
dataMapRequest.getDataMap().putByteArray("/gyro", data);
if ((i+1) == gyroLists.size()) {
dataMapRequest.getDataMap().putString("/done", DATA_COLLECTION_DONE);
} else {
dataMapRequest.getDataMap().putString("/done", "/not-done");
}
PutDataRequest request = dataMapRequest.asPutDataRequest();
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(googleApiClient, request);
}
//Vibrate and tell the user to check their phone
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(500L); //Vibrate for half a second
runOnUiThread(new Runnable() {
#Override
public void run() {
mPrompt.setText("Please finish the training by opening your phone.");
mProgress.setText("");
}
});
} catch (IOException e) {
Log.d(TAG, "Something fucky happened: " + e.getMessage());
} finally {
//Screen can turn off now.
wakeLock.release();
}
}
}
}
Related
So, in my Android sensor app, I have a service (foreground) which runs continuously in a different process and records separate sensors' data (accelerometer, magnetometer, GPS). I have used a timer of period 100 ms to fetch and write the data in various CSV files for different sensors. The problem is that the data recording is not consistent, i.e. the timer is not working properly or something. It skips some intervals like for few 100 ms or sometimes even few secs while it should record ten samples per second. I have defined custom classes for the sensors, and they are working properly.
Here is my code for the service:
package com.example.ark.ark.Services;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import java.text.SimpleDateFormat;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.NotificationCompat;
import com.example.ark.ark.Constants;
import com.example.ark.ark.R;
import com.example.ark.ark.Sensors.Accelerometer;
import com.example.ark.ark.Sensors.Gps;
import com.example.ark.ark.Sensors.Magnetometer;
import com.example.ark.ark.Sensors.Rotation;
import com.example.ark.ark.activity.MainActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import static com.example.ark.ark.Constants.*;
/**
* Created by ark on 12/8/17.
*/
#RequiresApi(api = Build.VERSION_CODES.N)
public class DataRecording extends Service {
private Magnetometer magnetometer;
private Accelerometer accelerometer;
private Gps gps;
private Rotation rot;
private Timer myTimer, gpsTimer;
//frequency variables
private int acc_mag_freq = 100;
private int gps_freq = 2000;
SimpleDateFormat time = new SimpleDateFormat(TIMESTAMP_FORMAT);
SimpleDateFormat time1 = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
//Declaring file variables
private File sdDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + DIRECTORY);
File accDirectory = new File(sdDirectory + Constants.DIRECTORY_ACC);
File magDirectory = new File(sdDirectory + Constants.DIRECTORY_MAG);
File gpsDirectory = new File(sdDirectory + Constants.DIRECTORY_GPS);
File rotationDirectory = new File(sdDirectory + Constants.DIRECTORY_ROTATION);
private File datafile_Rot;
private File dataFile_Acc;
private File dataFile_Mag;
private File dataFile_Gps;
private FileOutputStream dataOutputStream_Acc, dataOutputStream_Mag, dataOutputStream_Gps, dataOutputStream_Rot;
private StringBuilder data_Acc = new StringBuilder();
private StringBuilder data_Mag = new StringBuilder();
private StringBuilder data_Gps = new StringBuilder();
private StringBuilder data_Rot = new StringBuilder();
private String mode = new String();
private String acc_name, mag_name, rot_name, gps_name, user = "";
#Override
public void onCreate() {
Intent notify = new Intent(this, MainActivity.class);
PendingIntent pendingintent = PendingIntent.getActivity(this, 0, notify, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon_1_round)
.setContentTitle("Mobility")
.setContentText("Recording Data")
.setContentIntent(pendingintent).build();
startForeground(1337, notification);
//Creating objects of the different sensor's class
magnetometer = new Magnetometer(this);
accelerometer = new Accelerometer(this);
gps = new Gps(this);
rot = new Rotation(this);
data_Gps.append("\n");
data_Acc.append("\n");
if (magnetometer.isMagAvailable()) {
data_Rot.append("\n");
data_Mag.append("\n");
}
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent != null && intent.getExtras() != null) {
user = intent.getExtras().getString("username");
String t = intent.getExtras().getString("acc_mag_freq");
String r = intent.getExtras().getString("gps_freq");
mode = intent.getExtras().getString("mode");
acc_mag_freq = Integer.parseInt(t);
gps_freq = Integer.parseInt(r);
}
//Creating variables for file names
acc_name = user + "_" + time1.format(new Date()) + "_" + Constants.DATA_FILE_NAME_ACC;
gps_name = user + "_" + time1.format(new Date()) + "_" + Constants.DATA_FILE_NAME_GPS;
mag_name = user + "_" + time1.format(new Date()) + "_" + Constants.DATA_FILE_NAME_MAG;
rot_name = user + "_" + time1.format(new Date()) + "_" + Constants.DATA_FILE_NAME_ROTATION;
dataFile_Acc = new File(accDirectory, acc_name);
if (magnetometer.isMagAvailable()) {
dataFile_Mag = new File(magDirectory, mag_name);
if (mode.equals("1"))
datafile_Rot = new File(rotationDirectory, rot_name);
}
dataFile_Gps = new File(gpsDirectory, gps_name);
try {
dataOutputStream_Acc = new FileOutputStream(dataFile_Acc, true);
if (magnetometer.isMagAvailable()) {
dataOutputStream_Mag = new FileOutputStream(dataFile_Mag, true);
if (datafile_Rot != null)
dataOutputStream_Rot = new FileOutputStream(datafile_Rot, true);
}
dataOutputStream_Gps = new FileOutputStream(dataFile_Gps, true);
} catch (IOException e) {
e.printStackTrace();
}
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
getDataSensors();
}
}, 0, acc_mag_freq);
gpsTimer = new Timer();
gpsTimer.schedule(new TimerTask() {
#Override
public void run() {
getDataGps();
}
}, 0, gps_freq);
return START_STICKY;
}
public void getDataGps() {
data_Gps.append(time.format(new Date()));
data_Gps.append(",");
float[] gpsreading = gps.getReading();
data_Gps.append(String.format("%.10f", gpsreading[0]));
data_Gps.append(",");
data_Gps.append(String.format("%.10f", gpsreading[1]));
data_Gps.append("\n");
try {
dataOutputStream_Gps.write(data_Gps.toString().getBytes());
data_Gps.setLength(0);
} catch (IOException e) {
e.printStackTrace();
}
}
public void getDataSensors() {
data_Acc.append(time.format(new Date()));
data_Acc.append(",");
if (magnetometer.isMagAvailable()) {
data_Mag.append(time.format(new Date()));
data_Mag.append(",");
if (mode == "1") {
data_Rot.append(time.format(new Date()));
data_Rot.append(",");
}
}
float[] acc = accelerometer.getLastReading();
float[] mag = magnetometer.getLastReading();
float[] rotVal = rot.getLastReading();
//accelerometer data_Acc
data_Acc.append(String.format("%.3f", acc[0]));
data_Acc.append(",");
data_Acc.append(String.format("%.3f", acc[1]));
data_Acc.append(",");
data_Acc.append(String.format("%.3f", acc[2]));
//data_Acc.append(",");
if (magnetometer.isMagAvailable()) {
//magnetometer data_Acc
data_Mag.append(String.format("%.3f", mag[0]));
data_Mag.append(",");
data_Mag.append(String.format("%.3f", mag[1]));
data_Mag.append(",");
data_Mag.append(String.format("%.3f", mag[2]));
//data_Acc.append(",");
// write this data_Acc to file
data_Mag.append("\n");
if (mode == "1") {
data_Rot.append(String.format("%.3f", rotVal[0]));
data_Rot.append(",");
data_Rot.append(String.format("%.3f", rotVal[1]));
data_Rot.append(",");
data_Rot.append(String.format("%.3f", rotVal[2]));
//data_Acc.append(",");
// write this data_Acc to file
data_Rot.append("\n");
}
}
data_Acc.append("\n");
try {
dataOutputStream_Acc.write(data_Acc.toString().getBytes());
if (magnetometer.isMagAvailable()) {
dataOutputStream_Mag.write(data_Mag.toString().getBytes());
if (mode == "1") {
dataOutputStream_Rot.write(data_Rot.toString().getBytes());
}
}
data_Acc.setLength(0);
data_Rot.setLength(0);
data_Mag.setLength(0);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
As far as I know Android is not designed to support hard real time events, so I don't think you can achieve that in any way.
Anyway I think you could try using alarms and defining the service as a foreground service, if you can use these things in your app, from what I know it won't ensure that data retrieval is executed every 100 ms, but it's likely to do it on a more regular basis.
So I have been working on this code for a while now trying to implement Google Visions into my prior app that displays an image from pixabay then tells me the tags of the photo.I had both the google vision app and pixabay app work just fine on their own. In this new version it should give me tags and the labels found by Google Visions but, whenever I activate the UP command on the sensors it crashes.
Here is my code:
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionRequest;
import com.google.api.services.vision.v1.VisionRequestInitializer;
import com.google.api.services.vision.v1.model.AnnotateImageRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import com.google.api.services.vision.v1.model.Feature;
import com.google.api.services.vision.v1.model.Image;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import static edu.ggc.lutz.recipe.pixabaysamplerwalkthrough.R.id.tvLabels;
import static edu.ggc.lutz.recipe.pixabaysamplerwalkthrough.R.id.tvTags;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
public static final String PIXABAY = "Pixabay";
private ImageView imageView;
private static PixabayQueryResult result;
private String tags;
long numberOfHits;
long selected;
float[] gravity = new float[3];
float[] accel = new float[3];
private static final float ALPHA = 0.80f; // weighing factor used by the low pass filter
private static final String TAG = "OMNI";
private static final float VERTICAL_TOL = 0.3f;
private SensorManager manager;
private long lastUpdate;
private MediaPlayer popPlayer;
private MediaPlayer backgroundPlayer;
private TextToSpeech tts;
private TextView[] tvGravity;
private TextView[] tvAcceleration;
private boolean isDown = false;
private boolean isUp = false;
private static final String CLOUD_VISION_API_KEY = "AIzaSyCt35MZjvD_3ynTbYmeUuBFyMbYrjXUmzs";
private static final String ANDROID_CERT_HEADER = "X-Android-Cert";
private static final String ANDROID_PACKAGE_HEADER = "X-Android-Package";
private static final String TAGgoogle = MainActivity.class.getSimpleName();
private TextView pixtags;
private TextView googlelab;
private String urlString;
private Bitmap bitmapT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pixtags= (TextView) findViewById(tvTags);
googlelab= (TextView) findViewById(tvLabels);
/* FloatingActionButton fab = (FloatingActionButton) findViewById(fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PixabayFetchTask task = new PixabayFetchTask();
String service = "https://pixabay.com/api/";
String key = "5535853-23bc4a5e307cd5d1a5e16ebcc";
String query_params = "&editor_choice=true&safesearch=true&image_type=photo";
String urlString = service + "?key=" + key + query_params;
task.execute(urlString);
}
});*/
imageView= (ImageView) findViewById(R.id.imageView);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
int result1=0;
if(status == TextToSpeech.SUCCESS) {
result1 = tts.setLanguage(Locale.US);
}
if( result1 == TextToSpeech.LANG_MISSING_DATA || result1== TextToSpeech.LANG_NOT_SUPPORTED){
Log.e("TTS", "This Language is not supported");
}
else
{
Log.e("TTS", "Inizalization Failed");
}
}
});
//////////////////////////
manager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
backgroundPlayer = MediaPlayer.create(this, R.raw.mistsoftime4tmono);
//////////////////////////
//callCloudVision("https://pixabay.com/get/eb36b90f2df1053ed95c4518b7494395e67fe7d604b0154892f2c67da7eabc_640.jpg");
}
///////////////////////////////
#Override
protected void onResume() {
super.onResume();
manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
backgroundPlayer.start();
}
//////////////////////////////
#Override
protected void onPause() {
super.onPause();
manager.unregisterListener(this);
backgroundPlayer.pause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Intent intent = new Intent(this, About.class);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public static long getRandomLong(long minimum, long maximum)
{
return (long) (Math.random()* (maximum- minimum))+ minimum;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
gravity[0] = lowPass(event.values[0], gravity[0]);
gravity[1] = lowPass(event.values[1], gravity[1]);
gravity[2] = lowPass(event.values[2], gravity[2]);
accel[0] = highPass(event.values[0], accel[0]);
accel[1] = highPass(event.values[1], accel[1]);
accel[2] = highPass(event.values[2], accel[2]);
long actualTime = System.currentTimeMillis();
if (actualTime - lastUpdate > 100) {
if (inRange(gravity[2], -9.81f, VERTICAL_TOL)) {
Log.i(TAG, "Down");
if (!isDown) {
Vibrator v = (Vibrator) this.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
PixabayFetchTask task = new PixabayFetchTask();
String service = "https://pixabay.com/api/";
String key = "5535853-23bc4a5e307cd5d1a5e16ebcc";
String query_params = "&editor_choice=true&safesearch=true&image_type=photo";
urlString = service + "?key=" + key + query_params;
task.execute(urlString);
backgroundPlayer.setVolume(0.1f, 0.1f);
tts.speak("The device is pointing down", TextToSpeech.QUEUE_FLUSH, null);
backgroundPlayer.setVolume(1.0f, 1.0f);
isDown = true;
isUp = false;
}
} else if (inRange(gravity[2], 9.81f, VERTICAL_TOL)) {
if (!isUp) {
try {
callCloudVision(urlString);
} catch (IOException e) {
e.printStackTrace();
}
backgroundPlayer.setVolume(0.1f, 0.1f);
Log.i(TAG, "Up");
tags= (String) result.getTags((int)selected);
pixtags.setText("Tags: "+tags, null);
/* Snackbar.make(imageView, tags, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
tts.speak(tags.toString(), TextToSpeech.QUEUE_ADD, null);
//tts.speak("up", TextToSpeech.QUEUE_FLUSH, null);
backgroundPlayer.setVolume(1.0f, 1.0f);
isUp = true;
isDown = false;
}
} else {
Log.i(TAG, "In between");
//isDown = false; // Rubbish!
//isUp = false;
}
lastUpdate = actualTime;
}
}
private boolean inRange(float value, float target, float tol) {
return value >= target-tol && value <= target+tol;
}
// de-emphasize transient forces
private float lowPass(float current, float gravity) {
return current * (1-ALPHA) + gravity * ALPHA; // ALPHA indicates the influence of past observations
}
// de-emphasize constant forces
private float highPass(float current, float gravity) {
return current - gravity;
}
class PixabayFetchTask extends AsyncTask<String, Void, PixabayQueryResult> {
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {#link #execute}
* by the caller of this task.
* <p>
* This method can call {#link #publishProgress} to publish updates
* on the UI thread.
*
* #param params The parameters of the task.
* #return A result, defined by the subclass of this task.
* #see #onPreExecute()
* #see #onPostExecute
* #see #publishProgress
*/
#Override
protected PixabayQueryResult doInBackground(String... params) {
Log.v(PIXABAY,"String[0] =" + params[0]);
if(result==null || result.isExpired()) {
try {
String line;
URL u = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder json = new StringBuilder();
while ((line = reader.readLine()) != null) json.append(line);
result = new PixabayQueryResult(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* <p>Runs on the UI thread after {#link #doInBackground}. The
* specified result is the value returned by {#link #doInBackground}.</p>
* <p>
* <p>This method won't be invoked if the task was cancelled.</p>
*
* #param bitmap The result of the operation computed by {#link #doInBackground}.
* #see #onPreExecute
* #see #doInBackground
* #see #onCancelled(Object)
*/
#Override
protected void onPostExecute(PixabayQueryResult result) {
super.onPostExecute(result);
numberOfHits= result.size();
selected = getRandomLong(0, numberOfHits);
Bitmap bitmap= result.getBitmap((int)selected);
imageView.setImageBitmap(bitmap);
/* try {
callCloudVision(urlString);
} catch (IOException e) {
e.printStackTrace();
}*/
}
}
private void callCloudVision(final String loc) throws IOException {
// Switch text to loading
googlelab.setText(R.string.loading_message);
// Do the real work in an async task, because we need to use the network anyway
new AsyncTask<Object, Bitmap, String>() {
#Override
protected String doInBackground(Object... params) {
try {
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
VisionRequestInitializer requestInitializer =
new VisionRequestInitializer(CLOUD_VISION_API_KEY) {
/**
* We override this so we can inject important identifying fields into the HTTP
* headers. This enables use of a restricted cloud platform API key.
*/
#Override
protected void initializeVisionRequest(VisionRequest<?> visionRequest)
throws IOException {
super.initializeVisionRequest(visionRequest);
String packageName = getPackageName();
visionRequest.getRequestHeaders().set(ANDROID_PACKAGE_HEADER, packageName);
String sig = PackageManagerUtils.getSignature(getPackageManager(), packageName);
visionRequest.getRequestHeaders().set(ANDROID_CERT_HEADER, sig);
}
};
Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
builder.setVisionRequestInitializer(requestInitializer);
Vision vision = builder.build();
BatchAnnotateImagesRequest batchAnnotateImagesRequest =
new BatchAnnotateImagesRequest();
batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();
Bitmap bitmap = null;
try {
InputStream stream = new URL(loc).openStream();
bitmap = BitmapFactory.decodeStream(stream);
publishProgress(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
// Add the image
Image base64EncodedImage = new Image();
// Convert the bitmap to a JPEG
// Just in case it's a format that Android understands but Cloud Vision
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
// Base64 encode the JPEG
base64EncodedImage.encodeContent(imageBytes);
annotateImageRequest.setImage(base64EncodedImage);
// add the features we want
annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
Feature labelDetection = new Feature();
labelDetection.setType("LABEL_DETECTION");
labelDetection.setMaxResults(10);
add(labelDetection);
}});
// Add the list of one thing to the request
add(annotateImageRequest);
}});
Vision.Images.Annotate annotateRequest =
vision.images().annotate(batchAnnotateImagesRequest);
// Due to a bug: requests to Vision API containing large images fail when GZipped.
annotateRequest.setDisableGZipContent(true);
Log.d(TAGgoogle, "created Cloud Vision request object, sending request");
BatchAnnotateImagesResponse response = annotateRequest.execute();
return convertResponseToString(response);
} catch (GoogleJsonResponseException e) {
Log.d(TAGgoogle, "failed to make API request because " + e.getContent());
} catch (IOException e) {
Log.d(TAGgoogle, "failed to make API request because of other IOException " +
e.getMessage());
}
return "Cloud Vision API request failed. Check logs for details.";
}
/**
* Runs on the UI thread after {#link #publishProgress} is invoked.
* The specified values are the values passed to {#link #publishProgress}.
*
* #param bitmaps The values indicating progress.
* #see #publishProgress
* #see #doInBackground
*/
#Override
protected void onProgressUpdate(Bitmap... bitmaps) {
super.onProgressUpdate(bitmaps);
imageView.setImageBitmap(bitmaps[0]);
}
protected void onPostExecute(String result) {
googlelab.setText(result);
}
}.execute();
}
private String convertResponseToString(BatchAnnotateImagesResponse response) {
String message = "Labels:\n\n";
List<EntityAnnotation> labels = response.getResponses().get(0).getLabelAnnotations();
if (labels != null) {
for (EntityAnnotation label : labels) {
message += String.format(Locale.US, "%.3f: %s", label.getScore(), label.getDescription());
message += "\n";
}
} else {
message += "nothing";
}
return message;
}
}
Here is the it gives me error:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: edu.ggc.lutz.recipe.pixabaysamplerwalkthrough, PID: 21223 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at edu.ggc.lutz.recipe.pixabaysamplerwalkthrough.MainActivity$2$2.<init>(MainActivity.java:419)
at edu.ggc.lutz.recipe.pixabaysamplerwalkthrough.MainActivity$2.doInBackground(MainActivity.java:400)
at edu.ggc.lutz.recipe.pixabaysamplerwalkthrough.MainActivity$2.doInBackground(MainActivity.java:366)
at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
There is another error that says something about the text to speech but I think that is the result of this error.
I believe it has something to do with running two different Async tasks at the same time overloading it or that fact a null value it getting passed in causing the error.
on line 370 i need a way to look for a string of 'f's' in the advertisement data from a TI CC2650. I found this template online but I'm looking for specific advertising data. Please let me know what string array I need to look at to find this.
package net.jmodwyer.beacon.beaconPoC;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import net.jmodwyer.ibeacon.ibeaconPoC.R;
import org.altbeacon.beacon.Beacon;
import org.altbeacon.beacon.BeaconConsumer;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.RangeNotifier;
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.utils.UrlBeaconUrlCompressor;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
/**
* Adapted from original code written by D Young of Radius Networks.
* #author dyoung, jodwyer
*
*/
public class ScanActivity extends Activity implements BeaconConsumer,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
// Constant Declaration
private static final String PREFERENCE_SCANINTERVAL = "scanInterval";
private static final String PREFERENCE_TIMESTAMP = "timestamp";
private static final String PREFERENCE_POWER = "power";
private static final String PREFERENCE_PROXIMITY = "proximity";
private static final String PREFERENCE_RSSI = "rssi";
private static final String PREFERENCE_MAJORMINOR = "majorMinor";
private static final String PREFERENCE_UUID = "uuid";
private static final String PREFERENCE_INDEX = "index";
private static final String PREFERENCE_LOCATION = "location";
private static final String PREFERENCE_REALTIME = "realTimeLog";
private static final String MODE_SCANNING = "Stop Scanning";
private static final String MODE_STOPPED = "Start Scanning";
protected static final String TAG = "ScanActivity";
/*
* Define a request code to send to Google Play services
* This code is returned in Activity.onActivityResult
*/
private final static int
CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private FileHelper fileHelper;
private BeaconManager beaconManager;
private Region region;
private int eventNum = 1;
// This StringBuffer will hold the scan data for any given scan.
private StringBuffer logString;
// Preferences - will actually have a boolean value when loaded.
private Boolean index;
private Boolean location;
private Boolean uuid;
private Boolean majorMinor;
private Boolean rssi;
private Boolean proximity;
private Boolean power;
private Boolean timestamp;
private String scanInterval;
// Added following a feature request from D.Schmid.
private Boolean realTimeLog;
// LocationClient for Google Play Location Services
LocationClient locationClient;
private ScrollView scroller;
private EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
verifyBluetooth();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
BeaconScannerApp app = (BeaconScannerApp)this.getApplication();
beaconManager = app.getBeaconManager();
//beaconManager.setForegroundScanPeriod(10);
region = app.getRegion();
beaconManager.bind(this);
locationClient = new LocationClient(this, this, this);
fileHelper = app.getFileHelper();
scroller = (ScrollView)ScanActivity.this.findViewById(R.id.scanScrollView);
editText = (EditText)ScanActivity.this.findViewById(R.id.scanText);
// Initialise scan button.
getScanButton().setText(MODE_STOPPED);
}
#Override
public void onResume() {
super.onResume();
beaconManager.bind(this);
}
#Override
public void onPause() {
super.onPause();
// Uncommenting the following leak prevents a ServiceConnection leak when using the back
// arrow in the Action Bar to come out of the file list screen. Unfortunately it also kills
// background scanning, and as I have no workaround right now I'm settling for the lesser of
// two evils.
// beaconManager.unbind(this);
}
public String getCurrentLocation() {
/** Default "error" value is set for location, will be overwritten with the correct lat and
* long values if we're ble to connect to location services and get a reading.
*/
String location = "Unavailable";
if (locationClient.isConnected()) {
Location currentLocation = locationClient.getLastLocation();
if (currentLocation != null) {
location = Double.toString(currentLocation.getLatitude()) + "," +
Double.toString(currentLocation.getLongitude());
}
}
return location;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onBeaconServiceConnect() {}
/**
*
* #param view
*/
public void onScanButtonClicked(View view) {
toggleScanState();
}
// Handle the user selecting "Settings" from the action bar.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Settings:
// Show settings
Intent api = new Intent(this, AppPreferenceActivity.class);
startActivityForResult(api, 0);
return true;
case R.id.action_listfiles:
// Launch list files activity
Intent fhi = new Intent(this, FileHandlerActivity.class);
startActivity(fhi);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Start and stop scanning, and toggle button label appropriately.
*/
private void toggleScanState() {
Button scanButton = getScanButton();
String currentState = scanButton.getText().toString();
if (currentState.equals(MODE_SCANNING)) {
stopScanning(scanButton);
} else {
startScanning(scanButton);
}
}
/**
* start looking for beacons.
*/
private void startScanning(Button scanButton) {
// Set UI elements to the correct state.
scanButton.setText(MODE_SCANNING);
((EditText)findViewById(R.id.scanText)).setText("");
// Reset event counter
eventNum = 1;
// Get current values for logging preferences
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
HashMap <String, Object> prefs = new HashMap<String, Object>();
prefs.putAll(sharedPrefs.getAll());
index = (Boolean)prefs.get(PREFERENCE_INDEX);
location = (Boolean)prefs.get(PREFERENCE_LOCATION);
uuid = (Boolean)prefs.get(PREFERENCE_UUID);
majorMinor = (Boolean)prefs.get(PREFERENCE_MAJORMINOR);
rssi = (Boolean)prefs.get(PREFERENCE_RSSI);
proximity = (Boolean)prefs.get(PREFERENCE_PROXIMITY);
power = (Boolean)prefs.get(PREFERENCE_POWER);
timestamp = (Boolean)prefs.get(PREFERENCE_TIMESTAMP);
scanInterval = (String)prefs.get(PREFERENCE_SCANINTERVAL);
realTimeLog = (Boolean)prefs.get(PREFERENCE_REALTIME);
// Get current background scan interval (if specified)
if (prefs.get(PREFERENCE_SCANINTERVAL) != null) {
beaconManager.setBackgroundBetweenScanPeriod(Long.parseLong(scanInterval));
}
logToDisplay("Scanning...");
// Initialise scan log
logString = new StringBuffer();
//Start scanning again.
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Iterator <Beacon> beaconIterator = beacons.iterator();
while (beaconIterator.hasNext()) {
Beacon beacon = beaconIterator.next();
// Debug - logging a beacon - checking background logging is working.
System.out.println("Logging another beacon.");
logBeaconData(beacon);
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
// TODO - OK, what now then?
}
}
/**
* Stop looking for beacons.
*/
private void stopScanning(Button scanButton) {
try {
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
// TODO - OK, what now then?
}
String scanData = logString.toString();
if (scanData.length() > 0) {
// Write file
fileHelper.createFile(scanData);
// Display file created message.
Toast.makeText(getBaseContext(),
"File saved to:" + getFilesDir().getAbsolutePath(),
Toast.LENGTH_SHORT).show();
scanButton.setText(MODE_STOPPED);
} else {
// We didn't get any data, so there's no point writing an empty file.
Toast.makeText(getBaseContext(),
"No data captured during scan, output file will not be created.",
Toast.LENGTH_SHORT).show();
scanButton.setText(MODE_STOPPED);
}
}
/**
*
* #return reference to the start/stop scanning button
*/
private Button getScanButton() {
return (Button)findViewById(R.id.scanButton);
}
/**
*
* #param beacon The detected beacon
*/
private void logBeaconData(Beacon beacon) {
StringBuilder scanString = new StringBuilder();
if (index) {
scanString.append(eventNum++);
}
if (beacon.getServiceUuid() == 0xfeaa) {
if (beacon.getBeaconTypeCode() == 0x00) {
scanString.append(" Eddystone-UID -> ");
scanString.append(" Namespace : ").append(beacon.getId1());
scanString.append(" Identifier : ").append(beacon.getId2());
logEddystoneTelemetry(scanString, beacon);
} else if (beacon.getBeaconTypeCode() == 0x10) {
String url = UrlBeaconUrlCompressor.uncompress(beacon.getId1().toByteArray());
scanString.append(" Eddystone-URL -> " + url);
} else if (beacon.getBeaconTypeCode() == 0x20) {
scanString.append(" Eddystone-TLM -> ");
logEddystoneTelemetry(scanString, beacon);
}
} else {
// Just an old fashioned iBeacon or AltBeacon...
logGenericBeacon(scanString, beacon);
}
logToDisplay(scanString.toString());
scanString.append("\n");
// Code added following a feature request by D.Schmid - writes a single entry to a file
// every time a beacon is detected, the file will only ever have one entry as it will be
// recreated on each call to this method.
// Get current background scan interval (if specified)
if (realTimeLog) {
// We're in realtime logging mode, create a new log file containing only this entry.
fileHelper.createFile(scanString.toString(), "realtimelog.txt");
}
logString.append(scanString.toString());
}
/**
* Logs iBeacon & AltBeacon data.
*/
private void logGenericBeacon(StringBuilder scanString, Beacon beacon) {
// Comment stuff out for whatever reason
/*
if (location) {
scanString.append(" Location: ").append(getCurrentLocation()).append(" ");
}
`
*/
if (uuid) {
scanString.append(" UUID: ").append(beacon.getId1());
if (beacon.getId1().equals("ffffffff-ffff-ffff-ffff-ffffffffffff ")){
scanString.append("WE DID IT!!!!!!!!!!!");
}else{
scanString.append(" WE DID NOT DO IT =( ");
}
/*
if ((beacon.getId1()).equals ("f")){
scanString.append("WE DID IT!!!!!!!!!!!");
}else{
scanString.append(" WE DID NOT DO IT!!!!!!!!!!! ");
}
*/
}
// Making if statements to test for advertising data
/*
if (majorMinor) {
scanString.append(" Maj. Mnr.: ");
if (beacon.getId2() != null) {
scanString.append(beacon.getId2());
}
scanString.append("-");
if (beacon.getId3() != null) {
scanString.append(beacon.getId3());
}
}
if (rssi) {
scanString.append(" RSSI: ").append(beacon.getRssi());
}
if (proximity) {
scanString.append(" Proximity: ").append(BeaconHelper.getProximityString(beacon.getDistance()));
}
if (power) {
scanString.append(" Power: ").append(beacon.getTxPower());
}
if (timestamp) {
scanString.append(" Timestamp: ").append(BeaconHelper.getCurrentTimeStamp());
} */
}
private void logEddystoneTelemetry(StringBuilder scanString, Beacon beacon) {
// Do we have telemetry data?
if (beacon.getExtraDataFields().size() > 0) {
long telemetryVersion = beacon.getExtraDataFields().get(0);
long batteryMilliVolts = beacon.getExtraDataFields().get(1);
long pduCount = beacon.getExtraDataFields().get(3);
long uptime = beacon.getExtraDataFields().get(4);
scanString.append(" Telemetry version : " + telemetryVersion);
scanString.append(" Uptime (sec) : " + uptime);
scanString.append(" Battery level (mv) " + batteryMilliVolts);
scanString.append(" Tx count: " + pduCount);
}
}
/**
*
* #param line
*/
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
editText.append(line + "\n");
// Temp code - don't really want to do this for every line logged, will look for a
// workaround.
Linkify.addLinks(editText, Linkify.WEB_URLS);
scroller.fullScroll(View.FOCUS_DOWN);
}
});
}
private void verifyBluetooth() {
try {
if (!BeaconManager.getInstanceForApplication(this).checkAvailability()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bluetooth not enabled");
builder.setMessage("Please enable bluetooth in settings and restart this application.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
finish();
System.exit(0);
}
});
builder.show();
}
}
catch (RuntimeException e) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bluetooth LE not available");
builder.setMessage("Sorry, this device does not support Bluetooth LE.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
finish();
System.exit(0);
}
});
builder.show();
}
}
/* Location services code follows */
#Override
protected void onStart() {
super.onStart();
// Connect the client.
locationClient.connect();
}
#Override
protected void onStop() {
// Disconnect the client.
locationClient.disconnect();
super.onStop();
}
#Override
public void onConnected(Bundle dataBundle) {
// Uncomment the following line to display the connection status.
// Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
#Override
public void onDisconnected() {
// Display the connection status
Toast.makeText(this, "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
Toast.makeText(getBaseContext(),
"Location services not available, cannot track device location.",
Toast.LENGTH_SHORT).show();
}
}
// Define a DialogFragment that displays the error dialog
public static class ErrorDialogFragment extends DialogFragment {
// Global field to contain the error dialog
private Dialog mDialog;
// Default constructor. Sets the dialog field to null
public ErrorDialogFragment() {
super();
mDialog = null;
}
// Set the dialog to display
public void setDialog(Dialog dialog) {
mDialog = dialog;
}
// Return a Dialog to the DialogFragment.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return mDialog;
}
}
/*
* Handle results returned to the FragmentActivity
* by Google Play services
*/
#Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST :
/*
* If the result code is Activity.RESULT_OK, try
* to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK :
/*
* TODO - Try the request again
*/
break;
}
}
}
}
Need to cast to string first .toString
if (uuid) {
scanString.append(" UUID: ").append(beacon.getId1());
// Making if statements to look for all f's in advertising data
if (beacon.getId1().toString().equals(Str1)){
scanString.append("\nAlarm ACTIVATED\n");
}else{
scanString.append("\n Alarm NOT active\n");
}
}
I need to create an app to scan and gather the list of access points available, including the signal strength. For some reason every time I initiate the WifiManager.startScan() method, it returns true, but my Broadcast Receiver is never called.
Here is the code for my class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Manages radio (wifi) information
*/
public class NetworkManager {
private final Handler handler = new Handler();
private WifiReceiver receiverWifi;
private TextView mTextView;
// true if scan is done
private boolean mDone = true;
private WifiManager mManager;
List<ScanResult> results;
// Data in form SSID, RSSI
ArrayList<HashMap<String, String>> oldData = new ArrayList<>();
ArrayList<HashMap<String, String>> data = new ArrayList<>();
public NetworkManager(Context c, TextView mTextView) {
this.mTextView = mTextView;
mManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
if (!mManager.isWifiEnabled())
mManager.setWifiEnabled(true);
receiverWifi = new WifiReceiver();
c.registerReceiver(receiverWifi,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if (mManager.isWifiEnabled() == false)
mManager.setWifiEnabled(true);
scanAndSet();
Log.d("TAG", "Constructor after scan and set");
WifiInfo wifiInfo = mManager.getConnectionInfo();
Log.d("TAG", wifiInfo.toString());
}
/**
* Sets the SSID and RSSI from the scanned results into main data bin.
*/
// private void setNetworkData() {
// String result = "";
// oldData = data;
// data.clear();
// for(ScanResult r : results) {
// String ssid = r.SSID;
// String rssi = String.valueOf(r.level);
// HashMap<String, String> x = new HashMap<>();
// x.put(ssid, rssi);
// result += "ID: " + ssid + " - RSSI" + rssi + "/n";
// Log.d("TAG", "R: " + result);
// }
// mDone = true;
// }
public void scanAndSet() {
handler.post(new Runnable() {
#Override
public void run() {
Log.d("TAG", "Start: " + mManager.startScan());
}
});
}
public boolean isDone() {
return mDone;
}
class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG", "On receive broadcast receiver");
ArrayList<String> connections=new ArrayList<String>();
ArrayList<Float> Signal_Strenth= new ArrayList<Float>();
List<ScanResult> wifiList = mManager.getScanResults();
for(int i = 0; i < wifiList.size(); i++) {
connections.add(wifiList.get(i).SSID);
}
}
}
}
the scanAndSet() method is suppose to initiate the scan. The startScan() method returns true. The receiver is only called when I go to the settings and the device searches for wifi networks, I can't seem to do it programatically.
Another peculiar thing is when I want to get information for the connected access point using WifiManager.getConnectionInfo().toString(), this is what it gives.
D/TAG: SSID: <unknown ssid>, BSSID: <none>, Supplicant state: COMPLETED, RSSI: -127, Link speed: -1Mbps, Frequency: -1MHz, Net ID: -1, Metered hint: false, score: 0
I'm completely lost, is this common with all Wear devices or is the wifi api disabled?
I have the code to display the RSSI value of a connected network. A background process continually calls getRssi and the loop time is a few milliseconds.
In the loop I post values by a handler to the UI thread where I display the current time, along with the loop rate.
However, the UI display updates every few seconds whereas the loop rate is a few milliseconds. Why the discrepancy?
package uk.co.moonsit.apps.wifi;
import uk.co.moonsit.apps.sensors.R;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.View;
import android.widget.TextView;
public class WifiRssiActivity extends Activity {
private WifiManager wifi;
private Handler handler = new Handler();
// private String toastText;
private TextView tvStrength;
private TextView tvSpeed;
private TextView tvSSID;
private TextView tvTime;
private View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_rssi);
tvStrength = (TextView) findViewById(R.id.textViewWifiRssi);
tvSpeed = (TextView) findViewById(R.id.textViewSpeed);
tvSSID = (TextView) findViewById(R.id.textViewSSID);
tvTime = (TextView) findViewById(R.id.textViewTime);
String service = Context.WIFI_SERVICE;
wifi = (WifiManager) getSystemService(service);
Thread thread = new Thread(null, doBackgroundThreadProcessing,
"Background");
thread.start();
view = this.getWindow().getDecorView();
view.setKeepScreenOn(true);
}
private Runnable doBackgroundThreadProcessing = new Runnable() {
public void run() {
backgroundThreadProcessing();
}
};
private boolean isRunning = true;
// Method which does some processing in the background.
private void backgroundThreadProcessing() {
long mark = System.currentTimeMillis();
while (isRunning) {
int strength = 0;
int speed = 0;
String units = null;
String ssid = null;
long ms = 0;
WifiInfo info = wifi.getConnectionInfo();
if (info.getBSSID() != null) {
strength = WifiManager
.calculateSignalLevel(info.getRssi(), 100);
speed = info.getLinkSpeed();
units = WifiInfo.LINK_SPEED_UNITS;
ssid = info.getSSID();
}
long now = System.currentTimeMillis();
ms = now - mark;
mark = now;
GUIRunnable doUpdateGUI = new GUIRunnable(strength, speed, units,
ssid, ms);
handler.post(doUpdateGUI);
}
}
public class GUIRunnable implements Runnable {
private int strength;
private int speed;
private String units;
private String ssid = null;
private long ms;
public GUIRunnable(int st, int sp, String u, String ss, long m) {
strength = st;
speed = sp;
units = u;
ssid = ss;
ms = m;
}
public void run() {
updateGUI(strength, speed, units, ssid, ms);
}
}
private void updateGUI(int strength, int speed, String units, String ssid,
long ms) {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String millis = String.valueOf(System.currentTimeMillis());
String time = today.format("%k:%M:%S.")
+ millis.substring(millis.length() - 3);
tvStrength.setText("" + strength);
tvTime.setText(time + " - " + ms);
if (speed > 0)
tvSpeed.setText("" + speed + units);
if (ssid == null || ssid.length() == 0)
tvSSID.setText("No wifi connection");
else
tvSSID.setText(ssid);
// Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_LONG)
// .show();
}
#Override
protected void onResume() {
super.onResume();
isRunning = true;
}
#Override
protected void onPause() {
super.onPause();
// ssid = null;
isRunning = false;
}
}
checked google sources and it appears to me Android will only poll WIFI RSSI once every 3 seconds (most likely to save CPU and battery resources).
Here is the snippet from WifiStateMachine.java:-
/**
* Interval in milliseconds between polling for RSSI
* and linkspeed information
*/
private static final int POLL_RSSI_INTERVAL_MSECS = 3000;
The android api doesn't specify the rate at which RSSI is updated or when it is updated, but in general I doubt it is faster than once a second for power saving purposes.
I found an article on how to use a broadcast receiver to get RSSI updates (rather than continuously polling): http://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html
The API does suggest that getRSSI and other methods are simply returning data from the last scan, so likely the update rate is the wifi scan interval. This is typically controlled by the build.prop file (see line 103 of this example file: http://androidforums.com/threads/stock-build-prop-download.561012/) and could vary from device to device so likely, the most efficient way to update the user on RSSI (or other wifi parameters) is to use a broadcast receiver and update when available.
Note, based on the answers to this question, some wifi device drivers don't support being configured by the build.prop file, so even if one had root access to modify the file, it doesn't guarantee that you can change the scan interval: https://android.stackexchange.com/questions/37621/where-can-i-find-settings-for-wifi-internal-scan-period