Android Wifi熱點 資料傳輸Socket 通訊

coyan發表於2021-09-09

首先,描述下功能,兩個手機,其中一個手機開熱點,另外一個手機連線熱點,2個手機間資料通訊(不需要流量)。簡述一下原理:開熱點的手機相當於路由器,連線的手機作為客戶端,獲取路由器的IP建立Socket 連線,開始雙方通訊。

圖片描述

一、伺服器端:

開熱點的作為伺服器端

初始化資料:

    public static final int DEVICE_CONNECTING = 1;//有裝置正在連線熱點
    public static final int DEVICE_CONNECTED  = 2;//有裝置連上熱點
    public static final int SEND_MSG_SUCCSEE  = 3;//傳送訊息成功
    public static final int SEND_MSG_ERROR    = 4;//傳送訊息失敗
    public static final int GET_MSG           = 6;//獲取新訊息
    private TextView      text_state;    private WifiManager   wifiManager;    /**
     * 連線執行緒
     */
    private ConnectThread connectThread;    /**
     * 監聽執行緒
     */
    private ListenerThread listenerThread;    /**
     * 熱點名稱
     */
    private static final String WIFI_HOTSPOT_SSID = "TEST";    /**
     * 埠號
     */
    private static final int    PORT              = 54321;    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.create_wifi).setOnClickListener(this);
        findViewById(R.id.close_wifi).setOnClickListener(this);
        findViewById(R.id.send).setOnClickListener(this);
        text_state = (TextView) findViewById(R.id.receive);


        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);        /**
         * 先開啟監聽執行緒,在開啟連線
         */
        listenerThread = new ListenerThread(PORT, handler);
        listenerThread.start();        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }        //        開啟連線執行緒
        new Thread(new Runnable() {            @Override
            public void run() {                try {
                    Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress());                    //本地路由開啟通訊
                    String ip = getWifiApIpAddress();                    if (ip != null) {
                    } else {
                        ip = "192.168.43.1";
                    }
                    Socket socket = new Socket(ip, PORT);
                    connectThread = new ConnectThread(MainActivity.this, socket, handler);
                    connectThread.start();

                } catch (IOException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {                        @Override
                        public void run() {
                            text_state.setText("建立通訊失敗");
                        }
                    });

                }
            }
        }).start();


    }

建立wifi 熱點,之後開啟Socket 監聽執行緒

 /**
     * 建立Wifi熱點
     */
    private void createWifiHotspot() {        if (wifiManager.isWifiEnabled()) {            //如果wifi處於開啟狀態,則關閉wifi,
            wifiManager.setWifiEnabled(false);
        }        final WifiConfiguration config = new WifiConfiguration();
        config.SSID = WIFI_HOTSPOT_SSID;
        config.preSharedKey = "123456789";
        config.hiddenSSID = false;
        config.allowedAuthAlgorithms
                .set(WifiConfiguration.AuthAlgorithm.OPEN);//開放系統認證
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers
                .set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;        //透過反射呼叫設定熱點

        //192.168.43.59
        //        Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress() +
        //                "n");
        try {
            Method method = wifiManager.getClass().getMethod(                    "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);            boolean enable = (Boolean) method.invoke(wifiManager, config, true);            if (enable) {
                text_state.setText("熱點已開啟 SSID:" + WIFI_HOTSPOT_SSID + " password:123456789");                //        開啟連線執行緒
                new Thread(new Runnable() {                    @Override
                    public void run() {                        try {
                            Log.i("ip", "getWifiApIpAddress()" + getWifiApIpAddress()
                            );
                            String ip = getWifiApIpAddress();                            if (ip != null) {
                            } else {                                //一般Android手機預設路由是
                                ip = "192.168.43.1";
                            }                            //本地路由開啟通訊
                            Socket socket = new Socket(ip, PORT);
                            connectThread = new ConnectThread(MainActivity.this, socket, handler);
                            connectThread.start();


                        } catch (IOException e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {                                @Override
                                public void run() {
                                    text_state.setText("建立通訊失敗");
                                }
                            });

                        }
                    }
                }).start();
                Thread.sleep(1000);                //                listenerThread = new ListenerThread(PORT, handler);
                //                listenerThread.start();
            } else {
                text_state.setText("建立熱點失敗");
            }
        } catch (Exception e) {
            e.printStackTrace();
            text_state.setText("建立熱點失敗");
        }
    }

