Bonjour,
Je vais essayer de faire court, je souhaite faire une application
android en java avec android studio. Pour ce faire, mon but est d'envoyer 4 bytes en bluetooth à une carte arduino dont 2 sont obtenues avec l'accéléromètre du téléphone.
Les bytes bverticale et brotation sont obtenue via des boutons dans l'application et je pense ne pas avoir de problèmes avec eux.
Là où c'est plus compliqué, c'est pour les deux autres. J'ai fais une classe MouvementView en dehors du MainActivity où se trouve ma classe MyBluetoothClass avec sa method qui m'intéresse, la writebyte(byte b) et mes 4 variables en bytes
La classe MouvementView me servait à la base pour afficher une image qui se déplace sur l'écran quand je bouge le téléphone. Le problème c'est que je n'arrivai plus à faire un onSensorChanged dans mon MainActivity. J'essaye donc d'envoyer mes 4 variables dans la classe MouvementView ainsi que ma méthode writebyte(byte b) et mes 4 setText (car oui, je souhaite aussi afficher les valeurs que j'envoies).
Sauf que voilà, j'ai assez peu d'expérience en java et malgré pas mal d'heures de recherche, je n'ai pas réussi à faire fonctionner mon writebyte(byte b) ou mon setText.
Voici ce qu'il y a dans le MainActivity:
package fr. alexandre. xzurucontroller20;
import android. annotation. SuppressLint;
import android. bluetooth. BluetoothAdapter;
import android. bluetooth. BluetoothDevice;
import android. bluetooth. BluetoothSocket;
import android. content. Intent;
import android. hardware. Sensor;
import android. hardware. SensorManager;
import android. os. Bundle;
import android. os. Handler;
import android. os. Message;
import android. support. v7. app. AppCompatActivity;
import android. view. View;
import android. widget. AdapterView;
import android. widget. ArrayAdapter;
import android. widget. Button;
import android. widget. ListView;
import android. widget. TextView;
import java. io. IOException;
import java. io. InputStream;
import java. io. OutputStream;
import java. util. ArrayList;
import java. util. Set;
import java. util. UUID;
public class MainActivity extends AppCompatActivity {
private SensorManager mgr = null ;
private MouvementView view;
private TextView tv_status;
private ListView lv_devlist;
private BluetoothAdapter my_bt_adapter;
public static MyBluetoothClass mybluetooth;
private BluetoothSocket my_bt_soket = null ;
private OutputStream my_bt_out_stream = null ;
private InputStream my_bt_inp_stream = null ;
private String dev_address;
static final UUID myUUID = UUID. fromString ( "00001101-0000-1000-8000-00805F9B34FB" );
private Handler my_handler;
private final static int STATUS = 1 ;
private Button bon;
private Button boff;
private TextView textTransversale;
private TextView textLongitudinale;
private TextView textVerticale;
private TextView textRotation;
private Button bhaut;
private Button bbas;
private Button bpd;
private Button bpg;
private Button bcentre;
byte btransversale = 50 ;
byte blongitudinale = 50 ;
byte bverticale = 0 ;
byte brotation = 50 ;
@SuppressLint ( "HandlerLeak" )
@Override
public void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState);
setContentView ( R. layout. activity_main);
view = ( MouvementView) findViewById ( R. id. TheMouvementView);
mgr = ( SensorManager) getSystemService ( SENSOR_SERVICE);
tv_status = ( TextView) findViewById ( R. id. TV_STATUS);
lv_devlist = ( ListView) findViewById ( R. id. LV_DEVLIST);
my_handler = new Handler () {
public void handleMessage ( Message msg) {
switch ( msg. what) {
case STATUS:
tv_status. setText (( String)( msg. obj));
break ;
}
}
} ;
my_bt_adapter = BluetoothAdapter. getDefaultAdapter ();
if ( my_bt_adapter == null ) {
tv_status. setText ( "Pas d'interface Bluetooth" );
}
if (! my_bt_adapter. isEnabled ()) {
Intent turnOn = new Intent ( BluetoothAdapter. ACTION_REQUEST_ENABLE);
startActivityForResult ( turnOn, 0 );
}
while (! my_bt_adapter. isEnabled ()) ;
Set<BluetoothDevice> pairedDevices = my_bt_adapter. getBondedDevices ();
if ( pairedDevices. isEmpty ()) tv_status. setText ( "Liste Vide" );
ArrayList pairedlist = new ArrayList ();
for ( BluetoothDevice bt : pairedDevices)
pairedlist. add ( bt. getName () + " \n " + bt. getAddress ());
ArrayAdapter my_list_adapter = new ArrayAdapter ( this , android. R. layout. simple_list_item_1, pairedlist);
lv_devlist. setAdapter ( my_list_adapter);
tv_status. setText ( "Choisir un device dans la liste" );
lv_devlist. setOnItemClickListener ( devlist_listener);
this . textVerticale = findViewById ( R. id. textVerticale);
this . textRotation = findViewById ( R. id. textRotation);
this . textLongitudinale = findViewById ( R. id. textLongitudinale);
this . textTransversale = findViewById ( R. id. textTransversale);
this . bon = findViewById ( R. id. bon);
this . boff = findViewById ( R. id. boff);
this . bhaut = findViewById ( R. id. bhaut);
this . bbas = findViewById ( R. id. bbas);
this . bpd = findViewById ( R. id. bpd);
this . bpg = findViewById ( R. id. bpg);
this . bcentre = findViewById ( R. id. bcentre);
bon. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
btransversale= 50 ;
blongitudinale= 50 ;
bverticale= 0 ;
brotation= 0 ;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
boff. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
btransversale= 50 ;
blongitudinale= 50 ;
bverticale= 0 ;
brotation= 100 ;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
bcentre. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
btransversale= 50 ;
blongitudinale= 50 ;
bverticale= 0 ;
brotation= 50 ;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
bhaut. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
int verticale = bverticale + 1 ;
if ( verticale> 100 ) {
verticale = 100 ;
}
bverticale = ( byte ) verticale;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
bbas. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
int verticale = bverticale - 1 ;
if ( verticale< 0 ) {
verticale = 0 ;
}
bverticale = ( byte ) verticale;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
bpd. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
int rotation = brotation + 1 ;
if ( rotation> 100 ) {
rotation = 100 ;
}
brotation = ( byte ) rotation;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
bpg. setOnClickListener ( new View. OnClickListener () {
@Override
public void onClick ( View v) {
int rotation = brotation - 1 ;
if ( rotation< 0 ) {
rotation = 0 ;
}
brotation = ( byte ) rotation;
textTransversale. setText ( "Transversale: " + btransversale + " %" );
textLongitudinale. setText ( "Longitudinale: " + blongitudinale + " %" );
textVerticale. setText ( "Verticale: " + bverticale + " %" );
textRotation. setText ( "Rotation: " + brotation + " %" );
}
} );
}
public AdapterView. OnItemClickListener devlist_listener = new AdapterView. OnItemClickListener () {
public void onItemClick ( AdapterView av, View v, int arg2, long arg3) {
String devchoisi = (( TextView) v). getText (). toString ();
dev_address = devchoisi. substring ( devchoisi. length () - 17 );
tv_status. setText ( "CONNEXION EN COURS" );
mybluetooth = new MyBluetoothClass ();
mybluetooth. start ();
}
} ;
public class MyBluetoothClass extends Thread {
public void run () {
boolean SOCKET_OK, CONX_OK, OUTS_OK, INPS_OK;
BluetoothDevice HC05 = my_bt_adapter. getRemoteDevice ( dev_address);
SOCKET_OK = true ;
try {
my_bt_soket = HC05. createInsecureRfcommSocketToServiceRecord ( myUUID);
} catch ( IOException e) {
SOCKET_OK = false ;
}
if ( SOCKET_OK) {
CONX_OK = true ;
try {
my_bt_soket. connect ();
} catch ( IOException e) {
CONX_OK = false ;
}
if ( CONX_OK) {
OUTS_OK = true ;
try {
my_bt_out_stream = my_bt_soket. getOutputStream ();
} catch ( IOException e) {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Echec création OUTPUT stream" ). sendToTarget ();
OUTS_OK = false ;
}
INPS_OK = true ;
try {
my_bt_inp_stream = my_bt_soket. getInputStream ();
} catch ( IOException e) {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Echec création INPUT STREAM" ). sendToTarget ();
INPS_OK = false ;
}
if ( OUTS_OK && INPS_OK)
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Connécté" ). sendToTarget ();
} else {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Echec Connexion" ). sendToTarget ();
}
} else {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Echec création Soket COMM" ). sendToTarget ();
}
}
public void writebyte ( byte b) {
try {
my_bt_out_stream. write ( b);
} catch ( IOException e) {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Erreur dans writebyte" ). sendToTarget ();
}
}
void disconnect () {
try {
my_bt_soket. close ();
} catch ( IOException e) {
my_handler. obtainMessage ( STATUS, - 1 , - 1 , "Echec Déconnexion" ). sendToTarget ();
}
}
}
public void deconnecter ( View view) {
mybluetooth. disconnect ();
tv_status. setText ( "Déconnecté" );
}
@Override
protected void onResume () {
super . onResume ();
mgr. registerListener ( view, mgr. getDefaultSensor ( Sensor. TYPE_ACCELEROMETER), mgr. SENSOR_DELAY_GAME);
}
@Override
protected void onPause () {
super . onPause ();
mgr. unregisterListener ( view);
}
}
et voici ce qu'il y a dans mon MouvementView:
package fr. alexandre. xzurucontroller20;
import android. content. Context;
import android. graphics. Bitmap;
import android. graphics. BitmapFactory;
import android. graphics. Canvas;
import android. graphics. Paint;
import android. hardware. Sensor;
import android. hardware. SensorEvent;
import android. hardware. SensorEventListener;
import android. util. AttributeSet;
import android. util. Log;
import android. view. View;
import android. widget. TextView;
public class MouvementView extends View implements SensorEventListener {
private Paint paint = new Paint ( Paint. ANTI_ALIAS_FLAG);
private Bitmap droneBitmap;
private Bitmap centreBitmap;
private int imageWidth;
private int imageHeight;
private int currentX;
private int currentY;
private int centreX;
private int centreY;
private TextView textTransversale;
private TextView textLongitudinale;
private TextView textVerticale;
private TextView textRotation;
byte btransversale = 50 ;
byte blongitudinale = 50 ;
byte bverticale = 0 ;
byte brotation = 50 ;
public MouvementView ( Context context) {
super ( context);
}
public MouvementView ( Context context, AttributeSet attrSet) {
super ( context, attrSet);
}
@Override
public void onSizeChanged ( int width, int height, int oldw, int oldh) {
super . onSizeChanged ( width, height, oldw, oldh);
droneBitmap = BitmapFactory. decodeResource ( getResources (), R. drawable. drone);
centreBitmap = BitmapFactory. decodeResource ( getResources (), R. drawable. centre);
imageWidth = droneBitmap. getWidth ();
imageHeight = droneBitmap. getHeight ();
int imageCentreWidth = centreBitmap. getWidth ();
int imageCentreHeight = centreBitmap. getHeight ();
currentX = ( width - imageWidth) / 2 ;
currentY = ( height - imageHeight) / 2 ;
centreX = ( width - imageCentreWidth) / 2 ;
centreY = ( height - imageCentreHeight) / 2 ;
}
@Override
public void onDraw ( Canvas canvas) {
super . onDraw ( canvas);
canvas. drawBitmap ( droneBitmap, currentX, currentY, paint);
canvas. drawBitmap ( centreBitmap, centreX, centreY, paint);
}
@Override
public void onAccuracyChanged ( Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged ( SensorEvent sensorEvent) {
float x = sensorEvent. values[ 0 ]- 4 ;
if ( x> 5 ) {
x = 5 ;
} else if ( x<- 5 ) {
x = - 5 ;
}
double dlongitudinale = 10 * x + 50 ;
blongitudinale = ( byte ) Math. floor ( dlongitudinale);
float y = sensorEvent. values[ 1 ];
if ( y> 5 ) {
y = 5 ;
} else if ( y<- 5 ) {
y = - 5 ;
}
double dtransversale = 10 * x + 50 ;
btransversale = ( byte ) Math. floor ( dtransversale);
Log. i ( "DEBUG" , x + " - " + y);
MainActivity. mybluetooth. writebyte ( blongitudinale);
MainActivity. mybluetooth. writebyte ( btransversale);
MainActivity. mybluetooth. writebyte ( bverticale);
MainActivity. mybluetooth. writebyte ( brotation);
this . moveImage ( y* 6 , x* 6 );
}
public void moveImage ( float x, float y) {
currentX = ( int ) x* 6 + ( getWidth () - imageWidth)/ 2 ;
currentY = ( int ) y* 6 + ( getHeight () - imageHeight)/ 2 ;
this . invalidate ();
}
public static void main ( String[] args) {
}
}
mise à part ce problème tout semble marcher correctement, bien que je ne peux pas encore tester le bluetooth (les setText sont aussi là pour vérifier ce que j'enverrai du coup)
Je suis conscient que c'est peut être un peu beaucoup demander mais bon, en tout cas merci à ceux qui essayent de m'aider.
Configuration: Windows / Chrome 91.0.4472.106
Afficher la suite