⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btutil.java

📁 A bluetooth SMS patcher Java ME application I made quickly after I thought about making one. What i
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * BtUtil.java * * Created on August 2, 2006, 7:10 PM */package BtUtil;import java.io.IOException;import java.io.PrintStream;import java.util.Hashtable;import java.util.Vector;import javax.bluetooth.BluetoothStateException;import javax.bluetooth.DeviceClass;import javax.bluetooth.DiscoveryAgent;import javax.bluetooth.DiscoveryListener;import javax.bluetooth.LocalDevice;import javax.bluetooth.RemoteDevice;import javax.bluetooth.ServiceRecord;import javax.bluetooth.UUID;import javax.microedition.io.Connection;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import javax.microedition.io.StreamConnectionNotifier;/** * This class should be the simplest way to develop a&nbsp;bluetooth * application. If you don't have time to read the JSR82 specification and * need to develop a bluetooth enabled software, you came to&nbsp;the * right place.<br> * <br> * <span style="font-weight: bold;">PART I * -&nbsp;Bluetooth in a Nutshell</span><br> * <br> * First, you must understand something about&nbsp;bluetooh.<br> * <ul style="font-weight: bold;"> * <li>What is that?&nbsp;</li> * </ul> * &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; It is a * wireless communication technology. * <ul> * <li><span style="font-weight: bold;">What is it * used for?</span><br> * </li> * </ul> * &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; It is * used to connect devices (e.g a printer to a computer, a laptop to a * cellphone, a mouse to a computer) over ther air, that means, with no * cables. Therefore, to start playing with bluetooth, you need, at least, * two bluetooth devices.<br> * <ul> * <li><span style="font-weight: bold;">What else I * need to know?</span></li> * </ul> * &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; It's * necessary to understand the basic steps to get the bluetooth working. * &nbsp;This is only a didactic explanation. Don't waste you efforts * trying to do it in you cell phone. We will do it later, * programmatically. Just understand it.<br> * <ol> * <ol> * <li>First o all, enable the bluetooth stack on both devices * you have, let's say, device A and device B.</li> * <li>Then, ask device A to find which bluetooth enabled * devices are near to it.</li> * <li>Probably, it will find device B in the neighborhood and, * maybe, others devices.</li> * <li>Choose device B.</li> * <li>Each bluetooth device may have a bunch of bluetooth * services. Usually, you know which service you want to connect to. * &nbsp;Each bluetooth service is identified by a big number called * UUID.&nbsp;</li> * <li>So, connect to the desired service and start the data * exchange. The service connection is done with the service UUID.</li> * <li>On a bluetooth communication, there's a master and * slaves. Usually, in terms of implementation, there's a server code, * that publish the bluetooth service, and a client code, that consume the * bluetooth service.</li> * </ol> * </ol> * <ul style="font-weight: bold;"> * <li>Need more references?</li> * </ul> * &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Don't * hesitate, google it! :-) The root referene is <a * href="http://www.bluetooth.org">http://www.bluetooth.org</a><br> * <span style="font-weight: bold;">PART II - The BtUtil API<br> * <br> * </span>Ok, let's see how things work. <br> * <br> * Download the BtUtil API library, add it to your application's classpath * and start it:<br> * <br> * <small><span * style="font-family: Courier New,Courier,monospace;">BtUtil * bt = new BtUtil();</span></small><br> * <br> * <span style="font-weight: bold;">Client code:</span><br> * <br> * As we learnt in part I, the first thing to do is to dicovery other * bluetooth devices. Do it with the code below:<br> * <span style="font-family: Courier New,Courier,monospace;"></span><small><br> * <span style="font-family: Courier New,Courier,monospace;">Vector * devices = bt.discoverDevicesSync();</span><br> * </small><br> * Then, select a device from the device list and connect to one of its * services, let's say, the service identified by the UUID * 123456789ABCDEF123456789ABCDEF12:<br> * <br> * <small><span * style="font-family: Courier New,Courier,monospace;">StreamConnection * con = <br> * &nbsp;&nbsp;&nbsp;&nbsp;(StreamConnection)bt.connectToService(device, * "123456789ABCDEF123456789ABCDEF12");</span><br> * </small><br> * Now, send data to the service:<br> * <br> * <small><span * style="font-family: Courier New,Courier,monospace;">DataOutputStream * dout = con.openDataOutputStream();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">dout.writeUTF("My * lovely data.");</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">dout.flush();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">dout.close();</span><br * style="font-family: Courier New,Courier,monospace;"> * </small><br> * And read it's reponse:<br> * <span style="font-weight: bold;"></span><br> * <small style="font-family: Courier New,Courier,monospace;">DataInputStream * din = con.openDataInputStream();<br> * String dataReceived = din.readUTF();<br> * din.close();</small><br> * <br> * And that's it. Now you can connect to a bluetooth service running on a * remote device, send and receive data from it.<br> * <br> * <span style="font-weight: bold;">Service code:</span><br> * <br> * The question that raises now is "<span style="font-style: italic;">how * that</span><span * style="font-family: Times New Roman,Times,serif; font-style: italic;"></span><span * style="font-style: italic;"> * 123456789ABCDEF123456789ABCDEF12 service was implemented?</span>" * I 'll show you the code:<br> * <br> * <small><span * style="font-family: Courier New,Courier,monospace;">bt.startStreamService</span></small><small><span * style="font-family: Courier New,Courier,monospace;">("123456789ABCDEF123456789ABCDEF12"</span></small><small><span * style="font-family: Courier New,Courier,monospace;">, * listener);</span></small><br> * <br> * where <span style="font-family: Courier New,Courier,monospace;">listener</span> * is an BtUtilServerListener implementation, so you cand receive * notifications when a new client connection is established:<br> * <small><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">public * void connectionEstablished(Connection con) {</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; * System.out.println("Connection established notification received by * BtUtilServerListener!");</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;try * {</span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamConnection * sppCon = (StreamConnection)con;</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataInputStream * din = sppCon.openDataInputStream();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DataOutputStream * dout = sppCon.openDataOutputStream();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Data * received [" + din.readUTF() + "]");</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dout.writeUTF("BtUtil.startStreamService() * methot say: bluetooth rocks!!!");</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><br style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dout.flush();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dout.close();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;din.close();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * </span><span * style="font-family: Courier New,Courier,monospace;"><br> * &nbsp;&nbsp;&nbsp; } catch (Exception e) {</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; * &nbsp;&nbsp;&nbsp; e.printStackTrace();</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br * style="font-family: Courier New,Courier,monospace;"> * <span style="font-family: Courier New,Courier,monospace;">}</span></small>&nbsp;&nbsp;&nbsp; * <br> * <br> * Now, you're ready to the bluetooth world.<br> * Obs: Parts of our code was based on BLUElet API (www.benhui.net). * * * * @author alegomes * * LICENSE: * This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html) */public class BtUtil {    /** The output where debug messages will be printed out. */    private PrintStream out;    /** Reference for the localDevice running this code. */    private LocalDevice localDevice;    /** Object reponsible for the discovery process. */    private DiscoveryAgent agent;    /**     * List of remote devices found.     * Each device discovered in the neighborhood (Listener.deviceDiscovered)     * is inserted into this list.     */    private Vector remoteDevices = new Vector();    /**     * List of services found in a selected remote device.     * Each service discovered in the remote device (Listener.servicesDiscovered)     * is inserted into this list.     */    private Vector remoteServices = new Vector();    /**     * A set of objects interested in receveing bluetooth client notifications.     * Notifications types: device discovery, device scanning finished,     * service discovery, service search finished.     */    private Vector clientListeners = new Vector();    /**     * A set of services (StartedStreamService objects) started from the     * startStreamService method.     *     * Each bluetooth service started has a StreamConnectionNotified, responsible     * to receive new connections, and its own set of listeners.     * So, this Hashtable object will look like:<p>     *     * urlService1 -> startedStreamServiceObject1     * urlService2 -> startedStreamServiceObject1     * urlService3 -> startedStreamServiceObject1     */    private Hashtable startedStreamServices = new Hashtable();    /**     * Mark the end of the <b>device</b> discovery process. It's used in     * discoverDevicesSync and in Listener.inquiryCompleted methods.     */    private boolean deviceDiscoveryFinished = false;    /**     * Mark the end of the <b/>service</b> discovery process. It's used in     * discoverServicesSync and in Listener.serviceSearchComplete methods.     */    private boolean serviceDiscoveryFinished = false;    /**     * Constructor     *     * @param out The place where debug messages will be printed out.     */    public BtUtil(PrintStream out) throws BtUtilException {        //this.alert = alert;        this.out = out;        // Initialize the JABWT stack        initBluetoothStack();    }    /**     * Constructor.<br/>     * It uses the System.out for debug messages.     */    public BtUtil() throws BtUtilException {        this(System.out);    }    /**     * Close resources.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -