列出配对的蓝牙设备

时间:2016-01-14 22:10:32

标签: java android bluetooth runtimeexception pairing

我试图创建一个简单的应用程序,其中我在MainActivity中有一个按钮,在PairingList活动中有一个ListView。我想按钮启用蓝牙,然后列出其他活动列表视图中的所有配对设备。但是,在我点击按钮后,只要我启用了BT,就会收到运行时异常。到目前为止,这是我的代码:

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);
    }
}

记录器给了我这个:

    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) }

感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

您收到以下错误

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

这是因为您尝试启动活动的意图不会由手机中的任何可用活动/应用处理。尝试使用resolveActivity方法查找是否有任何活动可以处理此问题,然后只从您的应用程序中调用startActivity/ForResult

resolveActivity Method's docs