Go to Post At one point, we were all once rookies. - Vin211 [more]
Home
Go Back   Chief Delphi > Other > FIRST Tech Challenge
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 16-11-2016, 00:12
jkgjur jkgjur is offline
Registered User
FTC #9830
 
Join Date: Nov 2016
Location: United States
Posts: 1
jkgjur is an unknown quantity at this point
[FTC]: [FTC]: Using Android's Built in Sensors

I have been playing around with getting the built in sensors in the ZTE phones to return some values for me. Currently, I just want it to return the different sensors available on the phone. I have written code that will do this, but as soon as I initialized the robot, it gave off this error:

Code:
Can't create handler inside thread that has not called Looper.prepare()
I looked up the error and I tried using the methods that were given. I wrote some more code that could have possibly fixed it, but the same error was returned. This is the current code that I am testing:

My Sensors class
Code:
package org.firstinspires.ftc.robotcontroller.internal.sensor;

import android.app.Activity;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.os.Handler;
import android.os.Looper;

import java.util.List;

public class Sensors extends Activity implements SensorEventListener {

    private final SensorManager sensorManager;

    public Sensors() {
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    }

    protected void onResume() {
        super.onResume();
    }

    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
    }

    public String listAvailableSensors() {
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL); // gets list of all sensors available on the phone
        return sensorList.toString();
    }



}
My TestPhoneSensor class
Code:
package org.firstinspires.ftc.robotcontroller.internal.sensor;

import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;

public class TestPhoneSensor extends OpMode {

    Sensors sensor;

    @Override
    public void init() {
        sensor = new Sensors();
    }

    @Override
    public void init_loop() {

    }


    class LooperThread extends Thread { // code block that was supposed to "fix" it
        public Handler mHandler;

        public void run() {
            Looper.prepare();

            mHandler = new Handler() {
                public void handleMessage(Message msg) {

                }
            };

            Looper.loop();
        }
    }



    @Override
    public void loop() {
        new Thread() { // other code block that was supposed to "fix" it
            public void run() {
                new Sensors().runOnUiThread(new Runnable(){

                    @Override
                    public void run(){
                        try {
                            telemetry.addData("Sensor List:", sensor.listAvailableSensors());//Edit,integrate this on the runOnUiThread
                        } catch (Exception e) {
                            telemetry.addData("Error:", e.getStackTrace());
                        }
                    }
                });
            }
        }.start();

    }




}
Any help is appreciated.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 17:58.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi