Help please


#1

Hi, I wrote this code to control some motors using esp8266, but sometimes, apparently without reasons, the buttons stop work after I used accelerometer. Does someone can help me?

package com.matteoarcangeli.WiFiCar

import android.os.StrictMode
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL


class Car(private var url:URL) {

    fun muovi(cmd: String) {
        val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
        StrictMode.setThreadPolicy(policy)

            val urlConnection = URL(url.toString() + '/' + cmd).openConnection() as HttpURLConnection
            try {
                val inp = BufferedInputStream(urlConnection.inputStream)
                inp.close()
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                urlConnection.disconnect()
            }
        System.gc()
    }
}
package com.matteoarcangeli.WiFiCar

import android.annotation.SuppressLint
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.AsyncTask.execute
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Html.FROM_HTML_MODE_LEGACY
import android.text.Html.fromHtml
import android.view.View
import android.widget.CompoundButton
import android.widget.Switch
import android.widget.TextView
import com.matteoarcangeli.WiFiCar.R.id.swAcc
import java.net.URL
import kotlin.math.floor


class MainActivity : AppCompatActivity(), SensorEventListener, CompoundButton.OnCheckedChangeListener {

    private lateinit var sensorManager: SensorManager
    private lateinit var swAccellerometro: Switch
    private lateinit var txtAccellerometo: TextView
    private var car = Car(URL("http://192.168.4.1"))

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        //TODO(Disabilitare accellerometro quando si disabilita lo sitwch-- > Fatto)
        //TODO(Guardare disconnessione ad ogni comando)
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (swAccellerometro.isChecked)
            sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL)
        else {
            sensorManager.unregisterListener(this)
            txtAccellerometo.text = "Accellerometro disabilitato"
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onSensorChanged(event: SensorEvent?) {

        val x = floor(event!!.values[0] * 100) / 100
        val y = floor(event.values[1] * 100) / 100
        val z = floor(event.values[2] * 100) / 100
        /*if (abs(x) > 0.35 || abs(z) > 0.5) {*/

        Thread({ txtAccellerometo.text = fromHtml("<p><b>x: </b>$x</p><p><b>y: </b> $y</p><p><b>z: </b> $z</p>", FROM_HTML_MODE_LEGACY) }).start()

        when {
        //(x < 0.25 && x > -0.25) || (x > -0.7 && z < -0.25) -> car.muovi("stop")
            x > 3 -> execute { car.muovi("left") }
            x < -3 -> execute { car.muovi("right") }
            y < 1.5 -> execute { car.muovi("backward") }
            y > -1.5 -> execute { car.muovi("forward") }
            else -> execute { car.muovi("stop") }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager

        swAccellerometro = findViewById(swAcc)
        swAccellerometro.setOnCheckedChangeListener(this)
        txtAccellerometo = findViewById(R.id.textView)
        txtAccellerometo.text = "Accellerometro disabilitato"

        findViewById<View>(R.id.btnAvanti).setOnClickListener {
            execute {
                car.muovi("forward")
            }
        }


        findViewById<View>(R.id.btnIndietro).setOnClickListener {
            execute {
                car.muovi("backward")
            }
        }

        findViewById<View>(R.id.btnDestra).setOnClickListener {
            execute {
                car.muovi("right")
            }
        }

        findViewById<View>(R.id.btnSinistra).setOnClickListener {
            execute {
                car.muovi("left")
            }
        }

        findViewById<View>(R.id.btnStop).setOnClickListener {
            execute {
                car.muovi("stop")
            }
        }
    }
}