Android SMB realizes data transmission from mobile phone to computer in a few simple steps

Keywords: Java Android network

What is SMB

SMB, namely "Server Message Block" server information block, is a network file sharing protocol. It can be used for Web connection and information communication between client and server, allowing applications and end users to access file resources from remote file servers.

SMB communication protocol is an agreement formulated by Microsoft and Intel in 1987. It is mainly used as the communication protocol of Microsoft network. SMB is in the session layer and Presentation layer (presentation layer) and small parts application layer (application layer).

It can simply transfer files to the computer, but SMB is not very safe:
The mechanism of controlling file security transmission in SMB protocol is to use client authentication. In this way, the client sends the authentication password to the server to obtain the permission of file transmission. However, the network attack against this mechanism is relatively serious. The attack program steals the access right of the file by intercepting the authentication password, The security of file transmission under LAN can not be guaranteed.

Usage scenario

Generally, this method is not used to realize specific functional requirements. Some special scenarios will be used, such as:

  1. Some Wallpaper software on mobile phones have horizontal wallpaper, but the horizontal wallpaper is generally used on computers. If SMB technology is used, the pictures can be directly transmitted to the folder of the computer without downloading them locally and then transmitting them to the computer through wechat / QQ, which is much more convenient.

  2. Another example is that sometimes we have a large apk package. The suffix will be changed when it is transmitted to the computer through wechat. QQ may have large file restrictions. At this time, we can also consider using SMB to solve it. After all, the transmission code written by ourselves is forced to be filled directly 🤡.

  3. In addition, you want to directly reflect the data operated in the equipment on the computer for viewing, such as a vision detection tool at the tablet end. After detection, you can directly reflect the data on the computer for staff to use.

How to use

1. Preconditions

  • The mobile phone and computer are connected to the same LAN
  • The computer needs to set a user name and password
  • Set up shared folders( smb://username:password@ip/folder. (login authentication)
    • Mac settings: System Preferences - share - file share - add shared folder
    • Windows settings: folder - share - Advanced share - permissions - open change permissions
  • The computer can't stop the screen

Set up shared folders:

Mac settingsWindows settings

2. Code configuration

See GitHub for specific source code: BySMB

1) . code introduction

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
    implementation 'com.github.youlookwhat:BySMB:1.1.0'
}

2) . enable networking permission

<uses-permission android:name="android.permission.INTERNET" />

3) . initialize in Application

BySMB.initProperty()

4) . get SMB instance

val bySmb = BySMB.with()
        .setConfig(
                et_ip.text.toString(),       // ip
                et_username.text.toString(),// user name
                et_password.text.toString(),// password
                et_foldName.text.toString()// Shared folder name
        )
        .setReadTimeOut(60)
        .setSoTimeOut(180)
        .build()

View ip:

  • View ip: ifconfig | grep "inet" on Mac
  • View ip: ipconfig on Windows

3. Upload files to the computer

fun upload(bySmb: BySMB) {
    // Generate File
    val writeStringToFile = writeStringToFile(
            instance,
            et_content.text.toString(), // Text content
            et_fileName.text.toString()// File name, for example: notes.txt
    )
    // upload
    bySmb.writeToFile(writeStringToFile, object : OnOperationFileCallback {

        override fun onSuccess() {
            // success
        }

        override fun onFailure(message: String) {
            // fail
        }

    })
}

Note: if you upload a file with the same file name, the contents of the previous file will be overwritten.

4. Find the list of files on your computer

fun listFile(bySmb: BySMB){
    // Read all files in the root directory and overload the method ("" "*. txt", callback)
    bySmb.listShareFileName(object : OnReadFileListNameCallback {
        override fun onSuccess(fileNameList: List<String>) {
            // Successfully read fileNameList file name list
        }

        override fun onFailure(message: String) {
             // fail
        }
    })
}

5. Delete files on your computer

fun deleteFile(bySmb: BySMB){
    bySmb.deleteFile(et_fileName.text.toString(), object : OnOperationFileCallback {
        override fun onSuccess() {
	    // Delete succeeded
        }

        override fun onFailure(message: String) {
            // fail
        }
    })
}

epilogue

SMB is generally unavailable, but some users who need it have encountered a lot of confusion when doing it before, and many materials are in foreign languages. I hope this article will be helpful to some people. Attach the source code address, GitHub: BySMB.

Relevant information

Posted by puzzle on Thu, 28 Oct 2021 10:09:08 -0700