Search code examples
javaandroidbluetoothruntimeexceptionpairing

List Paired Bluetooth Devices


i'm trying to create a simple app in which I have a button in the MainActivity and a ListView in the PairingList activity. I want the button to enable bluetooth and then list all the paired devices in the listview in the other activity. However, I get a runtime exception as soon as I've enabled BT after I clicked the button. So far this is my code:

MainActivity.java

package com.gmburg.android.bluetoothservice;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    Button btOn;
    BluetoothAdapter btAdapter;
    int REQUEST_CODE = 1;
    Set<BluetoothDevice> paired_devices;
    String plist[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btOn = (Button) findViewById(R.id.btOn);

        btOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btAdapter = BluetoothAdapter.getDefaultAdapter();

                if (btAdapter == null) {
                    Toast.makeText(getBaseContext(), "Device has no bluetooth antenna", Toast.LENGTH_LONG).show();
                } else {
                    if (!btAdapter.isEnabled()) {
                        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(i, REQUEST_CODE);
                    }

                }
            }
        });


    }

    public void onActivityResult(int requestcode, int resultcode, Intent data) {

        if (requestcode == REQUEST_CODE) {
            if (resultcode == RESULT_OK) {
                Toast.makeText(getBaseContext(), "Bluetooth enabled", Toast.LENGTH_LONG).show();
                paired_devices = btAdapter.getBondedDevices();
                int count = paired_devices.size();
                plist = new String[count];
                int j = 0;
                for(BluetoothDevice device : paired_devices){
                    plist[j] = device.getName();
                    j++;
                    }
                 }
                Bundle btOn = new Bundle();
                btOn.putStringArray("paires", plist);
                Intent in = new Intent("pair_filter");
                in.putExtras(btOn);
                startActivity(in);

            }
        }
    }

PairingList.java

    package com.gmburg.android.bluetoothservice;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PairingList extends Activity {
    ListView lview;
    String[] pairs;
    ArrayAdapter<String> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pairinglist_layout);
        lview = (ListView) findViewById(R.id.listviewid);
        Bundle btOn = getIntent().getExtras();
        pairs = btOn.getStringArray("pairs");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pairs);
        lview.setAdapter(adapter);
    }
}

The logger gives me this:

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.gmburg.android.bluetoothservice/com.gmburg.android.bluetoothservice.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=pair_filter (has extras) }

All the help is appreciated!


Solution

  • You are getting below error

    android.content.ActivityNotFoundException: No Activity found to handle Intent { act=pair_filter (has extras) 
    

    this is because the intent you are trying to start activity for is not handled by any available activity/app in the phone. try resolveActivity method to find if any activity will be able to handle this, then only call startActivity/ForResult from your application.

    resolveActivity Method's docs