I am working on an extensible sensing and data processing framework for Android mobile devices. It will enable a wide range of data signals (e.g., Temperature, Battery,pressure, wifi signal strength, etc.) accessible via Android mobile devices.
To test sensors, I deploy my written android code on my Android device. Here, the limitation comes ---my android device has a limited set of sensors ( for example, it does not has temperature sensor) So, I can not test all the written code for all types of sensors (e.g., temperature, pressure, etc.).
On Internet, I have checked SensorSimulator project (), but it does not work for me. I am getting the following errors.
05-03 01:40:40.766: E/AndroidRuntime(10139): FATAL EXCEPTION: main
05-03 01:40:40.766: E/AndroidRuntime(10139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworldsensorsimulator/com.example.helloworldsensorsimulator.MainActivity}: android.os.NetworkOnMainThreadException
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.Looper.loop(Looper.java:177)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.main(ActivityThread.java:4947)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-03 01:40:40.766: E/AndroidRuntime(10139): at dalvik.system.NativeStart.main(Native Method)
05-03 01:40:40.766: E/AndroidRuntime(10139): Caused by: android.os.NetworkOnMainThreadException
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-03 01:40:40.766: E/AndroidRuntime(10139): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.startupSocket(Socket.java:566)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.tryAllAddresses(Socket.java:127)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.<init>(Socket.java:177)
05-03 01:40:40.766: E/AndroidRuntime(10139): at java.net.Socket.<init>(Socket.java:149)
05-03 01:40:40.766: E/AndroidRuntime(10139): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.connect(SensorSimulatorClient.java:116)
05-03 01:40:40.766: E/AndroidRuntime(10139): at org.openintents.sensorsimulator.hardware.SensorManagerSimulator.connectSimulator(SensorManagerSimulator.java:220)
05-03 01:40:40.766: E/AndroidRuntime(10139): at com.example.helloworldsensorsimulator.MainActivity.onCreate(MainActivity.java:37)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.Activity.performCreate(Activity.java:5207)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-03 01:40:40.766: E/AndroidRuntime(10139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
05-03 01:40:40.766: E/AndroidRuntime(10139): ... 11 more
I have written the following code, Suggested on the website to connect SensorSimulator through Java Project. AndroidManifest file contains the
<uses-permission android:name="android.permission.INTERNET"/>
import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
public class MainActivity extends Activity {
private SensorManagerSimulator mSensorManager;
private SensorEventListener mEventListenerAccelerometer;
private SensorEventListener mEventListenerGravity;
private SensorEventListener mEventListenerLinearAcceleration;
private SensorEventListener mEventListenerLight;
private SensorEventListener mEventListenerTemperature;
private SensorEventListener mEventListenerOrientation;
private SensorEventListener mEventListenerMagneticField;
private SensorEventListener mEventListenerPressure;
private SensorEventListener mEventListenerRotationVector;
private SensorEventListener mEventListenerBarcode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = SensorManagerSimulator.getSystemService(this,
SENSOR_SERVICE);
mSensorManager.connectSimulator();
initListeners();
}
private void initListeners() {
mEventListenerAccelerometer = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Accelerometer: " + values[0]
+ ", " + values[1] + ", " + values[2]);
/*mTextViewAccelerometer.setText("Accelerometer: " + values[0]
+ ", " + values[1] + ", " + values[2]);*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerLinearAcceleration = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Linear Acceleration: "
+ values[0] + ", " + values[1] + ", " + values[2]);
/*mTextViewLinearAcceleration.setText("Linear Acceleration: "
+ values[0] + ", " + values[1] + ", " + values[2]);*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerGravity = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Gravity: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewGravity.setText("Gravity: " + values[0] + ", "
+ values[1] + ", " + values[2]);*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerMagneticField = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Compass: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewMagneticField.setText("Compass: " + values[0] + ", "
+ values[1] + ", " + values[2]);
*/ }
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerOrientation = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Orientation: " + values[0] + ", "
+ values[1] + ", " + values[2]);
/*mTextViewOrientation.setText("Orientation: " + values[0] + ", "
+ values[1] + ", " + values[2]);*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerTemperature = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Temperature: " + values[0]);
//mTextViewTemperature.setText("Temperature: " + values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerLight = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Light: " + values[0]);
//mTextViewLight.setText("Light: " + values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerPressure = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("Pressure: " + values[0]);
//mTextViewPressure.setText("Pressure: " + values[0]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerRotationVector = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
System.out.println("RotationVector: " + values[0]
+ ", " + values[1] + ", " + values[2]);
/* mTextViewRotationVector.setText("RotationVector: " + values[0]
+ ", " + values[1] + ", " + values[2]);*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
mEventListenerBarcode = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
System.out.println("Barcode: " + event.barcode);
//mTextViewBarcode.setText("Barcode: " + event.barcode);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
@Override
protected void onResume() {
super.onResume();
/* mSensorManager.registerListener(mEventListenerAccelerometer,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerLinearAcceleration,
mSensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerGravity,
mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerMagneticField,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerOrientation,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);*/
mSensorManager.registerListener(mEventListenerTemperature,
mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE),
SensorManager.SENSOR_DELAY_FASTEST);
/*mSensorManager.registerListener(mEventListenerLight,
mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerPressure,
mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerBarcode,
mSensorManager.getDefaultSensor(Sensor.TYPE_BARCODE_READER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mEventListenerRotationVector,
mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
SensorManager.SENSOR_DELAY_FASTEST);*/
}
@Override
protected void onStop() {
/*mSensorManager.unregisterListener(mEventListenerAccelerometer);
mSensorManager.unregisterListener(mEventListenerLinearAcceleration);
mSensorManager.unregisterListener(mEventListenerGravity);
mSensorManager.unregisterListener(mEventListenerMagneticField);
mSensorManager.unregisterListener(mEventListenerOrientation);*/
mSensorManager.unregisterListener(mEventListenerTemperature);
/*mSensorManager.unregisterListener(mEventListenerLight);
mSensorManager.unregisterListener(mEventListenerPressure);
mSensorManager.unregisterListener(mEventListenerRotationVector);
mSensorManager.unregisterListener(mEventListenerBarcode);*/
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Calling SensorManager.connectSimulator()
constitutes a network operation as shown by your stack trace. Thus, you're attempting to perform network operations on your application's main thread. Android does not allow this by default and so you have two options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With