列出配对的蓝牙设备

List Paired Bluetooth Devices

我正在尝试创建一个简单的应用程序,其中我在 MainActivity 中有一个按钮,在 PairingList 中有一个 ListView activity。我希望按钮启用蓝牙,然后在另一个 activity 的列表视图中列出所有配对设备。但是,在单击按钮后启用 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) }

感谢所有帮助!

您遇到错误

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

这是因为您尝试启动 activity 的意图未被 phone 中的任何可用 activity/app 处理。尝试 resolveActivity 方法来查找是否有任何 activity 能够处理此问题,然后仅从您的应用程序调用 startActivity/ForResult

resolveActivity Method's docs