Search code examples
javaandroidjcifs

Connecting to PC to view shared folders from Android device


I am working on a samba client for Android. Given an IP address it should connect to it and browse the shared folders.

For this I use JCIFS. I dropped the jar in my Android project and added following code to connect to PC and get the list of files:

private void connectToPC() throws IOException {
    String ip = "x.x.x.x";
    String user = Constants.username + ":" + Constants.password;
    String url = "smb://" + ip;

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
    SmbFile root= new SmbFile(url, auth);

    String[] files = root.list();
    for (String fileName : files) {
        Log.d("GREC", "File: " + fileName);
    }
}

And I get in return: jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.

But the credentials are correct. I also tried with another samba client from the android market that uses JCIFS and it successfully connected to that ip, so obviously I am doing something wrong here but don't know what especially.

Any help is highly appreciated.


Solution

  • In the end I managed successfully to connect to PC. The issue turned out to be in the NtlmPasswordAuthentication(); constructor.

    So, instead of this:

    String user = Constants.username + ":" + Constants.password;
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
    

    I changed to this:

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",
                        Constants.username, Constants.password);
    

    I don't know why, perhaps it's because of ":" special character, perhaps because of Android, but passing an empty domain name, the user name, and password separately to the constructor, solved the issue.