从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android)
Posted
技术标签:
【中文标题】从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android)【英文标题】:Select a Bluetooth device from ListView and open a new Intent (Android) 【发布时间】:2014-10-25 04:36:15 【问题描述】:我正在做一个打开蓝牙并显示配对设备的 android 应用程序。 配对的设备显示在 ListView 中,但我还想单击特定的蓝牙设备(我们称之为 BTDEVICE03),如果设备与名称 BTDEVICE03 匹配,则打开一个新的 Intent。
可行吗?代码如下:
package com.example.homie;
import java.util.ArrayList;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity
private Button On,Off,Visible,list;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
On = (Button)findViewById(R.id.button1);
Off = (Button)findViewById(R.id.button2);
Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
lv = (ListView)findViewById(R.id.listView1);
BA = BluetoothAdapter.getDefaultAdapter();
public void on(View view)
if (!BA.isEnabled())
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(),"Turned on"
,Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),"Already on",
Toast.LENGTH_LONG).show();
public void list(View view)
pairedDevices = BA.getBondedDevices();
ArrayList<String> list = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
public void off(View view)
BA.disable();
Toast.makeText(getApplicationContext(),"Turned off" ,
Toast.LENGTH_LONG).show();
public void visible(View view)
Intent getVisible = new Intent(BluetoothAdapter.
ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return true;
【问题讨论】:
【参考方案1】:您必须添加一个 onClickListener 以在单击项目时执行操作。然后,您将不得不比较并启动意图。
public void list(View view)
pairedDevices = BA.getBondedDevices();
ArrayList<String> list = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
if (list.get(position).equals("BTDEVICE03")
Intent intent = new Intent(this, YourClassHere.class);
startActivity(intent);
);
【讨论】:
以上是关于从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android)的主要内容,如果未能解决你的问题,请参考以下文章