Bonjour, j'ai un équipement Arduino qui m'envoie des données chaque seconde par bluetooth. J'aimerais recupérer ces données en temps réel sur
mon téléphone Android.Pour simuler cela , j'ai créé une application android qui m'envoie les données en temps réel et une autre sur le téléphone qui récupérer ces données en temps réel.J'ai un probleme avec l'application qui m'envoit les données, voici le code ecrit :
public class Test extends Activity { public static final String CARDIO_UUID = "00001101-0000-1000-8000-00805F9B34FB"; private UUID cardioUuid = UUID.fromString(CARDIO_UUID); public final String RET = "\n"; private BluetoothSocket clientSocket = null; CardioWriteThread writeOnSocket; CardioConnectThread connectThread; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); Log.d("DEBUG","ICI1"); connectThread = new CardioConnectThread(); connectThread.start(); } class CardioConnectThread extends Thread { public CardioConnectThread() { BluetoothSocket temp = null; try { Thread.sleep(2000); Log.d("DEBUG","ICI2"); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if(adapter != null){ if(adapter.isEnabled()) { Set<BluetoothDevice> visibleDevices = adapter.getBondedDevices(); if(visibleDevices.size() > 0) { BluetoothDevice[] devices = (BluetoothDevice[]) visibleDevices.toArray(); BluetoothDevice device = devices[0]; //ParcelUuid[] uuids = device.getUuids(); Log.d("DEBUG","ICI3"); temp = device.createRfcommSocketToServiceRecord(cardioUuid/*uuids[0].getUuid()*/); } } } else { Log.d("DEBUG","ICI3"); Log.d("Error", "Bluetooth adaptater not found !!"); } } catch(IOException e) { Log.e("Error Socket","Socket listen() failed!"); } catch(InterruptedException e){} clientSocket = temp; } @Override public void run() { while(true) { try { clientSocket.connect(); } catch(IOException e) { Log.e("Error","couldn't serverSocket.accept() !"); break; } // lorsque la connection etait accepte if(clientSocket != null) { Log.d("DEBUG","ICI4"); writeOnSocket = new CardioWriteThread(); writeOnSocket.start(); } } } public void cancel() { try { clientSocket.close(); } catch(IOException e){} } } class CardioWriteThread extends Thread { private final OutputStream outStream; private boolean continueWriting = true; public CardioWriteThread() { OutputStream tmp = null; try { tmp = clientSocket.getOutputStream(); } catch(IOException e){} outStream = tmp; } public void write(String s,OutputStream outStream) throws IOException { outStream.write(s.getBytes()); } private int getRandom() { double high = 3; double low = 0.5; double value = Math.random() * (high - low) + low; return (int)Math.round(value); } @Override public void run() { while(continueWriting) { try { write(Integer.toHexString(getRandom())+RET+RET,outStream); Log.d("DEBUG","ICI5"); try { Thread.sleep(1000); } catch (InterruptedException e) { Log.e("Error","Interrupted thread !!"); } } catch(NullPointerException e){ Log.e("Error","can't find Bluetooth Adapter !!"); } catch(IOException e){ Log.e("Error","Input-Output Errors"); } } } public void cancel() { try { outStream.close(); } catch(IOException e){ } catch(NullPointerException e){} continueWriting = false; } } @Override public void onDestroy() { try { connectThread.cancel(); writeOnSocket.cancel(); }catch(Exception e) { Log.e("Canceling","Can't cancel connection !!"); } super.onDestroy(); } }
Afficher la suite