Android Tutorial Android SMB / CIFS tutorial

Discussion in 'Tutorials & Examples' started by Erel, Apr 23, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The SMB library allows you to access Microsoft Windows Network file system.
    You can read more information about this protocol here: Server Message Block - Wikipedia, the free encyclopedia

    SMB can be used to upload or download files from Windows shared folders.
    This library wraps JCIFS library: JCIFS

    Before starting with your own solution it is recommended to download an existing SMB client such as AndSMB: https://play.google.com/store/apps/details?id=lysesoft.andsmb and get it working. There are several configurations that may prevent a SMB client from working properly.

    If you are using Windows 7 you may want to allow access without an existing Windows account:

    Go to Advanced Sharing Settings and choose Turn off password protection.

    [​IMG]

    As with any network operations, all the operations are done in the background. An event will be raised when the operation completes.

    For example to list all files and directories we can use a code such as:
    Code:
    Sub Process_Globals
       
    Dim SMB1 As SMB
    End Sub

    Sub Globals

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    If FirstTime Then
          SMB1.Initialize(
    "SMB1")
       
    End If
       SMB1.ListFiles(
    "smb://USER-PC/Users/Public/""")
    End Sub

    Sub SMB1_ListCompleted(Url As String, Success As Boolean, Entries() As SMBFile)
       
    If Not(Success) Then 
          
    Log(LastException)
       
    Else
          
    For i = 0 To Entries.Length - 1
             
    Log("*****************")
             
    Log(Entries(i).Name)
             
    Log(Entries(i).Directory)
             
    Log(DateTime.Date(Entries(i).LastModified))
             
    Log(Entries(i).Parent)
             
    Log(Entries(i).Size)
          
    Next
       
    End If
    End Sub
    The Url can include all kinds of parameters. See this page for more information: JCIFS API

    The library is available here: http://www.basic4ppc.com/forum/additional-libraries-official-updates/17180-smb-library.html
     
  2. hackhack

    hackhack Active Member Licensed User

    Sweet!
     
  3. AscySoft

    AscySoft Active Member Licensed User

    thanks!
     
  4. galcott

    galcott New Member Licensed User

    Problem connecting to XP with SMB library

    I'm using the SMB library to try to connect to an XP machine but it fails every time. When I try to connect to a Windows 7 machine it does work if I specify a user name and password (using SetCredentials). I downloaded AndSMB, which is able to login to the XP machine using the "Anonymous" option, but not when I specify a username/password. It also logs into Windows 7 with the username/password. So I think the problem is I need to be able to do an anonymous login to XP with the SMB library but I don't know how, or even if it's possible. Can this be done?
     
  5. galcott

    galcott New Member Licensed User

    Ignore previous message

    You can ignore my previous message about the problem connecting anonymously to XP. I got it to work by using SetCredentials("","","").
    However, I only discovered that method by looking at the smb.xml file. Apparently it is necessary but is not included in your sample code.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. galcott

    galcott New Member Licensed User

    I see that it's listed there, but that doesn't make it clear that it's necessary for the connection to work. It should be included in the tutorial code.
     
  8. Damdu98

    Damdu98 Member Licensed User

    I copied both of the *.jar files and the *.xml file to B4As internal libraries folder and I immediately started a new project, but when I compile my Project, I always get an Error:
    Code:
    Compiling code.                         4.15
    Generating R 
    file.                      0.00
    Compiling generated Java code.          Error
    B4A line: 
    61
    If Entries(i).Directory Then
    javac 
    1.6.0_29
    ....\smbbrowser.java:
    349: cannot access jcifs.smb.SmbException
    class 
    file for jcifs.smb.SmbException not found
    if (_entries[_i].getDirectory()) { 
                                 ^
    1 error
    Code:
    Sub SMBC_ListCompleted (Url As String, Success As Boolean, Entries() As SMBFile)
       
    Log(URL & "->" & Success)
       
    ProgressDialogHide
       
    If Success Then
          lv.Clear
          
    For i=0 To Entries.Length-1
             
    Dim entry As SMBFile : entry = Entries(i)
             [B]
    If entry.Directory Then[/B]
                
    'lv.AddTwoLinesAndBitmap2(entry.Name, DateTime.Date(entry.LastModified) & " " & DateTime.Time(entry.LastModified), LoadBitmap(File.DirAssets, "ordner.png"), entry)
             Else
                
    'lv.AddTwoLinesAndBitmap2(entry.Name, entry.Size & " Bytes, " & DateTime.Date(entry.LastModified) & " " & DateTime.Time(entry.LastModified), LoadBitmap(File.DirAssets, "datei.png"), entry)
             End If
          
    Next
          NewURL(Url)
       
    Else
          
    If Msgbox2("Das Anfordern einer Liste mit Dateien auf der Freigabe ist fehlgeschlagen. Möchten Sie zum vorherigen Ordner zurück navigieren?",  "Fehler""Ja""""Nein"Null) = DialogResponse.POSITIVE Then
             ListF(
    settings.Get(global.Freigaben_KeyURL))
          
    Else
             
    StartActivity(Main)
             
    Activity.Finish
          
    End If
       
    End If
    End Sub
    The highlighted line is line 64.
    What's wrong with my code? :confused:
    I hope you can help me ;)

    EDIT: When I compile Erel's Code the same error happens at line 23
     
    Last edited: May 23, 2012
  9. Erel

    Erel Administrator Staff Member Licensed User

    Which version of Basic4android are you using?
     
  10. Damdu98

    Damdu98 Member Licensed User

    I'm using Version 1.70
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    For B4A v1.7 you will need to create a "dummy" xml file that will allow you to reference the native jar file. Create an xml file with this content:
    You should name it the same as the native jar file, without xml extension instead of jar. In the IDE you should then reference this library as well.

    This is not needed when working with B4A v1.8 or above.
     
  12. Damdu98

    Damdu98 Member Licensed User

    Thanks Erel, now I'm able to compile my project, but when I try the ListFiles-Command there is an Exception thrown:
    Code:
    (RuntimeException) java.lang.RuntimeException: Plain text passwords are disabled
    So how am I able to connect without a plain text password? Is there any option to connect with an encrypted password?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Have you called SMB.SetCredentials?
     
  14. Damdu98

    Damdu98 Member Licensed User

    Yes I've called
    Code:
    SMBC.SetCredentials(settings.Get(Global.Freigaben_KeyUsername), settings.Get(Global.Freigaben_KeyPassword), settings.Get(Global.Freigaben_KeyDomain))
    SMBC.Initialize(
    "SMBC")
    EDIT: Today I've bought Basic4Android 1.92 and now I get another exception instead of the old one:
    Code:
    (SmbException) jcifs.smb.SmbException: The network name cannot be found.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    The SMB library wasn't updated in v1.92. Make sure that there are no connection issues.

    The NTLM protocol is used with the username and password.
     
  16. Mike Henry

    Mike Henry Member Licensed User

    I have a question about Damdu98's code.
    Should JCIFS.jar be copied to the Basic4Android folder
    or the Android or JavaDK6 or JavaRE6 folder?
    All of those have a library folder that contains other jar files,
    and I would have guessed it belongs in the JavaDK6 folder.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    For some reason you appear as an unlicensed user in the forum. Please contact support@basic4ppc.com in order to fix it.
     
  18. dewif

    dewif Member Licensed User

    I want to check if the path in remote computer is exist. And then if it doesn't than make a directory. Is these can be done with SMB ?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    You can check if a folder exists by calling ListFiles (which also lists folders) on the parent folder.

    However there is no method available for creating a new folder.
     
  20. dmtulsa

    dmtulsa Member Licensed User

    great, fantastic.........Thank you

    Doug
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice