Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*package com.kaanaxinc.portalstudio;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.text.InputType;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView text = new TextView(this);
- text.setText("Bonjour, vous me devez 1 000 000€.");
- setContentView(text);
- EditText aideDeTexte = new EditText(this);
- aideDeTexte.setHint("smsjoismoi");
- aideDeTexte.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
- aideDeTexte.setLines(5);
- Button bouton = new Button(this);
- bouton.setText("foobar");
- }
- String aideDeTexte3 = "jmeois";
- }
- */
- package com.kaanaxinc.portalstudio;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.ParcelUuid;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnKeyListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.NumberFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Set;
- public class MainActivity extends Activity {
- // La chaîne de caractères par défaut
- private final String defaut = "Vous devez cliquer sur le bouton « Calculer l'IMC » pour obtenir un résultat.";
- // La chaîne de caractères de la megafonction
- private final String megaString = "Vous faites un poids parfait ! Wahou ! Trop fort ! On dirait Brad Pitt (si vous êtes un homme)/Angelina Jolie (si vous êtes une femme)/Willy (si vous êtes un orque) !";
- Button envoyer = null;
- Button raz = null;
- EditText poids = null;
- EditText taille = null;
- RadioGroup group = null;
- TextView result = null;
- CheckBox mega = null;
- String texteRecu = "";
- private OutputStream outputStream;
- private InputStream inStream;
- //private ConnectedThread connectedThread = new ConnectedThread("test");
- private void init() throws IOException {
- BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
- if (blueAdapter != null) {
- if (blueAdapter.isEnabled()) {
- Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();
- if(bondedDevices.size() > 0){
- String listview3 = "";
- String listview2 = "";
- BluetoothDevice bt2 = null;
- ArrayList<String> listview =
- new ArrayList<String>();//(Arrays.asList(bondedDevices.toString()));
- for(BluetoothDevice bt : bondedDevices) {
- listview3 += (bt.getName());
- listview3 += " - ";
- listview2 += (bt);
- bt2 = bt;
- }
- listview2 = listview.toString();
- result.setText(listview3);
- //BluetoothDevice[] devices = (BluetoothDevice[]) bondedDevices.toArray();
- BluetoothDevice device = bt2;
- ParcelUuid[] uuids = device.getUuids();
- BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
- socket.connect();
- outputStream = socket.getOutputStream();
- inStream = socket.getInputStream();
- }
- Log.e("error", "No appropriate paired devices.");
- }else{
- Log.e("error", "Bluetooth is disabled.");
- }
- }
- }
- public void write(String s) throws IOException {
- outputStream.write(s.getBytes());
- }
- /*private class ConnectedThread extends Thread {
- public ConnectedThread(String str) {
- super(str);
- }*/
- public void run() {
- /*final int BUFFER_SIZE = 1024;
- byte[] buffer = new byte[BUFFER_SIZE];
- int bytes = 0;
- int b = BUFFER_SIZE;
- long startTime = System.currentTimeMillis();
- try {
- bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
- //String bytes2 = String.valueOf(bytes);
- //result.setText(bytes2);
- } catch (IOException e) {
- e.printStackTrace();
- }*/
- byte[] buffer = new byte[1024]; // buffer store for the stream
- int bytes; // bytes returned from read()
- // Keep listening to the InputStream until an exception occurs
- while (true) {
- try {
- // Read from the InputStream
- bytes = inStream.read(buffer);
- String readMessage = new String(buffer, 0, bytes);
- // Send the obtained bytes to the UI activity
- result.setText(readMessage);
- if (readMessage == "OK 1")
- break;
- } catch (IOException e) {
- break;
- }
- }
- }/*
- }*/
- void beginListenForData()
- {
- final Handler handler = new Handler();
- final byte delimiter = 10; //This is the ASCII code for a newline character
- final boolean stopWorker = false;
- final int readBufferPosition = 0;
- final byte[]readBuffer = new byte[1024];
- Thread workerThread = new Thread(new Runnable()
- {
- public void run()
- {
- while(!Thread.currentThread().isInterrupted() && !stopWorker)
- {
- try
- {
- int bytesAvailable = inStream.available();
- int readBufferPosition2 = readBufferPosition;
- if(bytesAvailable > 0)
- {
- byte[] packetBytes = new byte[bytesAvailable];
- inStream.read(packetBytes);
- for(int i=0;i<bytesAvailable;i++)
- {
- byte b = packetBytes[i];
- if(b == delimiter)
- {
- byte[] encodedBytes = new byte[readBufferPosition2];
- System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
- final String data = new String(encodedBytes);
- readBufferPosition2 = 0;
- handler.post(new Runnable()
- {
- public void run()
- {
- result.setText(data);
- }
- });
- }
- else
- {
- readBuffer[readBufferPosition2++] = b;
- }
- }
- }
- }
- catch (IOException ex)
- {
- }
- }
- }
- });
- workerThread.start();
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // On récupère toutes les vues dont on a besoin
- envoyer = (Button)findViewById(R.id.calcul);
- raz = (Button)findViewById(R.id.raz);
- taille = (EditText)findViewById(R.id.taille);
- poids = (EditText)findViewById(R.id.poids);
- mega = (CheckBox)findViewById(R.id.mega);
- group = (RadioGroup)findViewById(R.id.group);
- result = (TextView)findViewById(R.id.result);
- // On attribue un listener adapté aux vues qui en ont besoin
- envoyer.setOnClickListener(envoyerListener);
- raz.setOnClickListener(razListener);
- taille.addTextChangedListener(textWatcher);
- poids.addTextChangedListener(textWatcher);
- // Solution avec des onKey
- //taille.setOnKeyListener(modificationListener);
- //poids.setOnKeyListener(modificationListener);
- mega.setOnClickListener(checkedListener);
- }
- /*@Override
- public void onResume()
- {
- super.onResume();
- //It is best to check BT status at onResume in case something has changed while app was paused etc
- checkBTState();
- try {
- btSocket = createBluetoothSocket(device);
- } catch (IOException e) {
- Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show();
- }
- // Establish the Bluetooth socket connection.
- try
- {
- btSocket.connect();
- } catch (IOException e) {
- try
- {
- btSocket.close();
- } catch (IOException e2)
- {
- //insert code to deal with this
- }
- }
- mConnectedThread = new ConnectedThread(btSocket);
- mConnectedThread.start();
- }*/
- /*private class ConnectedThread extends Thread {
- private final InputStream mmInStream;
- private final OutputStream mmOutStream;
- //creation of the connect thread
- public ConnectedThread(BluetoothSocket socket) {
- InputStream tmpIn = null;
- OutputStream tmpOut = null;
- try {
- //Create I/O streams for connection
- tmpIn = socket.getInputStream();
- tmpOut = socket.getOutputStream();
- } catch (IOException e) { }
- mmInStream = tmpIn;
- mmOutStream = tmpOut;
- }
- public void run() {
- byte[] buffer = new byte[256];
- int bytes;
- // Keep looping to listen for received messages
- while (true) {
- try {
- bytes = mmInStream.read(buffer); //read bytes from input buffer
- String readMessage = new String(buffer, 0, bytes);
- // Send the obtained bytes to the UI Activity via handler
- bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
- } catch (IOException e) {
- break;
- }
- }
- }
- //write method
- public void write(String input) {
- byte[] msgBuffer = input.getBytes(); //converts entered String into bytes
- try {
- mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream
- } catch (IOException e) {
- //if you cannot write, close the application
- Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
- finish();
- }
- }
- }*/
- /*private void checkBTState()
- {
- // Check device has Bluetooth and that it is turned on
- mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
- if(mBluetoothAdapter==null) {
- Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
- finish();
- } else {
- if (!mBluetoothAdapter.isEnabled()) {
- //Prompt user to turn on Bluetooth
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, 1);
- }
- }
- }*/
- /*
- // Se lance à chaque fois qu'on appuie sur une touche en étant sur un EditText
- private OnKeyListener modificationListener = new OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- // On remet le texte à sa valeur par défaut pour ne pas avoir de résultat incohérent
- result.setText(defaut);
- return false;
- }
- }*/
- private TextWatcher textWatcher = new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- result.setText(defaut);
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- }
- };
- // Uniquement pour le bouton "envoyer"
- private OnClickListener envoyerListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- /*if(!mega.isChecked()) {
- // Si la megafonction n'est pas activée
- // On récupère la taille
- String t = taille.getText().toString();
- // On récupère le poids
- String p = poids.getText().toString();
- // Puis on vérifie que la taille est cohérente
- if (t.length() == 0 || p.length() == 0) {
- result.setText("Veuillez entrer votre poids et votre taille.");
- } else {
- float tValue = Float.valueOf(t);
- if(tValue == 0)
- Toast.makeText(MainActivity.this, "Hého, tu es un Minipouce ou quoi ?", Toast.LENGTH_SHORT).show();
- else {
- float pValue = Float.valueOf(p);
- // Si l'utilisateur a indiqué que la taille était en centimètres
- // On vérifie que la Checkbox sélectionnée est la deuxième à l'aide de son identifiant
- if(group.getCheckedRadioButtonId() == R.id.radio2)
- tValue = tValue / 100;
- tValue = (float)Math.pow(tValue, 2);
- float imc = pValue / tValue;
- result.setText("Votre IMC est " + String.valueOf(imc));
- }
- }
- } else
- result.setText(megaString);
- */
- try {
- init();
- } catch (IOException e2) {
- }
- }
- };
- // Listener du bouton de remise à zéro
- private OnClickListener razListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- /*poids.getText().clear();
- taille.getText().clear();
- result.setText(defaut);
- */
- String aEnvoyer = poids.getText().toString();
- try {
- write(aEnvoyer);
- } catch (IOException e1) {
- }
- texteRecu = "***";
- result.setText(texteRecu);
- beginListenForData();
- }
- };
- // Listener du bouton de la megafonction.
- private OnClickListener checkedListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // On remet le texte par défaut si c'était le texte de la megafonction qui était écrit
- if(!((CheckBox)v).isChecked() && result.getText().equals(megaString))
- result.setText(defaut);
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement