[英]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. 我正在尝试创建一个简单的应用程序,其中在MainActivity中有一个按钮,在PairingList活动中有一个ListView。 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.
但是,单击按钮后启用BT后,会立即收到运行时异常。 So far this is my code:
到目前为止,这是我的代码:
MainActivity.java 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 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! 感谢所有的帮助!
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方法来查找是否有任何活动能够处理此问题,然后仅从应用程序中调用
startActivity/ForResult
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.