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

📄 bluetoothclient.java

📁 Java编写的蓝牙手机遥控器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;

final class BluetoothClient
  implements Runnable, DiscoveryListener
{
  private static final UUID BRC_SERVER = new UUID("7771FDC2FDDF4c9bAFFC98BCD91BF93B", false);
  private static final int READY = 0;
  private static final int DEVICE_SEARCH_CONNECT = 1;
  private static final int CONNECTED = 2;
  private static final int INACTIVE = 3;
  private static final int DIRECT_CONNECT = 4;
  private static final int DISCONNECTING = 5;
  private static final int INCOMING_CONNECTION = 6;
  private static final int ACTION_IDLE = 0;
  private static final int ACTION_INQUIRY = 1;
  private static final int ACTION_SERVICE_SEARCH = 2;
  private static final int ACTION_CONNECTING = 3;
  public static final byte CONNECT_DIRECT = 0;
  public static final byte CONNECT_COM = 1;
  private int state = 0;
  private DiscoveryAgent discoveryAgent;
  private GUISearch parent;
  private BluetoothRemoteControl own;
  private boolean isClosed;
  private Thread processorThread;
  private Vector devices = new Vector();
  private Vector records = new Vector();
  private int discType;
  private int[] searchIDs;
  private UUID[] uuidSet;
  private int deviceToConnect;
  private StreamConnection conn = null;
  private StreamConnection serverConn = null;
  private OutputStream out = null;
  private BluetoothClient.BluetoothServer server;
  private boolean userCancel = false;
  private int action;
  private RecordStore db;

  BluetoothClient(BluetoothRemoteControl paramBluetoothRemoteControl)
  {
    this.own = paramBluetoothRemoteControl;
    this.processorThread = new Thread(this);
    this.processorThread.start();
  }

  public void registerConnectUI(GUISearch paramGUISearch)
  {
    this.parent = paramGUISearch;
  }

  public void run()
  {
    boolean bool = false;
    try
    {
      LocalDevice localLocalDevice = LocalDevice.getLocalDevice();
      this.discoveryAgent = localLocalDevice.getDiscoveryAgent();
      bool = true;
    }
    catch (Exception localException)
    {
      BluetoothRemoteControl.debugError("Can't initialize bluetooth: " + localException);
    }
    this.own.completeInitialization(bool);
    if (!(bool))
      return;
    this.uuidSet = new UUID[1];
    mainLoop();
  }

  public void deviceDiscovered(RemoteDevice paramRemoteDevice, DeviceClass paramDeviceClass)
  {
    if ((paramDeviceClass.getMajorDeviceClass() == 256) && (this.devices.indexOf(paramRemoteDevice) == -1))
    {
      this.parent.oneDeviceFound();
      this.devices.addElement(paramRemoteDevice);
    }
  }

  public void inquiryCompleted(int paramInt)
  {
    this.discType = paramInt;
    synchronized (this)
    {
      super.notify();
    }
  }

  public void servicesDiscovered(int paramInt, ServiceRecord[] paramArrayOfServiceRecord)
  {
    int i = 0;
    i = this.records.size();
    for (int j = 0; j < paramArrayOfServiceRecord.length; ++j)
      this.records.insertElementAt(paramArrayOfServiceRecord[j], j + i);
  }

  public void serviceSearchCompleted(int paramInt1, int paramInt2)
  {
    int i = -1;
    for (int j = 0; j < this.searchIDs.length; ++j)
      if (this.searchIDs[j] == paramInt1)
      {
        i = j;
        break;
      }
    if (i == -1)
      BluetoothRemoteControl.debugError("Unexpected transaction index: " + paramInt1);
    else
      this.searchIDs[i] = -1;
    for (j = 0; j < this.searchIDs.length; ++j)
      if (this.searchIDs[j] != -1)
        return;
    synchronized (this)
    {
      super.notify();
    }
  }

  void requestSearch()
  {
    synchronized (this)
    {
      super.notify();
    }
  }

  void incomingConnection(StreamConnection paramStreamConnection)
  {
    synchronized (this)
    {
      if (this.state == 0)
      {
        this.serverConn = paramStreamConnection;
        this.state = 6;
        this.action = 0;
        super.notify();
      }
    }
  }

  void connectToDevice(int paramInt)
  {
    this.deviceToConnect = paramInt;
    if (this.state == 0)
    {
      this.state = 1;
      requestSearch();
    }
    else
    {
      this.own.informInformation("Local bluetooth device is busy!");
    }
  }

  void cancelSearch()
  {
    this.userCancel = true;
    synchronized (this)
    {
      if (this.state != 0)
      {
        int i;
        if (this.action == 1)
          this.discoveryAgent.cancelInquiry(this);
        else if (this.action == 2)
          for (i = 0; i < this.searchIDs.length; ++i)
            this.discoveryAgent.cancelServiceSearch(this.searchIDs[i]);
        this.state = 0;
      }
    }
  }

  void destroy()
  {
    synchronized (this)
    {
      this.isClosed = true;
      super.notify();
    }
    try
    {
      this.processorThread.join();
    }
    catch (InterruptedException localInterruptedException)
    {
    }
    this.server.destroy();
  }

  public int getState()
  {
    return this.state;
  }

  public void write(byte[] paramArrayOfByte)
  {
    if (this.state == 2)
      try
      {
        this.out.write(paramArrayOfByte);
        this.out.flush();
      }
      catch (Exception localException)
      {
        this.own.startDiconnectTimer(50);
        this.state = 5;
      }
      catch (Error localError)
      {
      }
  }

  private synchronized void mainLoop()
  {
    this.state = 0;
    while (true)
    {
      while (true)
      {
        while (true)
          while (true)
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      while (true)
                      {
                        while (true)
                        {
                          while (true)
                          {
                            while (true)
                            {
                              while (true)
                              {
                                while (true)
                                {
                                  if (this.isClosed)
                                    break label586;
                                  BluetoothRemoteControl.debug("Bt wait...");
                                  try
                                  {
                                    super.wait();
                                  }
                                  catch (InterruptedException localInterruptedException)
                                  {
                                    BluetoothRemoteControl.debugError("Unexpected interuption 1: " + localInterruptedException);
                                    return;
                                  }
                                  BluetoothRemoteControl.debug("Bt wait end");
                                  if (this.isClosed)
                                    return;
                                  this.userCancel = false;
                                  switch (this.state)
                                  {
                                  case 3:
                                  case 0:
                                  case 1:
                                  case 4:
                                  case 2:
                                  case 5:
                                  case 6:
                                  }
                                }
                                BluetoothRemoteControl.debug("Inquiry!");
                                this.action = 1;
                                String str1 = "";
                                str1 = searchDevices();
                                if (str1.length() <= 0)
                                  break;
                                this.own.informError(str1);
                                this.action = 0;
                              }
                              if (this.devices.size() != 0)
                                break;
                              this.own.informInformation("No Bluetooth computers found!");
                              this.action = 0;
                            }
                            this.action = 0;
                            BluetoothRemoteControl.debug("Inquiry done!");
                          }
                          BluetoothRemoteControl.debug("DEVICE_SEARCH_CONNECT");
                          this.action = 2;
                          if (readConnectMethod() == 0)
                            this.uuidSet[0] = BRC_SERVER;
                          else
                            this.uuidSet[0] = new UUID(4353L);
                          if (searchServices())
                            break;
                          this.parent.informSearchError("Service search failed!");
                          this.action = 0;
                          this.state = 0;
                        }
                        if (this.records.size() != 0)
                          break;
                        this.parent.informSearchError("No correct service found! Please make sure Bluetooth Remote Control is running!");
                        this.action = 0;
                        this.state = 0;
                      }
                      this.action = 0;
                      this.action = 3;
                      if (connectToBlutoothDevice(false))
                        break;
                      if (readConnectMethod() == 0)
                        this.parent.informSearchError("Failed to connect!");
                      else
                        this.parent.informSearchError("Failed to connect! \n Have you set the correct \n PC COM port?");
                      this.action = 0;
                      this.state = 0;
                    }
                    this.state = 2;
                    this.parent.connectSuccess();
                    this.action = 0;
                  }
                  String str2 = readRecord();
                  if (str2.length() <= 0)
                    break label457;
                  BluetoothRemoteControl.debug("Connecting...");
                  this.action = 3;
                  if (connectToBlutoothDevice(true))

⌨️ 快捷键说明

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