Bluetooth_smartcaractivity.java file
PACKAGE CN. bluetooth_smartcar;import java.io.ioexception;import java.io.outputstream;import java.io.printwriter;import java.lang.reflect.invocationtargetexception;import java.lang.reflect.method;import java.util.arrays;import java.util.uuid;import android.app.activity;import android.bluetooth.bluetoothadapter;import android.bluetooth.bluetoothdevice;import android.bluetooth.bluetoothsocket;import android.content.context;import android.content.intent;import android.os.bundle;import android.os.vibrator;import android.util.log;import android.view.menu;import android.view.menuitem;import android.view.motionevent;import android.view.view;import Android.view.view.ontouchlistener;import android.widget.button;import android.widget.seekbar;import android.widget.textview;import android.widget.toast;public class bluetooth_ Smartcaractivity extends acTIVITY {STRING INFORMATION;PUBLIC BYTE[] MESSAGE = NEW BYTE[1];p rivate vibrator vibrator;//Vibration Setting private bluetoothadapter btadapter = Bluetoothadapter.getdefaultadapter (); bluetoothdevice btdevice; bluetoothsocket bluetoothsocket = null;outputstream tmpout = null;private Static final uuid my_uuid = uuid.fromstring ("37849990-8aad-4e9f-88d5-8e417cbf02c3"); private string address = "00:12:10:31:04:08"; button buttonup; //Direction Key button buttondown; button buttonlefttop; button buttonrighttop; button buttonleftdown; button buttonrightdown; textview textviewdevicename; textview textviewsuccessed; textview textviewprogress; seekbar seekbar;int i = 70; //Initial Speed Value /* * called when the activity is first created. */ @Override public Void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview ( R.layout.main); buttonup= (Button) Findviewbyid (r.id.buttonup); buttondown= (button) Findviewbyid (R.id.buttondown); buttonrighttop= (Button) Findviewbyid (R.id.buttonrighttop); buttonlefttop= (Button) Findviewbyid (R.id.buttonlefttop); buttonleftdown= (Button) Findviewbyid (R.id.buttonleftdown); buttonrightdown= (Button) Findviewbyid (R.id.buttonrightdown); &nbSp; textviewdevicename= (TextView) Findviewbyid (r.id.textviewdevicename); textviewsuccessed= (TextView) Findviewbyid (r.id.textviewsuccessed); textviewprogress= (TextView) Findviewbyid (R.id.textViewProgress) ; seekbar= (SeekBar) Findviewbyid (R.id.seekbar); Buttonup.setontouchlistener (New buttonontouchlistener ()); Buttondown.setontouchlistener (New buttonontouchlistener ()); buttonlefttop.setontouchlistener (New buttonontouchlistener ()); buttonrighttop.setontouchlistener (New buttonontouchlistener ()); buttonleftdown.setontouchlistener (nEw buttonontouchlistener ()); Buttonrightdown.setontouchlistener (New buttonontouchlistener ()); seekbar.setonseekbarchangelistener (New seekbarlistener ()); //Detecting Bluetooth Adapter if (btadapter != null) { SYSTEM.OUT.PRINTLN ("Bluetooth device driver normal"); if (!btadapter.isenabled ()) { intent intent = new intent ( bluetoothadapter.action_request_enable); startactivity (intent);         SYSTEM.OUT.PRINTLN ("Adapter not available"); } else system.out.println ("adapter available"); } else system.out.println ("Bluetooth device driver exception "); Textviewprogress.settext ("Current speed:" +i); seekbar.setprogress (70); } /****** Send Information Method *********/ public void bluesend (byte[] msg) { try{ tmpout = bluetoothsocket.getoutputstream ();     SYSTEM.OUT.PRINTLN ("Generate output stream");     LOG.D ("Send", arrays.tostring (msg))     SYSTEM.OUT.PRINTLN ("The output stream is being sent to the buffer"); tmpout.write (msg);    SYSTEM.OUT.PRINTLN ("Successful output stream to buffer"); }catch (ioexception e) { &Nbsp; e.printstacktrace (); System.out.println ("Unable to generate output stream or send output stream to buffer"); } } /****** Vibration 10ms Method ******/ public void vibrator () { vibrator = (Vibrator) Getsystemservice (Context.vibrator_service); vibrator.vibrate (    SYSTEM.OUT.PRINTLN) ("Vibration 20ms"); } /*****SeekBar********/ private class seekbarlistener implements seekbar.onseekbarchangelistener{ @Override// public void onprogresschanged when the progress bar is changed (seekbar Seekbar, int progress, boolean fromuser) { / / todo auto-geneRated method stub i = seekbar.getprogress (); textviewprogress.settext ("Current speed:" +i); } @Override//Start dragging the slider public void Onstarttrackingtouch (Seekbar seekbar) { // todo auto-generated method stub i= seekbar.getprogress (); textviewprogress.settext ("Current speed:" +i); } @Override//End public void onstoptrackingtouch when dragging the slider (SeekBar seekbar) { // TODO Auto-generated method stub i= seekbar.getprogress (); textviewprogress.settext ("Current speed:" +i); int k; &nbSp; k = i/10; if (k==10) k=9; // Convert an integral type to a string string j = string.valueof (k); byte[] msgbuffer = j.getbytes (); bluesend (Msgbuffer); } } /******** Button Listener **************/ class buttonontouchlistener implements ontouchlistener{@Overridepublic boolean ontouch (view v, motionevent Event) {// todo auto-generated method stubint action = event.getaction (); action = event.getaction (); switch (action) {Case motionevent.action_down:switch (V.getid ()) { case r.id.buttonup: message[0] = (byte) 0x41;//a ASCII code 65;vibrator ();//Vibration 10msbluesend ( message); System.out.println ("FrontEnter ");break;case r.id.buttondown:message[0] = (byte) 0x42;//b ASCII code 66;vibrator ();//Vibration 10msbluesend (message); System.out.println ("Back");break; case r.id.buttonlefttop: message[0] = (byte) 0x43;//s ASCII code 69;vibrator ();//Vibration 10msbluesend (message); SYSTEM.OUT.PRINTLN ("upper left");break; case r.id.buttonrighttop: message[0] = (byte) 0x44;//f ASCII code 70;vibrator ();//Vibration 10msbluesend (message); System.out.println ("top right");break; case r.id.buttonleftdown: message[0] = (byte) 0x45;//g ASCII code 71;vibrator ();//Vibration 10msbluesend (message); System.out.println ("bottom left");break; case r.id.buttonrightdown: message[0] = (byte) 0x46;//h ASCII code 72;vibrator ();//Vibration 10msbluesend (message); System.out.println ("bottom Right"); Break; }break;case motionevent.action_up:message[0] = (byte) 0x61;//a ASCII code; vibrator ();//Vibration 10msbluesend (message); SYSTEM.OUT.PRINTLN ("Let go Stop"); break; }return false;} } /********* Menu * / @Override public boolean Oncreateoptionsmenu (Menu menu) { // todo auto-generated method stub menu.add (0, 0, 0, "exit"); menu.add (0, 1, 1, "remote"), menu.add (0, 2, 2, "track"); menu.add (0, 3, 3, "infrared"); menu.add (0, 4, 4, "Gold Scout"); menu.add (0, 5, 5, "Temperature Measurement"); menu.add (0, 6, 6, "Humidity Measurement"); menu.add (0, 7, 7, "velocimetry"); menU.add (0, 8, 8, "obstacle avoidance"), return super.oncreateoptionsmenu (menu); } @Override public boolean Onoptionsitemselected (Menuitem item) { // TODO Auto-generated method stub byte[] msgbufferway = new byte[1] ; if (Item.getitemid () ==0) { finish (); } if (Item.getitemid () ==1) { msgbufferway[0] = (byte) 0x62;// ASCII code of B; bluesend (msgbufferway); } if (Item.getitemid () ==2) { msgbufferway[0] = (byte) 0x63 ;//c ASCII code; bluesend (msgbufferway); } if (item.getitEmId () ==3) { msgbufferway[0] = (byte) 0x64;//d ASCII code;; bluesend (Msgbufferway); } if (Item.getitemid () ==4) { msgbufferway[0] = (byte) 0x65;//e ASCII code;; bluesend (Msgbufferway); } if (Item.getitemid () ==5) { msgbufferway[0] = (byte) 0x66;//f ASCII code;; bluesend (Msgbufferway); } if (Item.getitemid () ==6) { msgbufferway[0] = (byte) 0x67;//g ASCII code;; bluesend (Msgbufferway); } if (Item.getitemid () ==7) { msgbufferway[0] = (byte) 0x68;//h ASCII code;; &nBsp; bluesend (Msgbufferway); } if (Item.getItemId () ==8) { msgBufferWay[0] = (byte) 0x69;//i ASCII code;; bluesend (Msgbufferway); } return super.onoptionsitemselected (item); } /******onresume Stage ***********/ @Override protected void onresume () { // todo auto-generated method stub super.onresume (); btdevice = btadapter.getremotedevice (Address); System.out.println ("Get to remote Bluetooth device based on address"); if (Btdevice!=null) { Textviewdevicename.settext ("The device you are connecting to is:" +btdevice.getname ()); } try {//bluetoothsocket = btdevice.createrfcommsockettoservicerecord (MY_ UUID); /******* bluetooth connection ***********/ method m; try { m = btdevice.getclass (). GetMethod (" Createrfcommsocket ", new class[] {int.class}); bluetoothsocket = (Bluetoothsocket) m.invoke (btdevice, integer.valueof (1)); } catch (SECURITYEXCEPTION E1) { // todo auto-generated catch block &Nbsp; e1.printstacktrace (); } catch (NOSUCHMETHODEXCEPTION E1) { // TODO Auto-generated catch block e1.printstacktrace (); } catch (illegalargumentexception e) { // TODO auto-generated catch block e.printstacktrace (); } catch (illegalaccessexception e) { // TODO Auto-generated catch block E.printstacktrace (); } catch (invocationtargetexception e) { // todo auto-generated catch block &nbsP; e.printstacktrace (); } system.out.println (" Obtain the remote Bluetooth device client according to the UUID "); Bluetoothsocket.connect (); System.out.println ("Connected to remote Device"); Textviewsuccessed.settext ("Device Connection: Success");} catch (ioexception e) {// TODO Auto-generated catch Blocktextviewsuccessed.settext ("Device connection: Unsuccessful"); E.printstacktrace ();} } /******** Destruction Phase / @Override protected void ondestroy () { // TODO Auto-generated method stub Super.ondestroy (); try {bluetoothsocket.close (); SYSTEM.OUT.PRINTLN ("Shutting down the Client");} catch (ioexception e) {// todo auto-generated catch bLocke.printstacktrace ();} } }
Bluetooth car phone remote control software