Android 蓝牙控制器的按钮功能管理和方法调用
向大家致以亲切的问候 我最近加入了这个社区,但我对Android编程不是很有经验,所以我向大家致辞,希望能解决一个问题 我正在开发一个Android应用程序,它允许我通过蓝牙控制一个机器人,该机器人使用手机作为陀螺仪控制器,并通过按钮执行命令。 我希望方向命令的发送由ToggleButton管理;也就是说:只有在压力和转换到就绪状态之后,发送才会在下一个压力和转换到未就绪状态时被禁用。 我的问题是,在第一次按下后,能否调用“onSensorChanged”陀螺传感器方法 主要活动如下():Android 蓝牙控制器的按钮功能管理和方法调用,android,android-studio,Android,Android Studio,向大家致以亲切的问候 我最近加入了这个社区,但我对Android编程不是很有经验,所以我向大家致辞,希望能解决一个问题 我正在开发一个Android应用程序,它允许我通过蓝牙控制一个机器人,该机器人使用手机作为陀螺仪控制器,并通过按钮执行命令。 我希望方向命令的发送由ToggleButton管理;也就是说:只有在压力和转换到就绪状态之后,发送才会在下一个压力和转换到未就绪状态时被禁用。 我的问题是,在第一次按下后,能否调用“onSensorChanged”陀螺传感器方法 主要活动如下(): 导入
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.graphics.drawable.drawable;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.widget.Toast;
导入android.widget.ToggleButton;
导入java.io.IOException;
导入java.io.OutputStream;
导入java.util.UUID;
导入java.io.OutputStream;
公共类MainActivity扩展AppCompatActivity实现SensorEventListener{
私人传感器管理器;
专用传感器旋转矢量传感器;
私人监听者;
//阴道镜检查的变异性
私有文本视图assex;
私有文本视图;
私有文本视图assez;
//每频率可变
私有图像视图向上;
私有图像视图箭头向下;
私有图像视图左箭头;
私人影像查看权限;
//波托尼变量
私人按钮校准;
私人按钮自动;
私人按钮停止;
私有切换按钮切换状态;
//每种食品的价格差异
浮动补偿;
浮动补偿;
浮动[]方向=新浮动[3];
//易变的个人协会deispositivo蓝牙
公共UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
//可变蓝牙
BluetoothSocket mmSocket=null;
蓝牙设备mmDevice=null;
输出流外流;
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
//可变视觉化stato传感器
专用文本视图传感器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//每一次视觉化
assex=(TextView)findViewById(R.id.assexvalue);
assey=(TextView)findViewById(R.id.asseyvalue);
assez=(TextView)findViewById(R.id.assezvalue);
//每一次视觉化
arrowup=(ImageView)findViewById(R.id.imageViewUp);
箭头向下=(ImageView)findViewById(R.id.imageViewDown);
arrowleft=(ImageView)findViewById(R.id.imageViewLeft);
arrowright=(ImageView)findViewById(R.id.imageViewRight);
//波托尼的伊曼基尼酒店
校准=(按钮)findViewById(R.id.calibrate);
自动=(按钮)findViewById(R.id.auto);
停止=(按钮)findViewById(R.id.stop);
toggleState=(ToggleButton)findViewById(R.id.toggleState);
//每一次视觉化
传感器=(TextView)findViewById(R.id.sensor);
//controllo presenza giroscopio传感器
msSensorManager=(SensorManager)this.getSystemService(Activity.SENSOR\u服务);
rotationVectorSensor=msSensorManager.getDefaultSensor(传感器.类型\旋转\向量);
if(旋转矢量传感器==null)
sensor.setText(“Il sensore nonèpresente”);
其他的
sensor.setText(“Il sensoreèattivo”);
/***************普桑蒂宫***********************/
//脉冲校准:
calibrate.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
补偿Y=-Math.round(方向[1]);
补偿z=-Math.round(方向[2]);
}
});
//脉冲停止!:
stop.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
sendMessageBluetooth(“N”);//因维奥-科曼多-迪-斯托普!
}
});
//脉动自动:
auto.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
sendMessageBluetooth(“M”);//因维奥公司
}
});
//ToggleState就绪!/未就绪!://RIVEDI!!!!!!!
toggleState.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!toggleState.isChecked()){
//dopo la prima pressione del toggle按钮c’la scritta未读取!
//所有第二个按钮都是切换按钮
//非因维奥阿尔昆克拉特酒店
}
/*雷·弗雷奇·索诺双稳态酒店*/
否则{
//苏尔·波托内·克莱拉·斯克里塔准备好了!
//所有主要的按钮都可以切换到nella suddetta modalitée
//chiamo la funzione“onSensorChanged”
//因维奥·科曼迪·弗雷切
}
}
});
/***************************************************************/
/*****************手势e控制器连接蓝牙**********************/
if(mBluet
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor rotationVectorSensor;
private SensorEventListener mListener;
//variabili per misure giroscopio
private TextView assex;
private TextView assey;
private TextView assez;
//variabili per frecce
private ImageView arrowup;
private ImageView arrowdown;
private ImageView arrowleft;
private ImageView arrowright;
//variabili per i bottoni
private Button calibrate;
private Button auto;
private Button stop;
private ToggleButton toggleState;
//variabili per la compensazione della misura della posizione angolare rilevata dal giroscopio
float compensationy;
float compensationz;
float[] orientations = new float[3];
//variabile per associazione deispositivo bluetooth
public UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//variabili gestione bluetooth
BluetoothSocket mmSocket=null;
BluetoothDevice mmDevice=null;
OutputStream outStream;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//variabile visualizzazione stato sensore gioscopico
private TextView sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//per visualizzare le misure del giroscopio
assex = (TextView) findViewById(R.id.assexvalue);
assey = (TextView) findViewById(R.id.asseyvalue);
assez = (TextView) findViewById(R.id.assezvalue);
//per visualizzare le immagini delle frecce
arrowup = (ImageView) findViewById(R.id.imageViewUp);
arrowdown = (ImageView) findViewById(R.id.imageViewDown);
arrowleft = (ImageView) findViewById(R.id.imageViewLeft);
arrowright = (ImageView) findViewById(R.id.imageViewRight);
//per visualizzare le immagini del bottoni
calibrate = (Button) findViewById(R.id.calibrate);
auto = (Button) findViewById(R.id.Auto);
stop = (Button) findViewById(R.id.Stop);
toggleState = (ToggleButton) findViewById(R.id.ToggleState);
//per visualizzare lo stato del giroscopio
sensor = (TextView) findViewById(R.id.sensor);
//controllo presenza sensore giroscopio
mSensorManager = (SensorManager) this.getSystemService(Activity.SENSOR_SERVICE);
rotationVectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
if(rotationVectorSensor==null)
sensor.setText("Il sensore non è presente");
else
sensor.setText("Il sensore è attivo");
/*************** GESTIONE PULSANTI ***********************/
//Pulsante CALIBRATE:
calibrate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compensationy = -Math.round(orientations[1]);
compensationz = -Math.round(orientations[2]);
}
});
//Pulsante STOP!:
stop.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
sendMessageBluetooth("N"); //invio comando di STOP!
}
});
//Pulsante AUTO:
auto.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
sendMessageBluetooth("M"); //invio comando di FUNZIONAMENTO AUTOMATICO
}
});
//ToggleState READY!/NOT READY!: //RIVEDI!!!!!!!
toggleState.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(!toggleState.isChecked()){
//dopo la prima pressione del toggle button c'è la scritta NOT READ!
//alla seconda pressione del del toggle button entro nella suddetta modalità e
// non invio alcun carattere
}
/*le frecce sono disabilitate*/
else {
//sul bottone inizialmente c'è la scritta READY!
//alla prima pressione del del toggle button entro nella suddetta modalità e
// chiamo la funzione 'onSensorChanged'
//invio comandi frecce
}
}
});
/***************************************************************/
/***************** Gestione e controllo connessione bluetooth **********************/
if (mBluetoothAdapter == null){
// IL BLUETOOTH NON E' SUPPORTATO
Toast.makeText(MainActivity.this, "BlueTooth non supportato", Toast.LENGTH_LONG).show();
}
else {
if (!mBluetoothAdapter.isEnabled())//controlla che sia abilitato il devices
// NON E' ABILITATO IL BLUETOOTH
Toast.makeText(MainActivity.this, "BlueTooth non abilitato", Toast.LENGTH_LONG).show();
else {
mmDevice = mBluetoothAdapter.getRemoteDevice("00:12:12:24:17:08");
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
}
catch (IOException e) {
;
}
try {
mmSocket.connect();
outStream = mmSocket.getOutputStream();
Toast.makeText(MainActivity.this, "ON", Toast.LENGTH_SHORT).show();
}
catch (IOException closeException) {
try {
mmSocket.close();
}
catch (IOException ceXC) {
;
}
}
}
}
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, rotationVectorSensor, SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(
rotationMatrix, sensorEvent.values);
// Remap coordinate system
float[] remappedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix,
SensorManager.AXIS_Y,
SensorManager.AXIS_MINUS_X,
remappedRotationMatrix);
// Convert to orientations
SensorManager.getOrientation(remappedRotationMatrix, orientations);
for(int i = 0; i < 3; i++) {
orientations[i] = (float)(Math.toDegrees(orientations[i]));
}
float valuey = Math.round((orientations[1]) + compensationy);
float valuez = Math.round((orientations[2]) + compensationz);
assex.setText(Float.toString(Math.round(orientations[0])));
assey.setText(Float.toString( valuey ));
assez.setText(Float.toString( valuez ));;
if(valuey > 20 ) {
arrowup.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.INVISIBLE);
sendMessageBluetooth("W");
}
else if(valuey < -20 ) {
arrowdown.setVisibility(View.VISIBLE);
arrowup.setVisibility(View.INVISIBLE);
sendMessageBluetooth("S");
}
else if (valuez > 20) {
arrowright.setVisibility(View.VISIBLE);
arrowleft.setVisibility(View.INVISIBLE);
sendMessageBluetooth("D");
}
else if (valuez < -20) {
arrowleft.setVisibility(View.VISIBLE);
arrowright.setVisibility(View.INVISIBLE);
sendMessageBluetooth("A");
}
else if (valuez > -20 && valuez < -15 || valuey < 20 && valuey > 15|| valuey > -20 && valuey < -15|| valuez < 20 && valuez > 15 ){
sendMessageBluetooth("P");
}
else {
arrowup.setVisibility(View.INVISIBLE);
arrowdown.setVisibility(View.INVISIBLE);
arrowleft.setVisibility(View.INVISIBLE);
arrowright.setVisibility(View.INVISIBLE);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
;
}
public void sendMessageBluetooth(String message) {
if (outStream == null)
return;
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
}
catch (IOException e) {
;
}
}