伺服器端獲取本地路由:

   public String getWifiApIpAddress() {        try {            for (Enumeration en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();                if (intf.getName().contains("wlan")) {                    for (Enumeration enumIpAddr = intf
                            .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();                        if (!inetAddress.isLoopbackAddress()
                                && (inetAddress.getAddress().length == 4)) {
                            Log.d("Main", inetAddress.getHostAddress());                            return inetAddress.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("Main", ex.toString());
        }        return null;
    }

關閉熱點

 /**
     * 關閉WiFi熱點
     */
    public void closeWifiHotspot() {        try {
            Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
            method.setAccessible(true);
            WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
            Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            method2.invoke(wifiManager, config, false);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        text_state.setText("熱點已關閉");
    }

ListenerThread 監聽執行緒

import android.os.Handler;import android.os.Message;import android.util.Log;import com.example.syhuang.wifiserver.MainActivity;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;/**
 * 監聽執行緒
 * Created by syh on 2018/4/3
 */public class ListenerThread extends Thread {    private ServerSocket serverSocket = null;    private Handler handler;    private int     port;    private Socket  socket;    public ListenerThread(int port, Handler handler) {
        setName("ListenerThread");        this.port = port;        this.handler = handler;        try {
            serverSocket = new ServerSocket(port);//監聽本機的12345埠
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    @Override
    public void run() {        while (true) {            try {
                Log.i("ListennerThread", "阻塞");                //阻塞,等待裝置連線
                if (serverSocket != null)
                    socket = serverSocket.accept();
                Message message = Message.obtain();
                message.what = MainActivity.DEVICE_CONNECTING;
                handler.sendMessage(message);
            } catch (IOException e) {
                Log.i("ListennerThread", "error:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }    public Socket getSocket() {        return socket;
    }
}

ConnectThread 連線執行緒

package com.example.syhuang.wifiserver.thread;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import com.example.syhuang.wifiserver.MainActivity;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.text.DecimalFormat;/**
 * 連線執行緒
 * Created by syhuang on 2018/4/3
 */public class ConnectThread extends Thread {    private final Socket       socket;    private       Handler      handler;    private       InputStream  inputStream;    private       OutputStream outputStream;
    Context context;    public ConnectThread(Context context, Socket socket, Handler handler) {
        setName("ConnectThread");
        Log.i("ConnectThread", "ConnectThread");        this.socket = socket;        this.handler = handler;        this.context = context;
    }    @Override
    public void run() {/*        if(activeConnect){
//            socket.c
        }*/
        if (socket == null) {            return;
        }
        handler.sendEmptyMessage(MainActivity.DEVICE_CONNECTED);        try {            //獲取資料流
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();            byte[] buffer = new byte[1024];            int bytes;            while (true) {                //讀取資料
                bytes = inputStream.read(buffer);                if (bytes > 0) {                    final byte[] data = new byte[bytes];
                    System.arraycopy(buffer, 0, data, 0, bytes);

                    Message message = Message.obtain();
                    message.what = MainActivity.GET_MSG;
                    Bundle bundle = new Bundle();
                    bundle.putString("MSG", new String(data));
                    message.setData(bundle);
                    handler.sendMessage(message);
                }                //                DataInputStream dis = null;
                //                FileOutputStream fos = null;
                //                try {
                //                    dis = new DataInputStream(inputStream);
                //
                //                    // 檔名和長度
                //                    String fileName = dis.readUTF();
                //                    if (!fileName.equals("")) {
                //                        long fileLength = dis.readLong();
                //                        Log.i("ConnectThread", "======== 檔案接收 [File Name:" + fileName + "] " +
                //                                "[Size:" + getFormatFileSize(fileLength) + "] ========");
                //                        File directory = new File(Environment.getExternalStorageDirectory() + "/");
                //                        if (!directory.exists()) {
                //                            directory.mkdir();
                //                        } else {
                //                        }
                //                        File file = new File(directory.getAbsolutePath() + File.separatorChar + fileName);
                //                        fos = new FileOutputStream(file);
                //
                //                        // 開始接收檔案
                //                        byte[] bytesA = new byte[1024];
                //                        int length = 0;
                //                        int progress = 0;
                //                        while ((length = dis.read(bytesA, 0, bytesA.length)) != -1) {
                //                            Log.i("ConnectThread", length + "...");
                //                            fos.write(bytesA, 0, length);
                //                            fos.flush();
                //                            progress += length;
                //                            Log.i("ConnectThread", "| " + (100 * progress / file.length()) + "% |");
                //                        }
                //                        Log.i("ConnectThread", "檔案傳輸完成");
                //
                //                        Message message = Message.obtain();
                //                        message.what = MainActivity.GET_MSG;
                //                        Bundle bundle = new Bundle();
                //                        bundle.putString("MSG", new String("接收到檔案:" + file.getAbsolutePath()));
                //                        message.setData(bundle);
                //                        handler.sendMessage(message);
                //                    } else {
                //                        //讀取資料
                //                        bytes = inputStream.read(buffer);
                //                        if (bytes > 0) {
                //                            final byte[] data = new byte[bytes];
                //                            System.arraycopy(buffer, 0, data, 0, bytes);
                //
                //
                //                            Message message = Message.obtain();
                //                            message.what = MainActivity.GET_MSG;
                //                            Bundle bundle = new Bundle();
                //                            bundle.putString("MSG", new String(data));
                //                            message.setData(bundle);
                //                            handler.sendMessage(message);
                //
                //                            Log.i("ConnectThread", "讀取到資料:" + new String(data));
                //                        }
                //                    }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    /**
     * 格式化檔案大小
     *
     * @param length
     * @return
     */
    private String getFormatFileSize(long length) {
        DecimalFormat df = new DecimalFormat("#0.0");        double size = ((double) length) / (1 = 1) {            return df.format(size) + "GB";
        }
        size = ((double) length) / (1 = 1) {            return df.format(size) + "MB";
        }
        size = ((double) length) / (1 = 1) {            return df.format(size) + "KB";
        }        return length + "B";
    }    public static boolean copyFile(InputStream inputStream, OutputStream out) {        byte buf[] = new byte[1024];        int len;        try {            while ((len = inputStream.read(buf)) != -1) {
                out.write(buf, 0, len);

            }
            out.close();            //            inputStream.close();
        } catch (IOException e) {            return false;
        }        return true;
    }    /**
     * 傳送資料
     */
    public void sendData(String msg) {
        Log.i("ConnectThread", "傳送資料:" + (outputStream == null));        if (outputStream != null) {            try {
                outputStream.write(msg.getBytes());
                Log.i("ConnectThread", "傳送訊息:" + msg);
                Message message = Message.obtain();
                message.what = MainActivity.SEND_MSG_SUCCSEE;
                Bundle bundle = new Bundle();
                bundle.putString("MSG", new String(msg));
                message.setData(bundle);
                handler.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
                Message message = Message.obtain();
                message.what = MainActivity.SEND_MSG_ERROR;
                Bundle bundle = new Bundle();
                bundle.putString("MSG", new String(msg));
                message.setData(bundle);
                handler.sendMessage(message);
            }
        }
    }

}

二、客戶端

客戶端連線到熱點,程式碼差不多,初始化資料部分順序不一樣,先開啟連線執行緒,在開啟監聽執行緒,獲取路由器IP 地址有差別。其他基本一樣。

wifi獲取 已連線網路路由 路由ip地址

    /**
     * wifi獲取 已連線網路路由  路由ip地址
     * @param context
     * @return
     */
    private static String getWifiRouteIPAddress(Context context) {
        WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcpInfo = wifi_service.getDhcpInfo();        //        WifiInfo wifiinfo = wifi_service.getConnectionInfo();
        //        System.out.println("Wifi info----->" + wifiinfo.getIpAddress());
        //        System.out.println("DHCP info gateway----->" + Formatter.formatIpAddress(dhcpInfo.gateway));
        //        System.out.println("DHCP info netmask----->" + Formatter.formatIpAddress(dhcpInfo.netmask));
        //DhcpInfo中的ipAddress是一個int型的變數,透過Formatter將其轉化為字串IP地址
        String routeIp = Formatter.formatIpAddress(dhcpInfo.gateway);
        Log.i("route ip", "wifi route ip:" + routeIp);        return routeIp;
    }

初始化

 public static final int DEVICE_CONNECTING = 1;//有裝置正在連線熱點
    public static final int DEVICE_CONNECTED  = 2;//有裝置連上熱點
    public static final int SEND_MSG_SUCCSEE  = 3;//傳送訊息成功
    public static final int SEND_MSG_ERROR    = 4;//傳送訊息失敗
    public static final int GET_MSG           = 6;//獲取新訊息

    private TextView      text_state;    /**
     * 連線執行緒
     */
    private ConnectThread connectThread;    /**
     * 監聽執行緒
     */
    private ListenerThread listenerThread;    /**
     * 熱點名稱
     */
    private static final String WIFI_HOTSPOT_SSID = "TEST";    /**
     * 埠號
     */
    private static final int    PORT              = 54321;    private WifiManager wifiManager;    private TextView status_init;    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.send).setOnClickListener(this);
        findViewById(R.id.connect).setOnClickListener(this);
        findViewById(R.id.fileButton).setOnClickListener(this);
        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);        //檢查Wifi狀態
        if (!wifiManager.isWifiEnabled())
            wifiManager.setWifiEnabled(true);
        text_state = (TextView) findViewById(R.id.status_info);
        status_init = (TextView) findViewById(R.id.status_init);


        status_init.setText("已連線到:" + wifiManager.getConnectionInfo().getSSID() +                "nIP:" + getIp()
                + "n路由:" + getWifiRouteIPAddress(MainActivity.this));        //        initBroadcastReceiver();
        //        開啟連線執行緒
        new Thread(new Runnable() {            @Override
            public void run() {                try {
                    Socket socket = new Socket(getWifiRouteIPAddress(MainActivity.this), PORT);
                    connectThread = new ConnectThread(socket, handler);
                    connectThread.start();
                } catch (IOException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {                        @Override
                        public void run() {
                            text_state.setText("通訊連線失敗");
                        }
                    });

                }
            }
        }).start();


        listenerThread = new ListenerThread(PORT, handler);
        listenerThread.start();
    }

伺服器和客戶端Handler處理一樣

  private Handler handler = new Handler() {        @Override
        public void handleMessage(Message msg) {            switch (msg.what) {                case DEVICE_CONNECTING:
                    connectThread = new ConnectThread(listenerThread.getSocket(), handler);
                    connectThread.start();                    break;                case DEVICE_CONNECTED:
                    text_state.setText("裝置連線成功");                    break;                case SEND_MSG_SUCCSEE:
                    text_state.setText("傳送訊息成功:" + msg.getData().getString("MSG"));                    break;                case SEND_MSG_ERROR:
                    text_state.setText("傳送訊息失敗:" + msg.getData().getString("MSG"));                    break;                case GET_MSG:
                    text_state.setText("收到訊息:" + msg.getData().getString("MSG"));                    break;
            }
        }
    };

原文連結:http://www.apkbus.com/blog-864937-78344.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2236/viewspace-2805831/,如需轉載,請註明出處,否則將追究法律責任。

相關文章