• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps (Android Studio)-Connecting to a bluetooth device and receiving data

I am Currently building an application using android studio. So far i managed to get the app to turn on the bluetooth and search for devices, but i still can't figure out how to connect to a my device.

This my connection.java code

Code:
****************************************

        import android.app.Activity;
        import android.bluetooth.BluetoothAdapter;
        import android.bluetooth.BluetoothDevice;

        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;

        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.ListView;

        import android.widget.Toast;
        import java.util.ArrayList;
        import java.util.Set;

public class connection extends AppCompatActivity  {
    Button b1,b2,b3,b4;
    private BluetoothAdapter BA;
    private Set<BluetoothDevice>pairedDevices;
    ListView lv;

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connection);

             b1 = (Button)findViewById(R.id.button);
             b2=(Button)findViewById(R.id.button2);
             b3=(Button)findViewById(R.id.button3);
             b4=(Button)findViewById(R.id.button4);

             BA = BluetoothAdapter.getDefaultAdapter();
             lv = (ListView)findViewById(R.id.listView);
    }

    public void on(View v){
        if (!BA.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
            Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
        }
    }

    public void off(View v){
    if (BA.disable());
        Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
    }


    public  void visible(View v){
        Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(getVisible, 0);
    }


    public void list(View v){
        pairedDevices = BA.getBondedDevices();

        ArrayList list = new ArrayList();

        for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
        Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

        final ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_list_item_1, list);

        lv.setAdapter(adapter);
    }
}

*****************************************************

And Also that's my activity_connection.xml code

*******************************************************

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"

    <TextView android:text="Connect to TANGO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:textSize="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#0d0d0d"
        android:textStyle="normal" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:theme="[USER=19691]@Style[/USER]/Base.TextAppearance.AppCompat" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn On"
        android:id="@+id/button"
        android:clickable="true"
        android:eek:nClick="on"
        android:layout_alignBottom="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/but"
        android:textColor="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get visible"
        android:eek:nClick="visible"
        android:id="@+id/button2"
        android:layout_alignBottom="@+id/button4"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="@color/but"
        android:textColor="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List devices"
        android:eek:nClick="list"
        android:id="@+id/button3"
        android:layout_below="@+id/textview"
        android:layout_toEndOf="@+id/textView2"
        android:layout_marginTop="49dp"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignStart="@+id/button2"
        android:layout_alignParentEnd="true"
        android:background="@color/but"
        android:textColorHighlight="#ffffff"
        android:textColor="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="turn off"
        android:eek:nClick="off"
        android:id="@+id/button4"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/but"
        android:textColor="#ffffff" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_marginTop="39dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Paired devices:"
        android:id="@+id/textView2"
        android:textColor="#276ddd"
        android:textSize="25dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/button4"
        android:layout_toEndOf="@+id/button4" />

</RelativeLayout>

******************************************************************

If anyone can give me some tips, that'll be great.
 
Back
Top Bottom