Android Tutorial Android SMB / CIFS tutorial

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.

SS-2012-04-23_11.39.04.png


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:
B4X:
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.b4x.com/forum/additional-libraries-official-updates/17180-smb-library.html
 

galcott

New Member
Licensed User
Longtime 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?
 

galcott

New Member
Licensed User
Longtime 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.
 

galcott

New Member
Licensed User
Longtime 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.
 

Damdu98

Member
Licensed User
Longtime 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:
B4X:
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

B4X:
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:

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<doclet-version-NOT-library-version>1.00</doclet-version-NOT-library-version>
<version>1.0</version>
</root>
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.
 

Damdu98

Member
Licensed User
Longtime User
Thanks Erel, now I'm able to compile my project, but when I try the ListFiles-Command there is an Exception thrown:
B4X:
(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?
 

Damdu98

Member
Licensed User
Longtime User
Yes I've called
B4X:
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:
B4X:
(SmbException) jcifs.smb.SmbException: The network name cannot be found.
 

Mike Henry

Member
Licensed User
Longtime 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.
 

dewif

Member
Licensed User
Longtime 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 ?
 
Top