首页 > 解决方案 > 通过热点传输文件

问题描述

通过热点传输文件

项目通过wifi将文件传输到PC。

我想通过wifi和热点发送文件。

我的项目

与 wifi IP 共享文件。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;

import com.hwangjr.rxbus.RxBus;

import me.pengtao.filetransfer.Constants;

public class WifiConnectChangedReceiver extends 
BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            

 RxBus.get().post(Constants.RxBusEvent 
Type.WIFI_CONNECT_CHANGE_EVENT, 
networkInfo.getState());
        }
    }
}
}

此代码用于获取 wifi 的 IP 地址。我想通过wifi和热点共享文件。因此,我替换了此代码以获取 wifi 和热点 IP。

public void getDeviceIpAddress() {
    try {

        for (Enumeration<NetworkInterface> enumeration = NetworkInterface
                .getNetworkInterfaces(); enumeration.hasMoreElements();) {
            NetworkInterface networkInterface = enumeration.nextElement();
            for (Enumeration<InetAddress> enumerationIpAddr = networkInterface
                    .getInetAddresses(); enumerationIpAddr
                    .hasMoreElements();) {
                InetAddress inetAddress = enumerationIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()
                        && inetAddress.getAddress().length == 4) {
                    tvServerIP.setText(inetAddress.getHostAddress());
                }
            }
        }
    } catch (SocketException e) {
        Log.e("ERROR:", e.toString());
    }
}

我尝试使用此代码,但没有成功。

我怎样才能做到这一点?

标签: androidwifihotspot

解决方案


推荐阅读