Jon Jon
Jon Jon

Reputation: 27

Android - Initiate a bluetooth connection with a known device

I read lots of things about bluetooth on the Android Developer website, but I can't understand one thing though, how do I start a connection with a paired (or bonded) device with his mac address that I got in a ListView. Just to see if my code worked (the OnItemClickListner part), I use a button to display the mac address. But what I want to do is using OnItemClickListener to initiate a connection with the selected Device. (I hope my explanations are clear)

Here is my code at least one part of it. http://pastie.org/4591835

Thing is I don't know how to use the rfcomm thingy in order to do what I want to do.

Could someone explain this to me please ?

The purpose of my code is to connect my android phone to my Arduino and send letters to it through some buttons eg : The button Led 1 would send "A" to my arduino and so on.

Thanks in advance !

    package com.jon.arduino.remote;

import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class NextActivity extends Activity {

    // Variables & constantes
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    private ArrayAdapter<String> deviceslist;
    public static String EXTRA_DEVICE_ADDRESS = "device_address";





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        setupUI();
        searchbtdevices();
    }


    /////////////////////////////////////////////////////////////////// 
    private void searchbtdevices() {


        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                deviceslist.add(device.getName() + "\n" + device.getAddress());
                                                         }
                                        } 


                           }


    /////////////////////////////////////////////////////////////////// 
    /*private void connecttobt() 
    {


    }*/


    /////////////////////////////////////////////////////////////////// 
    private void setupUI() {


        final Button btnmacaddress = (Button) findViewById(R.id.btnmacaddress);
        final ListView bt_deviceslist = (ListView) findViewById(R.id.bt_deviceslist);
        deviceslist = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        bt_deviceslist.setAdapter(deviceslist);


        bt_deviceslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               public void onItemClick(AdapterView<?> list, View v, int pos, long id) {

                   mBluetoothAdapter.cancelDiscovery();
                   String info = ((TextView) v).getText().toString();

                        // Attempt to extract a MAC address
                        String macaddress = info.substring(info.length() - 17);

                        // Create the result Intent and include the MAC address
                        Intent intent = new Intent();
                        intent.putExtra(EXTRA_DEVICE_ADDRESS, macaddress);

                        mBluetoothAdapter.cancelDiscovery();
                    //  connecttobt();



                        btnmacaddress.setText(macaddress);
                                                                                    }});                
                            }
    ///////////////////////////////////////////////////////////////////                                                                                 
}

Upvotes: 1

Views: 4241

Answers (1)

digitalhack
digitalhack

Reputation: 446

I have done something similar to what you want to do and have two examples that can be found at:

http://digitalhacksblog.blogspot.com/2012/05/arduino-to-android-basic-bluetooth.html

http://digitalhacksblog.blogspot.com/2012/05/arduino-to-android-turning-led-on-and.html

The first example shows how to send text from Android to an Arduino using a program called BlueTerm. This shows the basic setup for both the Android and Arduino to get them to communicate.

The second example is a program on Android that turns on and off LEDs on an Arduino. This shows how to communicated between the two devices from a program.

The examples have all the code necessary both for Android and the Arduino.

Hope this helps.

Upvotes: 2

Related Questions