All Projects → youlookwhat → BySMB

youlookwhat / BySMB

Licence: Apache-2.0 license
Android 通过SMB (Server Message Block),实现手机给电脑传输数据

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to BySMB

smaghetti
A web based level editor for Super Mario Advance 4, the GBA port of Super Mario Bros 3
Stars: ✭ 31 (+24%)
Mutual labels:  smb3
SMB3-Foundry
SMB3 Level Editor in Python
Stars: ✭ 72 (+188%)
Mutual labels:  smb3
ksmbd
ksmbd kernel server(SMB/CIFS server)
Stars: ✭ 181 (+624%)
Mutual labels:  smb3
ksmbd
ksmbd kernel server(SMB/CIFS server)
Stars: ✭ 98 (+292%)
Mutual labels:  smb3

BySMB

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

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

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.1.0'
}

2).开启联网权限

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

3).在Application初始化

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) {
            // 失败
        }
    })
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].