Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.27 KB | None | 0 0
  1. /*package com.kaanaxinc.portalstudio;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.text.InputType;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9.  
  10.  
  11. public class MainActivity extends ActionBarActivity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16.  
  17. TextView text = new TextView(this);
  18. text.setText("Bonjour, vous me devez 1 000 000€.");
  19. setContentView(text);
  20.  
  21. EditText aideDeTexte = new EditText(this);
  22. aideDeTexte.setHint("smsjoismoi");
  23. aideDeTexte.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
  24. aideDeTexte.setLines(5);
  25.  
  26. Button bouton = new Button(this);
  27. bouton.setText("foobar");
  28. }
  29. String aideDeTexte3 = "jmeois";
  30. }
  31. */
  32.  
  33. package com.kaanaxinc.portalstudio;
  34.  
  35. import android.app.Activity;
  36. import android.bluetooth.BluetoothAdapter;
  37. import android.bluetooth.BluetoothDevice;
  38. import android.bluetooth.BluetoothSocket;
  39. import android.content.Intent;
  40. import android.os.Bundle;
  41. import android.os.Handler;
  42. import android.os.ParcelUuid;
  43. import android.text.Editable;
  44. import android.text.TextWatcher;
  45. import android.util.Log;
  46. import android.view.KeyEvent;
  47. import android.view.MotionEvent;
  48. import android.view.View;
  49. import android.view.View.OnClickListener;
  50. import android.view.View.OnKeyListener;
  51. import android.widget.ArrayAdapter;
  52. import android.widget.Button;
  53. import android.widget.CheckBox;
  54. import android.widget.EditText;
  55. import android.widget.ListView;
  56. import android.widget.RadioGroup;
  57. import android.widget.TextView;
  58. import android.widget.Toast;
  59.  
  60. import java.io.IOException;
  61. import java.io.InputStream;
  62. import java.io.OutputStream;
  63. import java.text.NumberFormat;
  64. import java.util.ArrayList;
  65. import java.util.Arrays;
  66. import java.util.List;
  67. import java.util.Set;
  68.  
  69. public class MainActivity extends Activity {
  70. // La chaîne de caractères par défaut
  71. private final String defaut = "Vous devez cliquer sur le bouton « Calculer l'IMC » pour obtenir un résultat.";
  72. // La chaîne de caractères de la megafonction
  73. 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) !";
  74.  
  75. Button envoyer = null;
  76. Button raz = null;
  77.  
  78. EditText poids = null;
  79. EditText taille = null;
  80.  
  81. RadioGroup group = null;
  82.  
  83. TextView result = null;
  84.  
  85. CheckBox mega = null;
  86.  
  87. String texteRecu = "";
  88.  
  89.  
  90.  
  91. private OutputStream outputStream;
  92. private InputStream inStream;
  93. //private ConnectedThread connectedThread = new ConnectedThread("test");
  94.  
  95. private void init() throws IOException {
  96. BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
  97.  
  98.  
  99. if (blueAdapter != null) {
  100. if (blueAdapter.isEnabled()) {
  101. Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();
  102.  
  103. if(bondedDevices.size() > 0){
  104. String listview3 = "";
  105. String listview2 = "";
  106. BluetoothDevice bt2 = null;
  107. ArrayList<String> listview =
  108. new ArrayList<String>();//(Arrays.asList(bondedDevices.toString()));
  109. for(BluetoothDevice bt : bondedDevices) {
  110. listview3 += (bt.getName());
  111. listview3 += " - ";
  112. listview2 += (bt);
  113. bt2 = bt;
  114. }
  115. listview2 = listview.toString();
  116. result.setText(listview3);
  117.  
  118.  
  119. //BluetoothDevice[] devices = (BluetoothDevice[]) bondedDevices.toArray();
  120. BluetoothDevice device = bt2;
  121. ParcelUuid[] uuids = device.getUuids();
  122. BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
  123. socket.connect();
  124. outputStream = socket.getOutputStream();
  125. inStream = socket.getInputStream();
  126. }
  127.  
  128. Log.e("error", "No appropriate paired devices.");
  129. }else{
  130. Log.e("error", "Bluetooth is disabled.");
  131. }
  132. }
  133. }
  134.  
  135. public void write(String s) throws IOException {
  136. outputStream.write(s.getBytes());
  137. }
  138. /*private class ConnectedThread extends Thread {
  139. public ConnectedThread(String str) {
  140. super(str);
  141. }*/
  142. public void run() {
  143. /*final int BUFFER_SIZE = 1024;
  144. byte[] buffer = new byte[BUFFER_SIZE];
  145. int bytes = 0;
  146. int b = BUFFER_SIZE;
  147. long startTime = System.currentTimeMillis();
  148. try {
  149. bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
  150. //String bytes2 = String.valueOf(bytes);
  151. //result.setText(bytes2);
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }*/
  155. byte[] buffer = new byte[1024]; // buffer store for the stream
  156. int bytes; // bytes returned from read()
  157.  
  158. // Keep listening to the InputStream until an exception occurs
  159. while (true) {
  160. try {
  161. // Read from the InputStream
  162. bytes = inStream.read(buffer);
  163. String readMessage = new String(buffer, 0, bytes);
  164. // Send the obtained bytes to the UI activity
  165. result.setText(readMessage);
  166. if (readMessage == "OK 1")
  167. break;
  168. } catch (IOException e) {
  169. break;
  170. }
  171. }
  172.  
  173. }/*
  174. }*/
  175.  
  176.  
  177. void beginListenForData()
  178. {
  179. final Handler handler = new Handler();
  180. final byte delimiter = 10; //This is the ASCII code for a newline character
  181.  
  182. final boolean stopWorker = false;
  183. final int readBufferPosition = 0;
  184. final byte[]readBuffer = new byte[1024];
  185. Thread workerThread = new Thread(new Runnable()
  186. {
  187. public void run()
  188. {
  189. while(!Thread.currentThread().isInterrupted() && !stopWorker)
  190. {
  191.  
  192. try
  193. {
  194. int bytesAvailable = inStream.available();
  195. int readBufferPosition2 = readBufferPosition;
  196. if(bytesAvailable > 0)
  197.  
  198. {
  199. byte[] packetBytes = new byte[bytesAvailable];
  200. inStream.read(packetBytes);
  201. for(int i=0;i<bytesAvailable;i++)
  202. {
  203. byte b = packetBytes[i];
  204. if(b == delimiter)
  205. {
  206. byte[] encodedBytes = new byte[readBufferPosition2];
  207. System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
  208. final String data = new String(encodedBytes);
  209. readBufferPosition2 = 0;
  210.  
  211. handler.post(new Runnable()
  212. {
  213. public void run()
  214. {
  215. result.setText(data);
  216. }
  217. });
  218. }
  219. else
  220. {
  221. readBuffer[readBufferPosition2++] = b;
  222. }
  223. }
  224. }
  225. }
  226. catch (IOException ex)
  227. {
  228. }
  229. }
  230. }
  231. });
  232.  
  233. workerThread.start();
  234. }
  235.  
  236. @Override
  237. public void onCreate(Bundle savedInstanceState) {
  238. super.onCreate(savedInstanceState);
  239. setContentView(R.layout.activity_main);
  240. // On récupère toutes les vues dont on a besoin
  241. envoyer = (Button)findViewById(R.id.calcul);
  242.  
  243. raz = (Button)findViewById(R.id.raz);
  244.  
  245. taille = (EditText)findViewById(R.id.taille);
  246. poids = (EditText)findViewById(R.id.poids);
  247.  
  248. mega = (CheckBox)findViewById(R.id.mega);
  249.  
  250. group = (RadioGroup)findViewById(R.id.group);
  251. result = (TextView)findViewById(R.id.result);
  252.  
  253. // On attribue un listener adapté aux vues qui en ont besoin
  254. envoyer.setOnClickListener(envoyerListener);
  255. raz.setOnClickListener(razListener);
  256. taille.addTextChangedListener(textWatcher);
  257. poids.addTextChangedListener(textWatcher);
  258. // Solution avec des onKey
  259. //taille.setOnKeyListener(modificationListener);
  260. //poids.setOnKeyListener(modificationListener);
  261. mega.setOnClickListener(checkedListener);
  262.  
  263. }
  264.  
  265. /*@Override
  266. public void onResume()
  267. {
  268. super.onResume();
  269. //It is best to check BT status at onResume in case something has changed while app was paused etc
  270. checkBTState();
  271. try {
  272. btSocket = createBluetoothSocket(device);
  273. } catch (IOException e) {
  274. Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show();
  275. }
  276. // Establish the Bluetooth socket connection.
  277. try
  278. {
  279. btSocket.connect();
  280. } catch (IOException e) {
  281. try
  282. {
  283. btSocket.close();
  284. } catch (IOException e2)
  285. {
  286. //insert code to deal with this
  287. }
  288. }
  289. mConnectedThread = new ConnectedThread(btSocket);
  290. mConnectedThread.start();
  291. }*/
  292.  
  293. /*private class ConnectedThread extends Thread {
  294. private final InputStream mmInStream;
  295. private final OutputStream mmOutStream;
  296.  
  297. //creation of the connect thread
  298. public ConnectedThread(BluetoothSocket socket) {
  299. InputStream tmpIn = null;
  300. OutputStream tmpOut = null;
  301.  
  302. try {
  303. //Create I/O streams for connection
  304. tmpIn = socket.getInputStream();
  305. tmpOut = socket.getOutputStream();
  306. } catch (IOException e) { }
  307.  
  308. mmInStream = tmpIn;
  309. mmOutStream = tmpOut;
  310. }
  311.  
  312. public void run() {
  313. byte[] buffer = new byte[256];
  314. int bytes;
  315.  
  316. // Keep looping to listen for received messages
  317. while (true) {
  318. try {
  319. bytes = mmInStream.read(buffer); //read bytes from input buffer
  320. String readMessage = new String(buffer, 0, bytes);
  321. // Send the obtained bytes to the UI Activity via handler
  322. bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
  323. } catch (IOException e) {
  324. break;
  325. }
  326. }
  327. }
  328. //write method
  329. public void write(String input) {
  330. byte[] msgBuffer = input.getBytes(); //converts entered String into bytes
  331. try {
  332. mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream
  333. } catch (IOException e) {
  334. //if you cannot write, close the application
  335. Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
  336. finish();
  337.  
  338. }
  339. }
  340. }*/
  341.  
  342. /*private void checkBTState()
  343. {
  344. // Check device has Bluetooth and that it is turned on
  345. mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); // CHECK THIS OUT THAT IT WORKS!!!
  346. if(mBluetoothAdapter==null) {
  347. Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
  348. finish();
  349. } else {
  350. if (!mBluetoothAdapter.isEnabled()) {
  351. //Prompt user to turn on Bluetooth
  352. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  353. startActivityForResult(enableBtIntent, 1);
  354. }
  355. }
  356. }*/
  357.  
  358. /*
  359. // Se lance à chaque fois qu'on appuie sur une touche en étant sur un EditText
  360. private OnKeyListener modificationListener = new OnKeyListener() {
  361. @Override
  362. public boolean onKey(View v, int keyCode, KeyEvent event) {
  363. // On remet le texte à sa valeur par défaut pour ne pas avoir de résultat incohérent
  364. result.setText(defaut);
  365. return false;
  366. }
  367. }*/
  368.  
  369. private TextWatcher textWatcher = new TextWatcher() {
  370.  
  371. @Override
  372. public void onTextChanged(CharSequence s, int start, int before, int count) {
  373. result.setText(defaut);
  374. }
  375.  
  376. @Override
  377. public void beforeTextChanged(CharSequence s, int start, int count,
  378. int after) {
  379.  
  380. }
  381.  
  382. @Override
  383. public void afterTextChanged(Editable s) {
  384.  
  385. }
  386. };
  387.  
  388. // Uniquement pour le bouton "envoyer"
  389. private OnClickListener envoyerListener = new OnClickListener() {
  390. @Override
  391. public void onClick(View v) {
  392. /*if(!mega.isChecked()) {
  393. // Si la megafonction n'est pas activée
  394. // On récupère la taille
  395. String t = taille.getText().toString();
  396. // On récupère le poids
  397. String p = poids.getText().toString();
  398.  
  399. // Puis on vérifie que la taille est cohérente
  400. if (t.length() == 0 || p.length() == 0) {
  401. result.setText("Veuillez entrer votre poids et votre taille.");
  402. } else {
  403. float tValue = Float.valueOf(t);
  404. if(tValue == 0)
  405. Toast.makeText(MainActivity.this, "Hého, tu es un Minipouce ou quoi ?", Toast.LENGTH_SHORT).show();
  406. else {
  407. float pValue = Float.valueOf(p);
  408. // Si l'utilisateur a indiqué que la taille était en centimètres
  409. // On vérifie que la Checkbox sélectionnée est la deuxième à l'aide de son identifiant
  410. if(group.getCheckedRadioButtonId() == R.id.radio2)
  411. tValue = tValue / 100;
  412.  
  413. tValue = (float)Math.pow(tValue, 2);
  414. float imc = pValue / tValue;
  415. result.setText("Votre IMC est " + String.valueOf(imc));
  416. }
  417. }
  418. } else
  419. result.setText(megaString);
  420. */
  421. try {
  422. init();
  423. } catch (IOException e2) {
  424.  
  425. }
  426. }
  427. };
  428.  
  429. // Listener du bouton de remise à zéro
  430. private OnClickListener razListener = new OnClickListener() {
  431. @Override
  432. public void onClick(View v) {
  433. /*poids.getText().clear();
  434. taille.getText().clear();
  435. result.setText(defaut);
  436. */
  437. String aEnvoyer = poids.getText().toString();
  438.  
  439. try {
  440. write(aEnvoyer);
  441. } catch (IOException e1) {
  442.  
  443. }
  444. texteRecu = "***";
  445. result.setText(texteRecu);
  446. beginListenForData();
  447. }
  448. };
  449.  
  450. // Listener du bouton de la megafonction.
  451. private OnClickListener checkedListener = new OnClickListener() {
  452. @Override
  453. public void onClick(View v) {
  454. // On remet le texte par défaut si c'était le texte de la megafonction qui était écrit
  455. if(!((CheckBox)v).isChecked() && result.getText().equals(megaString))
  456. result.setText(defaut);
  457. }
  458. };
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement