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

profile picture

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

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 https://bootstrap.pypa.io/get-pip.py

4. Execute pip installer sudo python get-pip.py

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

host = "DEVICE_LOCAL_IP"
port = 22
username = "root"
password = "password"

# Connect to Android Device
print 'Start Connexion...'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
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
sftp.close()
ssh.close()

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

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 = stdout.read().splitlines()

for afile in filelist:
    (head, filename) = os.path.split(afile)
    distantFiles.append(filename)
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 ;)

about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    18SY81ejLGFuJ9KMWQu5zPrDGuR5rDiauM

  • ETH

    ETH

    0x519e0eaa9bc83018bb306880548b79fc0794cd08

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

recent posts

QBUS - MQTT Quick Tutorial

1
10 months ago

DDOS By Web Crawlers

1
1 year ago

Something Given

1
1 year ago

2024 © My Dynamic Production SRL All rights Reserved.