Android- Bluetooth: Bluetooth Service behaving unexpectedly -


i writing android app can interact arduino. app have 2 buttons 1. connect 2. dis-connect, start , stop bluetooth service. have test sketch on arduino when receives "1" send "404"(just test!) phone.

here's bluetooth service class

    public class bluetoothservice extends service {         private bluetoothmanager bluetoothmanager;         private servicehandler mshandler;          public bluetoothservice(){}          private final class servicehandler extends handler {             public servicehandler(looper looper) {                 super(looper);             }              @override             public void handlemessage(message msg) {             }         }          @override         public void oncreate() {             super.oncreate();             handlerthread thread = new handlerthread("servicestartarguments", android.os.process.thread_priority_background);             thread.start();             looper msloop = thread.getlooper();             mshandler = new servicehandler(msloop);         }          @override         public int onstartcommand(intent intent, int flags, int startid) {             message msg = mshandler.obtainmessage();             msg.arg1 = startid;             mshandler.sendmessage(msg);             bluetoothmanager=new bluetoothmanager();             bluetoothmanager.writedata("1", getapplicationcontext());  //sending "1" arduino             string str= bluetoothmanager.readdata(getapplicationcontext());  //reading "404" arduino             toast.maketext(getapplicationcontext(), str, toast.length_long).show();             return start_sticky;         }           @override         public void ondestroy(){             bluetoothmanager.turnbluetoothoff();         }          @override         public ibinder onbind(intent intent) {         return null;         }     } 

now here's bluetoothmanager class:

public class bluetoothmanager {     private bluetoothadapter bluetoothadapter;     private bluetoothdevice bluetoothdevice;     private bluetoothsocket bluetoothsocket;     private connectedthread connectedthread;     private byte[] buffer;      public bluetoothmanager(){         buffer=new byte[256];         bluetoothsocket=null;         bluetoothadapter=null;         bluetoothdevice=null;         connectedthread=null;         getbluetoothadapter();         if(!isbluetoothavailable()){             turnbluetoothon();         }         scantoconnect();     }     public void turnbluetoothoff(){         try {             bluetoothsocket.close();             bluetoothsocket=null;             bluetoothadapter.canceldiscovery();             bluetoothadapter.disable();             bluetoothadapter=null;             bluetoothdevice=null;         }catch(exception e){             e.printstacktrace();         }     }     private boolean isbluetoothavailable(){         return bluetoothadapter.isenabled();     }     private void turnbluetoothon(){         bluetoothadapter.enable();     }     public string readdata(context context){         string outputstring=null;         if(isbluetoothavailable()) {             outputstring = connectedthread.read(buffer);         }else{             toast.maketext(context, "error: not connected", toast.length_long).show();         }         return outputstring;     }     public void writedata(string string, context context){         if(isbluetoothavailable()) {             connectedthread.write(string.getbytes());         }else{             toast.maketext(context, "error: not connected", toast.length_long).show();         }     }     private void getbluetoothadapter(){         try{             bluetoothadapter=bluetoothadapter.getdefaultadapter();         }catch (exception e){             e.printstacktrace();         }     }     private void scantoconnect(){         set<bluetoothdevice> paireddevices=bluetoothadapter.getbondeddevices();         if(paireddevices.size()>0){             try {                 (bluetoothdevice device : paireddevices) {                     if (device.getname().equals("hc-05")) {                         bluetoothdevice = device;                         new connectbt(bluetoothdevice);                         break;                     }                 }             }catch(exception e){                 e.printstacktrace();             }         }     }     private class connectbt extends thread {         public connectbt(bluetoothdevice device) {             bluetoothsocket tmp = null;             bluetoothdevice = device;             uuid uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");             try {                 tmp = device.createrfcommsockettoservicerecord(uuid);             } catch (ioexception e) {                 e.printstacktrace();             }             bluetoothsocket = tmp;             run();         }         public void run() {             bluetoothadapter.canceldiscovery();             try {                 bluetoothsocket.connect();                 connectedthread = new connectedthread(bluetoothsocket);             } catch (ioexception connectexception) {                 closesocket();             }         }         private void closesocket() {             try {                 bluetoothsocket.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }     }     private class connectedthread extends thread{         private inputstream minput=null;         private outputstream moutput=null;         private string strinput;          public connectedthread(bluetoothsocket socket){             bluetoothsocket=socket;             inputstream tmpin=null;             outputstream tmpout=null;             try{                 tmpin=socket.getinputstream();                 tmpout=socket.getoutputstream();             }catch(ioexception e){                 e.printstacktrace();                 closesocket();             }             minput=tmpin;             moutput=tmpout;         }         public void write(byte[] bytes){             try{                 moutput.write(bytes);             }catch(ioexception e){                 e.printstacktrace();             }         }         public string read(byte[] bytes){             try {                 minput.read(bytes);                 strinput = new string(bytes);             }catch(exception e){                 e.printstacktrace();             }             return strinput;         }         public void closesocket(){             try{                 bluetoothsocket.close();             }catch(ioexception e){                 e.printstacktrace();             }         }     } } 

my problem have press connect button twice connect arduino, first press enable bluetooth , second press connect arduino, send , receive data. not intended, enabling bluetooth , connecting should have taken place single press. why behaving this?

n.b: newbie java , android.

after enabling bluetooth take sometime paired device list. in case reading paired devices after turning on bluetooth. may delay connection part.

use handler methods delay execution.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -