How make a SMB connection through Network object in Android?

703 Views Asked by At

I'm trying to make a SMB (Samba) connection to get a list of files and download them with the SMBClient of smbj library.

To that I have to connect to a specific network and use that class, but in Android Q I have to change the way to connect to the wireless, like this:

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.

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(network)

1

There are 1 best solutions below

0
On

I found a method in the ConnectivityManager class the method is bindProcessToNetwork

connectivityManager.bindProcessToNetwork(network)