Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:17:20+00:00 2026-06-07T11:17:20+00:00

I’m trying to make a java android application with bluetooth and I have some

  • 0

I’m trying to make a java android application with bluetooth and I have some troubles to show the result after search all the devices paired or not in a listview and to connect to a device, in my code I’m trying to connect with a device using the mac addres of it, but it doesn’t work and I show the result in a textview, any idea????
Thank you.

public class Conexion_bluetooth extends Activity {
TextView out; 

// Debugging
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket mmSocket=null;
private static final String TAG = "BluetoothChat";
private static final boolean D = true;

private static final int REQUEST_ENABLE_BT = 3;

private BluetoothAdapter mBluetoothAdapter = null;

ListView lstDispositivos;
ListView lstDispositivos2;

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     if(D) Log.e(TAG, "+++ ON CREATE +++");

     // Set up the window layout
     setContentView(R.layout.activity_conexion_bluetooth);

     // Get local Bluetooth adapter
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

     // If the adapter is null, then Bluetooth is not supported
     if (mBluetoothAdapter == null) {
         Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
         finish();
         return;
     }
 }

@Override
//Comprueba que el bluetooth esta conectado
public void onStart() {
    super.onStart();
    if(D) Log.e(TAG, "++ ON START ++");

    // Si el bluetooth no está activado muestra un mensaje solicitando permiso para activarle
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        Toast.makeText(this, "conectando...", Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(this, "conectado", Toast.LENGTH_LONG).show();
    }
}

public void buscar(View view){
    //TextView dispositivos = (TextView) findViewById(R.id.dispositivos);


    if (mBluetoothAdapter.isDiscovering()) {
        Toast.makeText(this, "\nCancel discovery...", Toast.LENGTH_LONG).show();
        mBluetoothAdapter.cancelDiscovery(); 
    }else{
        Toast.makeText(this, "\nStarting discovery...", Toast.LENGTH_LONG).show();
        //mBluetoothAdapter.
        mBluetoothAdapter.startDiscovery(); 
        Toast.makeText(this, "\nDone with discovery...", Toast.LENGTH_LONG).show();
    }

    final TextView dispositivos = (TextView) findViewById(R.id.dispositivos);
    String dispositivo[] = new String[]{};
    final Vector<String> dispositivosBT = new Vector<String>();
    //final int dispEncontrados=0;

    // Create a BroadcastReceiver for ACTION_FOUND
    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            //String dispositivosBT[] = new String[]{};
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                //setContentView(R.layout.dispositivos_bluetooth);//determinamos que al iniciar la aplicacion aparezca esta pantalla
                    // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    //  Add the name and address to an array adapter to show in a ListView
                    dispositivos.append("\n" + device.getName() + "\n" + device.getAddress());
            }
        }
    };
    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

    //dispositivosBT = new String[dispositivos.length()];
    //final ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, dispositivosBT);
    final TextView dispositivosSincronizados = (TextView) findViewById(R.id.dispositivosSincronizados);
    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
            dispositivosSincronizados.append("\n" + device.getName() + "\n" + device.getAddress());
        }
    }
}
public void paraBusqueda(View view){
    if (mBluetoothAdapter.isDiscovering()) {
        Toast.makeText(this, "\nCancel discovery...", Toast.LENGTH_LONG).show();
        mBluetoothAdapter.cancelDiscovery();
    }else{
        Toast.makeText(this, "\nis not discovering...", Toast.LENGTH_LONG).show();
    }

}

public void pararBluetooth(){
    if (mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);        
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_conexion_bluetooth, menu);
    return true;
}

public void conectar(View view){
    String address = "00:15:83:07:d1:14";
 device = mBluetoothAdapter.getRemoteDevice(address);
 Toast.makeText(getApplicationContext(), "cONNECTING", Toast.LENGTH_SHORT).show();
 try {

 mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
 mmSocket.connect();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:17:22+00:00Added an answer on June 7, 2026 at 11:17 am

    Using AsyncTask we can make Bluetooth Connection.

     public class BluetoothConnectionActivity extends Activity {
    
        private Button connectButton;
    
        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
                connectButton = (Button) findViewById(R.id.connectButton);
                connectButton.setOnclickListener(View.onClickListener() {
                            @Override
                        public void onClick(View v) {
                               String address = "00:15:83:07:d1:14";
                       new ConnectAsyncTask(address).execute();
                        }
                        });
    
    
            }
    
        private class ConnectAsyncTask extends AsyncTask<String, Void, Boolean> {
            @Override
            protected void onPostExecute(Boolean isConnected) {
                Toast.makeText(getApplicationContext(), "Connecting...",
                        Toast.LENGTH_SHORT).show();
            }
    
            @Override
            protected Boolean doInBackground(String... param) {
                BluetoothDevice device = BluetoothAdapter
                        .getDefaultBluetoothAdapter().getRemoteDevice(param[0]);
                try {
                    BluetoothSocket mmSocket = device
                            .createRfcommSocketToServiceRecord(MY_UUID);
                    mmSocket.connect();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("BluetoothConnectivity", "ERROR:" + e.getMessage());
                    return false;
                }
                return true;
            }
    
            @Override
            protected void onPostExecute(Boolean isConnected) {
                if (isConnected) {
                    Toast.makeText(getApplicationContext(), "Connected",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Failed to connected",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    

    Regards
    Srini

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
Thanks in advance for your help. I have a need within an application to
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.