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

📄 client.java

📁 这是一个蓝牙五子棋的手机游戏,大家来看看呀
💻 JAVA
字号:


import javax.microedition.lcdui.*;
import java.util.Vector;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

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;

public class Client implements Runnable, DiscoveryListener
{
    private Connection connection;
    private DiscoveryAgent discoveryAgent;
  
    private static final UUID ECHO_SERVER_UUID = new UUID(
        "F0E0D0C0B0A000908070605040302010", false);

    Vector devices = new Vector();
  
    Vector records = new Vector();
    String messagex, messagey;
    String message_1, message_2; 
    StreamConnection conn;
    int i = 1;
   
    int[]transIDs;
    private Thread processorThread;
    private boolean isClosed = false;
    private int discType;

    public Client(Connection connection)
    {

        this.connection = connection;
    }

    public void start()
    {

        processorThread = new Thread(this);
        processorThread.start();
    }

    public void sendMessage(String message1, String message2)
    {
        message_1 = message1;
        message_2 = message2;
        Thread fetchThread = new Thread()
        {
            public void run()
            {
                try
                {
                    ServiceRecord sr = (ServiceRecord)records.elementAt(0);
                    String url = sr.getConnectionURL
                        (ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
           


                    conn = (StreamConnection)Connector.open(url);
                 
                    DataOutputStream dos = conn.openDataOutputStream();
                    dos.writeUTF(message_1);
                    System.out.println("writex" + i + message_1);
                    dos.writeUTF(message_2);
                    System.out.println("writey" + i + message_2);
                    dos.flush();
                    dos.close();

                    DataInputStream dis = conn.openDataInputStream();
                    messagex = dis.readUTF();
                    System.out.println("messagex is" + i + messagex);
                    messagey = dis.readUTF();
                    System.out.println("messagey is" + i + messagey);
                    dis.close();
                    System.out.println(i);
                    i++;
                    connection.receiveMessage(messagex, messagey);
                }
                catch (Exception e)
                {
                    System.out.println("Exception");
                    e.printStackTrace();
                }
            }
        };
        fetchThread.start();

    }


    public synchronized void run()
    {

        try
        {
            LocalDevice local = LocalDevice.getLocalDevice();
            discoveryAgent = local.getDiscoveryAgent();

        }
        catch (BluetoothStateException e)
        {
            System.err.println("Can't initialize bluetooth: " + e);
        }


        try
        {
            discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
        
        }
        catch (BluetoothStateException bse){}

        try
        {
            wait(); 
        }
        catch (InterruptedException e)
        {
            System.err.println("Unexpected interuption: " + e);
            return ;
        }

        if (isClosed)
        {
            return ;
        }


        UUID[]uuids = new UUID[1];
        uuids[0] = ECHO_SERVER_UUID;
        transIDs = new int[devices.size()];
        if(devices.size()!=0){
        for (int i = 0; i < devices.size(); i++)
        {
            RemoteDevice rd = (RemoteDevice)devices.elementAt(i);
            try
            {
              
                System.out.println("begin searchservices");
                transIDs[i] = discoveryAgent.searchServices(null, uuids, rd,
                    this);
            }
            catch (BluetoothStateException e)
            {
                continue;
            }

        } 
      }
        try
        {
          
            wait();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        System.out.println("Client run end");





    }


    public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass
        deviceClass)
    {
        devices.addElement(remoteDevice);
        System.out.println("discovered a device ");
        System.out.println(remoteDevice.getBluetoothAddress());
    } 
    public void inquiryCompleted(int discType)
    {
        System.out.println("start inquiry completed");
        this.discType = discType;
        if (discType != INQUIRY_COMPLETED || devices.size() == 0)
        {
            connection.completeInitialization(false);
        }
        synchronized(this)
        {
            notify();
        }

    }

    public void serviceSearchCompleted(int transID, int respCode)
    {
        System.out.println("complete service Search");
        switch (respCode)
        {
            case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
                break;
            case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
                break;
            case DiscoveryListener.SERVICE_SEARCH_ERROR:
                break;
            case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
                break;
            case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
                break;
        }
        synchronized(this)
        {
            notify();
        }

    }

    public void servicesDiscovered(int transID, ServiceRecord[]serviceRecord)
    {
      
        System.out.println("get server");

        for (int i = 0; i < serviceRecord.length; i++)
        {
            records.addElement(serviceRecord[i]);
        }

    } 


    void destroy()
    {
        synchronized(this)
        {
            isClosed = true;

            notify();

           
        }

  
        try
        {
            processorThread.join();
        }
        catch (InterruptedException e){}
     
    }

}

⌨️ 快捷键说明

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