Coder Social home page Coder Social logo

bysmb's Introduction

BySMB

通过 SMB(Server Message Block),实现手机(Android)给电脑传输数据。

对应文章:Android SMB 简单几步实现手机给电脑传输数据

Java版本:BySMBJavaActivity.java

1.前提条件

  • 手机和电脑连接到同一局域网
  • 电脑需要设置用户名和密码
  • 设置共享文件夹 (smb://username:password@ip/folder。(登录鉴权))
    • Mac设置:系统偏好设置-共享-文件共享-添加共享文件夹
    • Windows设置:文件夹-共享-高级共享-权限-打开更改权限
  • 电脑不能息屏

设置共享文件夹:

Mac设置 Windows设置
Mac设置 Windows设置

2.代码配置

1).代码引入

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

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

2).开启联网权限

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

3).在使用时初始化,ActivityApplication

BySMB.initProperty()

4).得到SMB实例

val bySmb = BySMB.with()
        .setConfig(
                et_ip.text.toString(),       // ip
                et_username.text.toString(),// 用户名
                et_password.text.toString(),// 密码
                et_foldName.text.toString()// 共享文件夹名
        )
        .setReadTimeOut(60)
        .setSoTimeOut(180)
        .build()

查看ip:

  • Mac上查看ip:ifconfig | grep "inet"
  • Windows上查看ip:ipconfig

3.上传文件到电脑

fun upload(bySmb: BySMB) {
    // 生成文件 File
    val writeStringToFile = writeStringToFile(
            instance,
            et_content.text.toString(), // 文本内容
            et_fileName.text.toString()// 文件名,例如:随感笔记.txt
    )
    // 上传
    bySmb.writeToFile(writeStringToFile, object : OnOperationFileCallback {

        override fun onSuccess() {
            // 成功
        }

        override fun onFailure(message: String) {
            // 失败
        }

    })
}

注意:如上传相同文件名的文件,会覆盖之前文件的内容。

4.查找电脑上的文件列表

fun listFile(bySmb: BySMB){
    // 读取根目录下的所有文件,重载方法("", "*.txt", callback)
    bySmb.listShareFileName(object : OnReadFileListNameCallback {
        override fun onSuccess(fileNameList: List<String>) {
            // 读取成功 fileNameList文件名列表
        }

        override fun onFailure(message: String) {
             // 失败
        }
    })
}

5.删除电脑上的文件

fun deleteFile(bySmb: BySMB){
    bySmb.deleteFile(et_fileName.text.toString(), object : OnOperationFileCallback {
        override fun onSuccess() {
	    // 删除成功
        }

        override fun onFailure(message: String) {
            // 失败
        }
    })
}

bysmb's People

Contributors

youlookwhat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.