Home>

Very good Bluetooth communication demo to implement sending and receiving functions,This was achieved using two classes,The specific content is as follows

There are two main types of ideas.Main interface classandBluetooth chat service class. First create the thread is actuallyCreate bluetoothchatservice ()(Bluetooth chat service class) Pass the handler at this time so that you can operate the ui interface.Keep polling to read Bluetooth messages, when the Send button is clicked on the home screenCall bluetoothchatservicesend method write method, here the write method uses the handler to send the message,Displayed on the main interface,Another client keeps reading Bluetooth messages. A similar read method is also displayed on the interface.This completes the communication.

import java.util.arraylist;
import java.util.set;
import android.app.activity;
import android.app.alertdialog;
import android.bluetooth.bluetoothadapter;
import android.bluetooth.bluetoothdevice;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.dialoginterface;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.util.log;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.window;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;
public class bluetoothchat extends activity {
  //message types sent from the bluetoothchatservice handler
  public static final int message_state_change=1;
  public static final int message_read=2;
  public static final int message_write=3;
  public static final int message_device_name=4;
  public static final int message_toast=5;
  //key names received from the bluetoothchatservice handler
  public static final string device_name="device_name";
  public static final string toast="toast";
  //intent request codes
  private static final int request_connect_device=1;
  private static final int request_enable_bt=2;
  private textview mtitle;
  private edittext text_chat;
  private edittext text_input;
  private button but_on_off;
  private button but_search;//------>searchable in menu
  private button but_create;//------>set "discoverable" in the menu
  private button msendbutton;
  //the name of the connected Bluetooth device
  private string mconnecteddevicename;
  //string buffer for outgoing messages
  private stringbuffer moutstringbuffer;
  //local bluetooth adapter
  private bluetoothadapter mbluetoothadapter=null;
  //member object for the chat services
  private bluetoothchatservice mchatservice=null;
  private arraylist<string>mpaireddeviceslist=new arraylist<string>();
  private arraylist<string>mnewdeviceslist=new arraylist<string>();
  private string [] strname;
  private string address;
  @override
  public void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    requestwindowfeature (window.feature_custom_title);
    setcontentview (r.layout.main);
    mtitle=(textview) this.findviewbyid (r.id.text_title);
    text_chat=(edittext) this.findviewbyid (r.id.text_chat);
    text_input=(edittext) this.findviewbyid (r.id.text_input);
    but_on_off=(button) this.findviewbyid (r.id.but_off_on);
    but_search=(button) this.findviewbyid (r.id.but_search_div);
    but_create=(button) this.findviewbyid (r.id.but_cjlj);
    msendbutton=(button) this.findviewbyid (r.id.but_fsxx);
    //Get the local Bluetooth adapter
    mbluetoothadapter=bluetoothadapter.getdefaultadapter ();
    //If null, no Bluetooth device
    if (mbluetoothadapter == null) {
      toast.maketext (this, "No Bluetooth device", toast.length_long) .show ();
      finish ();
      return;
    }
    if (mbluetoothadapter.isenabled ()) {
      but_on_off.settext ("Turn off Bluetooth");
    } else {
      but_on_off.settext ("Turn on Bluetooth");
    }
    but_on_off.setonclicklistener (new onclicklistener () {
      @override
      public void onclick (view v) {
        if (! mbluetoothadapter.isenabled ()) {
          mbluetoothadapter.enable ();
          toast.maketext (bluetoothchat.this, "Bluetooth is turned on",              toast.length_short) .show ();
          but_on_off.settext ("Turn off Bluetooth");
        } else {
          mbluetoothadapter.disable ();
          toast.maketext (bluetoothchat.this, "Bluetooth is off",              toast.length_short) .show ();
          but_on_off.settext ("Turn on Bluetooth");
        }
      }
    });
    but_search.setonclicklistener (new onclicklistener () {
      @override
      public void onclick (view v) {
        searchdevice ();
      }
    });
    but_create.setonclicklistener (new onclicklistener () {
      @override
      public void onclick (view v) {
        final edittext et=new edittext (bluetoothchat.this);
        et.setsingleline ();
        et.settext (mbluetoothadapter.getname ());
        new alertdialog.builder (bluetoothchat.this)
            .settitle ("Please enter a room name:")
            .setview (et)
            .setpositivebutton ("OK",                new dialoginterface.onclicklistener () {
                  @override
                  public void onclick (dialoginterface dialog,                      int which) {
                    string name=et.gettext (). tostring ()
                        .trim ();
                    if (name.equals ("")) {
                      toast.maketext (bluetoothchat.this,                          "Please enter a room name",                          toast.length_short) .show ();
                      return;
                    }
                    //Set the room name
                    mbluetoothadapter.setname (name);
                  }
                })
            .setnegativebutton ("Cancel",                new dialoginterface.onclicklistener () {
                  @override
                  public void onclick (dialoginterface dialog,                      int which) {
                  }
                }). create (). show ();
        //create a connection,That is, the device's local Bluetooth device can be found by other users' Bluetooth
        ensurediscoverable ();
      }
    });
    //Get a set collection of already paired Bluetooth devices
    set<bluetoothdevice>paireddevices=mbluetoothadapter
        .getbondeddevices ();
    if (paireddevices.size ()>0) {
      for (bluetoothdevice device:paireddevices) {
        mpaireddeviceslist.add ("Paired:" + device.getname () + "\ n"
            + device.getaddress ());
      }
    } else {
      toast.maketext (this, "No paired device", toast.length_short) .show ();
    }
    //Register broadcast when a new Bluetooth device is found
    intentfilter filter=new intentfilter (bluetoothdevice.action_found);
    this.registerreceiver (mreceiver, filter);
    //Register broadcast when search is complete
    filter=new intentfilter (bluetoothadapter.action_discovery_finished);
    this.registerreceiver (mreceiver, filter);
  }
  @override
  public void onstart () {
    super.onstart ();
    //if bt is not on, request that it be enabled.
    //setupchat () will then be called during onactivityresult
    if (! mbluetoothadapter.isenabled ()) {
      intent enableintent=new intent (
          bluetoothadapter.action_request_enable);
      startactivityforresult (enableintent, request_enable_bt);
      //otherwise, setup the chat session
    } else {
      if (mchatservice == null)
        setupchat ();
    }
  }
  @override
  public synchronized void onresume () {
    super.onresume ();
    //performing this check in onresume () covers the case in which bt was
    //not enabled during onstart (), so we were paused to enable it ...
    //onresume () will be called when action_request_enable activity
    //returns.
    if (mchatservice!=null) {
      //only if the state is state_none, do we know that we haven "t
      //started already
      if (mchatservice.getstate () == bluetoothchatservice.state_none) {
        //start the bluetooth chat services
        mchatservice.start ();
      }
    }
  }
  private void setupchat () {
    msendbutton.setonclicklistener (new onclicklistener () {
      public void onclick (view v) {
        //send a message using content of the edit text widget
        string message=text_input.gettext (). tostring ();
        sendmessage (message);
      }
    });
    //initialize the bluetoothchatservice to perform bluetooth connections
    mchatservice=new bluetoothchatservice (this, mhandler);
    //initialize the buffer for outgoing messages
    moutstringbuffer=new stringbuffer ("");
  }
  @override
  public void ondestroy () {
    super.ondestroy ();
    //stop the bluetooth chat services
    if (mchatservice!=null)
      mchatservice.stop ();
    //make sure we "re not doing discovery anymore
    if (mbluetoothadapter!=null) {
      mbluetoothadapter.canceldiscovery ();
    }
    //unregister broadcast listeners
    this.unregisterreceiver (mreceiver);
  }
  /** Make local Bluetooth devices discoverable * /
  private void ensurediscoverable () {
    if (mbluetoothadapter.getscanmode ()!=bluetoothadapter.scan_mode_connectable_discoverable) {
      intent discoverableintent=new intent (
          bluetoothadapter.action_request_discoverable);
      discoverableintent.putextra (
          bluetoothadapter.extra_discoverable_duration, 300);
      startactivity (discoverableintent);
    }
  }
  /**
   * sends a message.
   *
   * @param message
   * a string of text to send.
   * /
  private void sendmessage (string message) {
    //check that we "re actually connected before trying anything
    if (mchatservice.getstate ()!=bluetoothchatservice.state_connected) {
      toast.maketext (this, "Not connected", toast.length_short) .show ();
      return;
    }
    //check that there "s actually something to send
    if (message.length ()>0) {
      //get the message bytes and tell the bluetoothchatservice to write
      byte [] send=message.getbytes ();
      mchatservice.write (send);
      //reset out string buffer to zero and clear the edit text field
      moutstringbuffer.setlength (0);
      text_input.settext (moutstringbuffer);
    }
  }
  //the handler that gets information back from the bluetoothchatservice
  private final handler mhandler=new handler () {
    @override
    public void handlemessage (message msg) {
      switch (msg.what) {
      case message_state_change:
        switch (msg.arg1) {
        case bluetoothchatservice.state_connected:
          mtitle.settext ("connected");
          mtitle.append (mconnecteddevicename);
          //mconversationarrayadapter.clear ();
          break;
        case bluetoothchatservice.state_connecting:
          mtitle.settext ("Connecting.
.. ");
          break;
        case bluetoothchatservice.state_listen:
        case bluetoothchatservice.state_none:
          mtitle.settext ("Not connected");
          break;
        }
        break;
      case message_write:
        byte [] writebuf=(byte []) msg.obj;
        //construct a string from the buffer
        string writemessage=new string (writebuf);
        //mconversationarrayadapter.add ("me:" + writemessage);
        text_chat.append ("I:" + writemessage + "\ n");
        break;
      case message_read:
        byte [] readbuf=(byte []) msg.obj;
        //construct a string from the valid bytes in the buffer
        string readmessage=new string (readbuf, 0, msg.arg1);
        //mconversationarrayadapter.add (mconnecteddevicename + ":" +
        //readmessage);
        text_chat.append (mconnecteddevicename + ":" + readmessage
            + "\ n");
        break;
      case message_device_name:
        //save the connected device "s name
        mconnecteddevicename=msg.getdata (). getstring (device_name);
        toast.maketext (getapplicationcontext (),            "Connected to" + mconnecteddevicename, toast.length_short)
            .show ();
        break;
      case message_toast:
        toast.maketext (getapplicationcontext (),            msg.getdata (). getstring (toast), toast.length_short)
            .show ();
        break;
      }
    }
  };
  //Connect a Bluetooth device
  private void linkdevice () {
    if (mbluetoothadapter.isdiscovering ()) {
      mbluetoothadapter.canceldiscovery ();
    }
    int cou=mpaireddeviceslist.size () + mnewdeviceslist.size ();
    if (cou == 0) {
      toast.maketext (bluetoothchat.this, "No available Bluetooth devices were found",          toast.length_short) .show ();
      return;
    }
    //Put the names of the paired Bluetooth device and the newly discovered Bluetooth device into the array,To display in the dialog list
    strname=new string [cou];
    for (int i=0;i<mpaireddeviceslist.size ();i ++) {
      strname [i]=mpaireddeviceslist.get (i);
    }
    for (int i=mpaireddeviceslist.size ();i<strname.length;i ++) {
      strname [i]=mnewdeviceslist.get (i-mpaireddeviceslist.size ());
    }
    address=strname [0] .substring (strname [0] .length ()-17);
    new alertdialog.builder (bluetoothchat.this)
        .settitle ("Bluetooth devices found:")
        .setsinglechoiceitems (strname, 0,            new dialoginterface.onclicklistener () {
              @override
              public void onclick (dialoginterface dialog,                  int which) {
                //When the user clicks on the selected Bluetooth device,Remove the mac address of the selected Bluetooth device
                address=strname [which] .split ("\\ n") [1] .trim ();
              }
            })
        .setpositivebutton ("connection", new dialoginterface.onclicklistener () {
          @override
          public void onclick (dialoginterface dialog, int which) {
            if (address == null) {
              toast.maketext (bluetoothchat.this, "Please connect an external Bluetooth device first",                  toast.length_short) .show ();
              return;
            }
            log.i ("sxd", "address:" + address);
            //get the bluetoothdevice object
            bluetoothdevice device=mbluetoothadapter
                .getremotedevice (address);
            //attempt to connect to the device
            mchatservice.connect (device);
          }
        })
        .setnegativebutton ("Cancel", new dialoginterface.onclicklistener () {
          @override
          public void onclick (dialoginterface dialog, int which) {
          }
        }). create (). show ();
  }
  //Search for Bluetooth devices
  private void searchdevice () {
    mtitle.settext ("Working on search.
.. ");
    setprogressbarindeterminatevisibility (true);
    if (mbluetoothadapter.isdiscovering ()) {
      mbluetoothadapter.canceldiscovery ();
    }
    mnewdeviceslist.clear ();
    mbluetoothadapter.startdiscovery ();
  }
  @override
  public boolean oncreateoptionsmenu (menu menu) {
    menu.add (0, 1, 0, "Search for devices");
    menu.add (0, 2, 0, "can be found");
    return true;
  }
  private final broadcastreceiver mreceiver=new broadcastreceiver () {
    @override
    public void onreceive (context context, intent intent) {
      string action=intent.getaction ();
      //When a new Bluetooth device is found
      if (bluetoothdevice.action_found.equals (action)) {
        bluetoothdevice device=intent
            .getparcelableextra (bluetoothdevice.extra_device);
        //if it "s already paired, skip it, because it" s been listed
        //already
        if (device.getbondstate ()!=bluetoothdevice.bond_bonded) {
          string s="Unpaired:" + device.getname () + "\ n"
              + device.getaddress ();
          if (! mnewdeviceslist.contains (s))
            mnewdeviceslist.add (s);
        }
        //when discovery is finished, change the activity title
      } else if (bluetoothadapter.action_discovery_finished
          .equals (action)) {
        setprogressbarindeterminatevisibility (false);
        if (mnewdeviceslist.size () == 0) {
          toast.maketext (bluetoothchat.this, "No new devices found",              toast.length_short) .show ();
        }
        mtitle.settext ("Not connected");
        linkdevice ();
      }
    }
  };
  @override
  public boolean onoptionsitemselected (menuitem item) {
    switch (item.getitemid ()) {
    case 1:
      searchdevice ();
      return true;
    case 2:
      //ensure this device is discoverable by others
      ensurediscoverable ();
      return true;
    }
    return false;
  }
}
package com.it2388.bluetooth;
/*
 * copyright (c) 2009 the android open source project
 *
 * licensed under the apache license, version 2.0 (the "license");
 * you may not use this file except in compliance with the license.
 * you may obtain a copy of the license at
 *
 * http://www.apache.org/licenses/license-2.0
 *
 * unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "as is" basis, * without warranties or conditions of any kind, either express or implied.
 * see the license for the specific language governing permissions and
 * limitations under the license.
 * /
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.uuid;
import android.bluetooth.bluetoothadapter;
import android.bluetooth.bluetoothdevice;
import android.bluetooth.bluetoothserversocket;
import android.bluetooth.bluetoothsocket;
import android.content.context;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.util.log;
/**
 * this class does all the work for setting up and managing bluetooth
 * connections with other devices. it has a thread that listens for incoming
 * connections, a thread for connecting with a device, and a thread for
 * performing data transmissions when connected.
 *
 * * This class does all the work,Establish and manage Bluetooth connections with other devices.
It has a thread listening for incoming connections,A thread for connecting a device,And a thread that performs data transfers when connected.
 * /
public class bluetoothchatservice {
  //debugging
  private static final string tag="bluetoothchatservice";
  private static final boolean d=true;
  //name for the sdp record when creating server socket
  private static final string name="bluetoothchat";
  //unique uuid for this application
  private static final uuid my_uuid=uuid
      .fromstring ("fa87c0d0-afac-11de-8a39-0800200c9a66");
  //member fields
  private final bluetoothadapter madapter;
  private final handler mhandler;
  private acceptthread macceptthread;
  private connectthread mconnectthread;
  private connectedthread mconnectedthread;
  private int mstate;
  //constants that indicate the current connection state
  public static final int state_none=0;//we "re doing nothing
  public static final int state_listen=1;//now listening for incoming
                        //connections
  public static final int state_connecting=2;//now initiating an outgoing
                          //start an outgoing
                          //connection
  public static final int state_connected=3;//now connected to a remote
                          //connect to a remote
                          //device
  /**
   * constructor. prepares a new bluetoothchat session.
   *
   * @param context
   * the ui activity context
   * @param handler
   * a handler to send messages back to the ui activity
   * /
  public bluetoothchatservice (context context, handler handler) {
    madapter=bluetoothadapter.getdefaultadapter ();
    mstate=state_none;
    mhandler=handler;
  }
  /**
   * set the current state of the chat connection
   *
   * @param state
   * an integer defining the current connection state
   * /
  private synchronized void setstate (int state) {
    if (d)
      log.d (tag, "setstate ()" + mstate + "->" + state);
    mstate=state;
    //give the new state to the handler so the ui activity can update
    mhandler.obtainmessage (bluetoothchat.message_state_change, state, -1)
        .sendtotarget ();
  }
  /**
   * return the current connection state.
   * /
  public synchronized int getstate () {
    return mstate;
  }
  /**
   * start the chat service. specifically start acceptthread to begin a
   * session in listening (server) mode. called by the activity onresume ()
   * /
  public synchronized void start () {
    if (d)
      log.d (tag, "start");
    //cancel any thread attempting to make a connection
    if (mconnectthread!=null) {
      mconnectthread.cancel ();
      mconnectthread=null;
    }
    //cancel any thread currently running a connection
    if (mconnectedthread!=null) {
      mconnectedthread.cancel ();
      mconnectedthread=null;
    }
    //start the thread to listen on a bluetoothserversocket
    if (macceptthread == null) {
      macceptthread=new acceptthread ();
      macceptthread.start ();
    }
    setstate (state_listen);
  }
  /**
   * start the connectthread to initiate a connection to a remote device.
   *
   * @param device
   * the bluetoothdevice to connect
   * /
  public synchronized void connect (bluetoothdevice device) {
    if (d)
      log.d (tag, "connect to:" + device);
    //cancel any thread attempting to make a connection
    if (mstate == state_connecting) {
      if (mconnectthread!=null) {
        mconnectthread.cancel ();
        mconnectthread=null;
      }
    }
    //cancel any thread currently running a connection
    if (mconnectedthread!=null) {
      mconnectedthread.cancel ();
      mconnectedthread=null;
    }
    //start the thread to connect with the given device
    mconnectthread=new connectthread (device);
    mconnectthread.start ();
    setstate (state_connecting);
  }
  /**
   * start the connectedthread to begin managing a bluetooth connection
   *
   * @param socket
   * the bluetoothsocket on which the connection was made
   * @param device
   * the bluetoothdevice that has been connected
   * /
  public synchronized void connected (bluetoothsocket socket,      bluetoothdevice device) {
    if (d)
      log.d (tag, "connected");
    //cancel the thread that completed the connection
    if (mconnectthread!=null) {
      mconnectthread.cancel ();
      mconnectthread=null;
    }
    //cancel any thread currently running a connection
    if (mconnectedthread!=null) {
      mconnectedthread.cancel ();
      mconnectedthread=null;
    }
    //cancel the accept thread because we only want to connect to one
    //device
    if (macceptthread!=null) {
      macceptthread.cancel ();
      macceptthread=null;
    }
    //start the thread to manage the connection and perform transmissions
    mconnectedthread=new connectedthread (socket);
    mconnectedthread.start ();
    //send the name of the connected device back to the ui activity
    message msg=mhandler.obtainmessage (bluetoothchat.message_device_name);
    bundle bundle=new bundle ();
    bundle.putstring (bluetoothchat.device_name, device.getname ());
    msg.setdata (bundle);
    mhandler.sendmessage (msg);
    setstate (state_connected);
  }
  /**
   * stop all threads
   * /
  public synchronized void stop () {
    if (d)
      log.d (tag, "stop");
    if (mconnectthread!=null) {
      mconnectthread.cancel ();
      mconnectthread=null;
    }
    if (mconnectedthread!=null) {
      mconnectedthread.cancel ();
      mconnectedthread=null;
    }
    if (macceptthread!=null) {
      macceptthread.cancel ();
      macceptthread=null;
    }
    setstate (state_none);
  }
  /**
   * write to the connectedthread in an unsynchronized manner
   *
   * @param out
   * the bytes to write
   * @see connectedthread #write (byte [])
   * /
  public void write (byte [] out) {
    //create temporary object
    connectedthread r;
    //synchronize a copy of the connectedthread
    synchronized (this) {
      if (mstate!=state_connected)
        return;
      r=mconnectedthread;
    }
    //perform the write unsynchronized
    r.write (out);
  }
  /**
   * indicate that the connection attempt failed and notify the ui activity.
   * /
  private void connectionfailed () {
    setstate (state_listen);
    //send a failure message back to the activity
    message msg=mhandler.obtainmessage (bluetoothchat.message_toast);
    bundle bundle=new bundle ();
    bundle.putstring (bluetoothchat.toast, "Cannot connect to device");
    msg.setdata (bundle);
    mhandler.sendmessage (msg);
  }
  /**
   * indicate that the connection was lost and notify the ui activity.
   * /
  private void connectionlost () {
    setstate (state_listen);
    //send a failure message back to the activity
    message msg=mhandler.obtainmessage (bluetoothchat.message_toast);
    bundle bundle=new bundle ();
    bundle.putstring (bluetoothchat.toast, "The device connection was interrupted");
    msg.setdata (bundle);
    mhandler.sendmessage (msg);
  }
  /**
   * this thread runs while listening for incoming connections. it behaves
   * like a server-side client. it runs until a connection is accepted (or
   * until cancelled).
   * /
  private class acceptthread extends thread {
    //the local server socket
    private final bluetoothserversocket mmserversocket;
    public acceptthread () {
      bluetoothserversocket tmp=null;
      //create a new listening server socket
      try {
        tmp=madapter
            .listenusingrfcommwithservicerecord (name, my_uuid);
      } catch (ioexception e) {
        log.e ("sxd", "========= ufcomm exception =======", e);
      }
      mmserversocket=tmp;
    }
    public void run () {
      if (d)
        log.d (tag, "begin macceptthread" + this);
      setname ("acceptthread");
      bluetoothsocket socket=null;
      //listen to the server socket if we "re not connected
      while (mstate!=state_connected) {
        try {
          //this is a blocking call and will only return on a
          //successful connection or an exception
          socket=mmserversocket.accept ();
        } catch (ioexception e) {
          log.e ("sxd", "--->accept socket failed<---", e);
          break;
        }
        //if a connection was accepted
        if (socket!=null) {
          synchronized (bluetoothchatservice.this) {
            switch (mstate) {
            case state_listen:
            case state_connecting:
              //situation normal. start the connected thread.
              connected (socket, socket.getremotedevice ());
              break;
            case state_none:
            case state_connected:
              //either not ready or already connected. terminate
              //new socket.
              try {
                socket.close ();
              } catch (ioexception e) {
                log.e (tag, "could not close unwanted socket", e);
              }
              break;
            }
          }
        }
      }
      if (d)
        log.i (tag, "end macceptthread");
    }
    public void cancel () {
      if (d)
        log.d (tag, "cancel" + this);
      try {
        mmserversocket.close ();
      } catch (ioexception e) {
        log.e (tag, "close () of server failed", e);
      }
    }
  }
  /**
   * this thread runs while attempting to make an outgoing connection with a
   * device. it runs straight through;the connection either succeeds or
   * fails.
   * /
  private class connectthread extends thread {
    private final bluetoothsocket mmsocket;
    private final bluetoothdevice mmdevice;
    public connectthread (bluetoothdevice device) {
      mmdevice=device;
      bluetoothsocket tmp=null;
      //get a bluetoothsocket for a connection with the
      //given bluetoothdevice
      try {
        tmp=device.createrfcommsockettoservicerecord (my_uuid);
      } catch (ioexception e) {
        log.e (tag, "create () failed", e);
      }
      mmsocket=tmp;
    }
    public void run () {
      log.i (tag, "begin mconnectthread");
      setname ("connectthread");
      //always cancel discovery because it will slow down a connection
      madapter.canceldiscovery ();
      //make a connection to the bluetoothsocket
      try {
        //this is a blocking call and will only return on a
        //successful connection or an exception
        mmsocket.connect ();
      } catch (ioexception e) {
        log.e ("sxd", "An exception occurred on the link", e);
        connectionfailed ();
        //close the socket
        try {
          mmsocket.close ();
        } catch (ioexception e2) {
          log.e (tag,              "unable to close () socket during connection failure",              e2);
        }
        //start the service over to restart listening mode
        bluetoothchatservice.this.start ();
        return;
      }
      //reset the connectthread because we "re done
      synchronized (bluetoothchatservice.this) {
        mconnectthread=null;
      }
      //start the connected thread
      connected (mmsocket, mmdevice);
    }
    public void cancel () {
      try {
        mmsocket.close ();
      } catch (ioexception e) {
        log.e (tag, "close () of connect socket failed", e);
      }
    }
  }
  /**
   * this thread runs during a connection with a remote device. it handles all
   * incoming and outgoing transmissions.
   *
   * Data transfer with the settings of the established connection
   * /
  private class connectedthread extends thread {
    private final bluetoothsocket mmsocket;
    private final inputstream mminstream;
    private final outputstream mmoutstream;
    public connectedthread (bluetoothsocket socket) {
      log.d (tag, "create connectedthread");
      mmsocket=socket;
      inputstream tmpin=null;
      outputstream tmpout=null;
      //get the bluetoothsocket input and output streams
      try {
        tmpin=socket.getinputstream ();
        tmpout=socket.getoutputstream ();
      } catch (ioexception e) {
        log.e (tag, "temp sockets not created", e);
      }
      mminstream=tmpin;
      mmoutstream=tmpout;
    }
    public void run () {
      log.i (tag, "begin mconnectedthread");
      byte [] buffer=new byte [1024];
      int bytes;
      //keep listening to the inputstream while connected
      while (true) {
        try {
          //read from the inputstream
          bytes=mminstream.read (buffer);
          //send the obtained bytes to the ui activity
          mhandler.obtainmessage (bluetoothchat.message_read, bytes,              -1, buffer) .sendtotarget ();
        } catch (ioexception e) {
          log.e (tag, "disconnected", e);
          connectionlost ();
          break;
        }
      }
    }
    /**
     * write to the connected outstream.
     *
     * @param buffer
     * the bytes to write
     * /
    public void write (byte [] buffer) {
      try {
        mmoutstream.write (buffer);
        //share the sent message back to the ui activity
        mhandler.obtainmessage (bluetoothchat.message_write, -1, -1,            buffer) .sendtotarget ();
      } catch (ioexception e) {
        log.e (tag, "exception during write", e);
      }
    }
    public void cancel () {
      try {
        mmsocket.close ();
      } catch (ioexception e) {
        log.e (tag, "close () of connect socket failed", e);
      }
    }
  }
}
  • Previous Jquery method to implement click to change navigation style
  • Next Mobile page flip plugin droploadjs (supports Zepto and jQuery)