Retieving files via bluetooth

Tutorials with advanced 'difficulty' and more Lines of Code.

Retieving files via bluetooth

Postby jonbonazza » Wed Jul 21, 2010 2:11 pm

I realize there is already a bluetooth tutorial on the site, however, this is a much more simplistic (read, "less involved") approach. The other method is probably better depending on your needs, but here is mine just in case it helps someone.

First off, this approach will only handle incoming files. Not outgoing.

It would probably be better to add some checks in here, but to keep the code simple and easy to understand to the average Joe, I have left them out.

The first thing you want to do is create your class variables that will be used to create a server socket.
For this, you will need one class variable and two class constants, a name for your connection, the UUID, and a BluetoothAdapter object. The name can be whatever you want. It doesn't matter. The UUID has to follow a specific protocol. There are various UUID generators out there, or you could just use the one I supplied below.
Code: Select all
private final String NAME = "MyName";
private final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66")
private  BluetoothAdapter blueoothAdapter = null;


The next thing to do is to get the default bluetooth adapter. To do this, in your overrided onCreate() method, put the following:
Code: Select all
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


Next, we create a new thread that will establish a server socket.

This thread should look something like this:
Code: Select all
private class AcceptThread implements Runnable
    {
       private BluetoothServerSocket serverSocket;
       
       public AcceptThread()
       {
          //Temporary swap server socket for conditional
          BluetoothServerSocket tmp = null;
          
          try
          {   
             tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("MYNAME", MY_UUID);
          }
          catch(IOException e)
          {
             //handle exceptions here
          }
          //Swap server sockets
          serverSocket = tmp;
       }
       
       public void run()
       {
               //Handle any non UI thread taks here and send a message back to the UI thread to handle UI activities
               mHandler.sendEmptyMessage(0);
       }
    }


Next we need to start this thread as soon as the activity is created. To do this, add the following line at the end of the onCreate() method.
Code: Select all
Thread acceptThread = new Thread(new AcceptThread());
acceptThread.start();


Also, we have the handler that is referenced in this thread.

Code: Select all
private final Handler handler = new Handler()
    {
       @Override
       public void handleMessage(Message msg)
       {
          //Handle any UI changes here.
       }
    };


That's really it!

making a bluetooth connection and receiving files isn't as intimidating as it first may seem!
Last edited by jonbonazza on Thu Jul 22, 2010 11:21 pm, edited 1 time in total.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Re: Retieving files via bluetooth

Postby jonbonazza » Wed Jul 21, 2010 5:23 pm

Fixed a very stupid but major typo in my code. Should work fine now.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests