Tuesday 6 December 2016

Sending files using Bluetooth from terminal to an Android phone

In the last post I described how to get Bluetooth up and running on a Linux machine all from the terminal. In this post I am going to describe one half of the most obvious use case of Bluetooth i.e. sending files from the Linux machine to another Bluetooth device e.g. a phone.
I am assuming that you have a Bluetooth adaptor running, if no then check the above mentioned post on how to do that. 
First thing you are going to do has little to do with your Linux machine. You need to make your phone a ftp server. Don't worry, it's not very fancy. An app installation will do just fine. The app I'd recommend for this is the Astro Bluetooth Module by Metago. Once the app is installed, open it and tick the following three things (you are gonna wanna do this every time you need to receive files):

  • Bluetooth
  • Discover-able
  • OBEX ftp server
OBEX is the Object Exchange profile that is designed specially for the transfer of files between two Bluetooth devices.
Now it is time to turn to the Linux machine and make sure it is ftp ready. First thing is to just update stuff just for good measure:

$ sudo apt-get update

Then you can go ahead and add obexftp:

$ sudo apt-get install obexftp

Now it is time to hunt for the phone you want to interact with. Use the following command to scan all nearby Bluetooth devices:

$ hcitool scan

It will start scanning like so:


Scanning ...
B4:CE:F6:B5:45:D7 Nexus 9
D8:5D:E2:A8:0B:B4 DIPTENDU-PC
5C:E0:C5:56:D3:CF PRIYARAN-E7450

You need to copy or at least make a note of the address of the device you wish to communicate with. Lets assume the address of the device is B4:CE:F6:B5:45:D7.

After this you're going to use an interactive tool provided to you already in order to do any interactions using Bluetooth. The tool is bluetoothctl and you can invoke it using the command:

$ bluetoothctl

Trust me and type the following four commands directly into the interactive session:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# scan on

Now connect to the device using its MAC address:

[bluetooth]# pair B4:CE:F6:B5:45:D7
[bluetooth]# trust B4:CE:F6:B5:45:D7

Obviously you'll get some kind of verification code on your phone to confirm this connection, accept the invitation. You have now paired with the phone. Without closing this terminal, open up another one. And type the following command:

$ obexftp -b B4:CE:F6:B5:45:D7 -p /path/to/file/to/be/send

From here you should get a prompt at your phone showing the transfer happening.
Once the file transfer is done, exit the terminal window and quite the interactive session by typing:

[bluetooth]# quit

To know more about these interactive commands, you can use 

[bluetooth]# help

1 comment:

  1. Casino Site - Lucky Club
    Lucky Club offers you the chance to play online blackjack, roulette, baccarat, slots luckyclub.live and more. The club offers a variety of Blackjack, Roulette,

    ReplyDelete