Transfer files from a Raspberry Pi to your android phone over WiFi with Python

Transfer files from a Raspberry Pi to your android phone over WiFi with Python

Nov 21 2016, 19:00 in Domotic, Web Development

Today I'll show a bit of python that could help you remotely manage your Android device. The goal here is to setup an SSH connection between a Raspberry Pi and an Android device in order to transfer files from your local environment (a NAS for example) to your device.

In my case, I've setup a script that will pick a random music on my phone and delete it, then replace it with a music randomly picked up from my music library. The purpose is to always have new music automatically on my phone without doing anything.

We will see here how to connect to the phone and get/delete/put files on the device using a Python script.

The first thing to do is to install an SSH server on your phone

That is way more easy than you think it is ! Download and install QuickSSHd on your device. This simple app will setup an SSH server on your phone.

After the app is installed, be sure to enable/disable these settings:

  • Start at launch : enabled
  • Active Screen : disabled
  • Mandatory WiFi : enabled
  • Keep WiFi : enabled
  • Auto Start on WiFi : enabled
  • Add your home WiFi in the Whitelist
  • Notification Icon : disabled
  • Setup your password

Now once the app is started and the server is running, take note of the connection information (IP, port, username, password).

With your favourite SSH client, you can now try to access your device and browse through your files.

Make your environment ready (Raspberry Pi or any Linux System)

First of all be sure to have Python installed, alongside with the paramiko library. As simple as saying "FooBar" !

1. Fetch latest packages sudo apt-get update

2. Install Python sudo apt-get install python

3. Download pip installer wget

4. Execute pip installer sudo python

5. Install paramiko library sudo pip install paramiko

Establish a connection with a simple python script

Create a new python file with whatever name you prefer, and put these lines into it:

import paramiko, os

port = 22
username = "root"
password = "password"

# Connect to Android Device
print 'Start Connexion...'
ssh = paramiko.SSHClient()
ssh.connect(host, port=port, username=username, password=password)
sftp = ssh.open_sftp()
print 'Connected.'

# Here you will add all scripts you need to run !
# ...
# ...
# ...
# ...

# End Script

Be sure to set your own connection information (host, port, username, password) and then try to execute the script python

Now that you are succesfully connected from your Pi to your Android, let's see what we can do!

List all mp3 files on your Android device

# List the distant files
print 'Listing all mp3 from distant device Music folder...'
distantFiles = list()
filePath = '/storage/emulated/0/Music'
filePattern = '"*.mp3"'
rawcommand = 'find {path} -name {pattern}'
command = rawcommand.format(path=filePath, pattern=filePattern)

stdin, stdout, stderr = ssh.exec_command(command)
filelist =

for afile in filelist:
    (head, filename) = os.path.split(afile)
print distantFiles

Delete a file from your Android device

# Deleting Distant File
sftp.remove('/storage/emulated/0/Music/Avicii - Wake Me Up.mp3')
print 'The file has been deleted'

Transfer File from Raspberry => Android device

# Transfering a file to Distant Device
sftp.put('/home/pi/mount/NAS/Music/Avicii - Wake Me Up.mp3', '/storage/emulated/0/Music/Avicii - Wake Me Up.mp3') # You need to re-type the name of the file in the destination
print 'The file has been transfered to your device'

Transfer File from Android device => Raspberry

# Transfering a file to Distant Device
sftp.get('/storage/emulated/0/Music/Avicii - Wake Me Up.mp3', '/home/pi/mount/NAS/Music/Avicii - Wake Me Up.mp3') # You need to re-type the name of the file in the destination
print 'The file has been downloaded'

Now that you are familiar with paramiko and these simple method, you are able to build whatever you need ;)