Étape 6: Android:. Classe Code partie 2.
LedControl classe ouverte et importer les packages suivants :
import android.bluetooth.BluetoothSocket; import android.content.Intent;import android.view.View;import android.widget.Button;import android.widget.SeekBar;import android.widget.TextView;import android.widget.Toast;import android.app.ProgressDialog;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.os.AsyncTask;import java.io.IOException;import java.util.UUID;
Créer les widget des variables suivantes :
Button btnOn, btnOff, btnDis;SeekBar brightness;String address = null;private ProgressDialog progress;BluetoothAdapter myBluetooth = null;BluetoothSocket btSocket = null;private boolean isBtConnected = false;static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
Nous avons initialiser les variables et récupérer que l’adresse de périphérique bluetooth obtenu dans leurs classe.
//receive the address of the bluetooth deviceIntent newint = getIntent();address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); //view of the ledControl layout setContentView(R.layout.activity_led_control);//call the widgtesbtnOn = (Button)findViewById(R.id.button2);btnOff = (Button)findViewById(R.id.button3);btnDis = (Button)findViewById(R.id.button4);brightness = (SeekBar)findViewById(R.id.seekBar);
Nous allons créer une classe pour démarrer la connexion :
private class ConnectBT extends AsyncTask<Void, Void, Void> // UI thread{ private boolean ConnectSuccess = true; //if it's here, it's almost connected protected void onPreExecute() { progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please wait!!!"); //show a progress dialog } protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background { try { if (btSocket == null || !isBtConnected) { myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); btSocket.connect();//start connection } } catch (IOException e) { ConnectSuccess = false;//if the try failed, you can check the exception here } return null; } protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine { super.onPostExecute(result); if (!ConnectSuccess) { msg("Connection Failed. Is it a SPP Bluetooth? Try again."); finish(); } else { msg("Connected."); isBtConnected = true; } progress.dismiss(); } }
Nous avons besoin « d’écouter » lorsque le bouton est cliqué pour écrire une commande pour allumer / éteindre la led, débrancher et le contrôle de la luminosité.
btnOn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { turnOnLed(); //method to turn on } });btnOff.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { turnOffLed(); //method to turn off } });btnDis.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Disconnect(); //close connection } });brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser==true) { lumn.setText(String.valueOf(progress)); try { btSocket.getOutputStream().write(String.valueOf(progress).getBytes()); } catch (IOException e) { } } } public void onStartTrackingTouch(SeekBar seekBar) { } public void onStopTrackingTouch(SeekBar seekBar) { } });
Il existe une méthode appelée msg() ; Cette méthode appelle Toast.maketext() ; Il y a la méthode Disconnect() appelé ; turnOffLed() ; et turnOnLed() ;
private void msg(String s) { Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); }
private void Disconnect(){ if (btSocket!=null) //If the btSocket is busy { try { btSocket.close(); //close connection } catch (IOException e) { msg("Error");} } finish(); //return to the first layout}
private void turnOffLed() { if (btSocket!=null) { try { btSocket.getOutputStream().write("TF".toString().getBytes()); } catch (IOException e) { msg("Error"); } } }
private void turnOnLed() { if (btSocket!=null) { try { btSocket.getOutputStream().write("TO".toString().getBytes()); } catch (IOException e) { msg("Error"); } } }