java - Android - Arduino Bluetooth communication -


hi im new android studio , java , hoping read data arduino android.

the plan have button widget on interface send signal arduino , have flex sensor send info android via bluetooth.

the bluetooth module have hc-06 , code designed phone sends '*' arduino , sends random value android random value shown on textbox on interface. change meant happen on java ofcourse im not sure how it. please help.

this arduino code first

#include <softwareserial.h> const int rx_pin = 2; const int tx_pin = 3; softwareserial serial(rx_pin, tx_pin); char commandchar;  void setup () { serial.begin (9600); andomseed(analogread(0)) }  void loop () {  if(serial.available())  {   commandchar = serial.read();  switch(commandchar)   {       case '*':       serial.print(random(1000) + "#");       break;      }   } } 

and here code done on android studio:

package com.example.ft.myapplication;  import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothsocket; import android.content.intent; import android.os.handler; import android.os.message; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast;  import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.set; import java.util.uuid;   public class mainactivity extends appcompatactivity {  bluetoothadapter mbluetoothadapter;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mbluetoothadapter = bluetoothadapter.getdefaultadapter();     if (mbluetoothadapter == null) { // device not support bluetooth     }     if (!mbluetoothadapter.isenabled()) {         intent enablebtintent = new                    intent(bluetoothadapter.action_request_enable);         startactivityforresult(enablebtintent, 1);     }     set<bluetoothdevice> paireddevices =   mbluetoothadapter.getbondeddevices();     bluetoothdevice mdevice = null;     if (paireddevices.size() > 0) {         (bluetoothdevice device : paireddevices) {             mdevice = device;         }     }     connectthread mconnectthread = new connectthread(mdevice);     mconnectthread.start();   }  handler mhandler = new handler() {     @override     public void handlemessage(message msg) {         byte[] writebuf = (byte[]) msg.obj;         int begin = (int)msg.arg1;         int end = (int)msg.arg2;          switch(msg.what) {            case 1:                 string writemessage = new string(writebuf);                 writemessage = writemessage.substring(begin, end);                 break;         }     } };  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  private class connectthread extends thread {     private final bluetoothsocket mmsocket;     private final bluetoothdevice mmdevice;     private final uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");     public connectthread(bluetoothdevice device) {         bluetoothsocket tmp = null;         mmdevice = device;         try {             tmp = device.createrfcommsockettoservicerecord(my_uuid);         } catch (ioexception e) { }         mmsocket = tmp;     }     public void run() {         mbluetoothadapter.canceldiscovery();         try {             mmsocket.connect();         } catch (ioexception connectexception) {             try {                 mmsocket.close();             } catch (ioexception closeexception) { }             return;         }         connectedthread mconnectedthread = new connectedthread(mmsocket);         mconnectedthread.start();      }     public void cancel() {         try {             mmsocket.close();         } catch (ioexception e) { }     } } private class connectedthread extends thread {     private final bluetoothsocket mmsocket;     private final inputstream mminstream;     private final outputstream mmoutstream;     public connectedthread(bluetoothsocket socket) {         mmsocket = socket;         inputstream tmpin = null;         outputstream tmpout = null;         try {             tmpin = socket.getinputstream();             tmpout = socket.getoutputstream();         } catch (ioexception e) { }         mminstream = tmpin;         mmoutstream = tmpout;     }     public void run() {         string s = "*";         write(s.getbytes());          byte[] buffer = new byte[1024];         int begin = 0;         int bytes = 0;         while (true) {             try {                 bytes += mminstream.read(buffer, bytes, buffer.length - bytes);                 for(int = begin; < bytes; i++) {                     if(buffer[i] == "#".getbytes()[0]) {                         mhandler.obtainmessage(1, begin, i, buffer).sendtotarget();                         begin = + 1;                         if(i == bytes - 1) {                             bytes = 0;                             begin = 0;                         }                     }                 }             } catch (ioexception e) {                 break;             }         }     }     public void write(byte[] bytes) {         try {             mmoutstream.write(bytes);         } catch (ioexception e) { }     }     public void cancel() {         try {             mmsocket.close();         } catch (ioexception e) { }     } } 

}


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -