I followed this instruction to debug my app on emulator using sensor simulator: http://code.google.com/p/openintents/wiki/SensorSimulator#How_to_use_the_in_your_application
I completed all steps above, but in my case everything was not as simple as described in instructions. I got NetworkOnMainThreadException trying to connect, register listener etc. So I created async task to solve this problem. Now, I have the following:
connection to simulator:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
sensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);
this.new ConnectToSimulator().execute();
}
onResume() listner's registration:
@Override
public void onResume() {
super.onResume();
this.new RegisterToSimulator().execute();
}
finally my tasks:
class ConnectToSimulator extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... arg0) {
try{
sensorManager.connectSimulator();
}catch(Exception e)
{
Log.i("error", e.getMessage());
}
return null;
}
}
class RegisterToSimulator extends AsyncTask<Object, Object, Object> {
@Override
protected Object doInBackground(Object... arg0) {
try{
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(Compass.this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}catch(Exception e)
{
Log.i("error", e.getMessage());
}
return null;
}
}
but even after that, I still receive the error trying to launch app on emulator:
12-15 14:23:28.877: E/AndroidRuntime(3724): FATAL EXCEPTION: main 12-15 14:23:28.877: E/AndroidRuntime(3724): android.os.NetworkOnMainThreadException 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 12-15 14:23:28.877: E/AndroidRuntime(3724): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 12-15 14:23:28.877: E/AndroidRuntime(3724): at libcore.io.IoBridge.recvfrom(IoBridge.java:513) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.InputStreamReader.read(InputStreamReader.java:244) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.BufferedReader.fillBuf(BufferedReader.java:130) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.io.BufferedReader.readLine(BufferedReader.java:354) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:654) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.readSensor(SensorSimulatorClient.java:571) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient.access$1000(SensorSimulatorClient.java:53) 12-15 14:23:28.877: E/AndroidRuntime(3724): at org.openintents.sensorsimulator.hardware.SensorSimulatorClient$1.handleMessage(SensorSimulatorClient.java:505) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.Handler.dispatchMessage(Handler.java:99) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.os.Looper.loop(Looper.java:137) 12-15 14:23:28.877: E/AndroidRuntime(3724): at android.app.ActivityThread.main(ActivityThread.java:5039) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.lang.reflect.Method.invokeNative(Native Method) 12-15 14:23:28.877: E/AndroidRuntime(3724): at java.lang.reflect.Method.invoke(Method.java:511) 12-15 14:23:28.877: E/AndroidRuntime(3724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 12-15 14:23:28.877: E/AndroidRuntime(3724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 12-15 14:23:28.877: E/AndroidRuntime(3724): at dalvik.system.NativeStart.main(Native Method)
Can someone clarify why I have so strange error? moreover there is no info in simulator instruction about async task usage necessity, maybe I am doing something wrong. Please help, thanks in advance.