简体   繁体   English

如何通过 Android 中的 Network 对象建立 SMB 连接?

[英]How make a SMB connection through Network object in Android?

I'm trying to make a SMB ( Samba ) connection to get a list of files and download them with the SMBClient of smbj library.我正在尝试建立 SMB ( Samba ) 连接以获取文件列表并使用smbj库的SMBClient下载它们。

To that I have to connect to a specific network and use that class, but in Android QI have to change the way to connect to the wireless, like this:为此,我必须连接到特定网络并使用该类,但在 Android QI 中必须更改连接到无线的方式,如下所示:

val wifiNetworkSpecifier: WifiNetworkSpecifier = WifiNetworkSpecifier.Builder().apply {
    setSsid(ssid)
    setWpa2Passphrase(password)
}.build()

val networkRequest: NetworkRequest = NetworkRequest.Builder().apply {
    addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
    setNetworkSpecifier(wifiNetworkSpecifier)
}.build()

val networkCallback: ConnectivityManager.NetworkCallback = object : ConnectivityManager.NetworkCallback() {
    override fun onAvailable(network: Network) {
        Log.d(tag, "::onAvailable - Entry")
        super.onAvailable(network)
    }

    override fun onUnavailable() {
        Log.d(tag, "::onUnavailable - Entry")
        super.onUnavailable()
    }
}

This makes a connection in the app, but establishes the main connection via mobile data and I can't establish a connection because the server is unreachable.这在应用程序中建立了连接,但通过移动数据建立了主连接,我无法建立连接,因为服务器无法访问。 I have to find a way to make the connection through the network object in the onAvailable function.我必须找到一种方法来通过onAvailable函数中的network对象建立连接。

Did you know how or is there an alternative way?

Solution解决方案

I found a method in the ConnectivityManager class the method is bindProcessToNetwork我在ConnectivityManager类中找到了一个方法,该方法是bindProcessToNetwork

connectivityManager.bindProcessToNetwork(network)

我在 ConnectivityManager 类中找到了一个方法,该方法是 bindProcessToNetwork

connectivityManager.bindProcessToNetwork(network)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM