gpt4 book ai didi

android - 从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android)

转载 作者:太空狗 更新时间:2023-10-29 12:41:08 25 4
gpt4 key购买 nike

我正在开发一个 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;
}

}

最佳答案

您必须添加一个 onClickListener 以在单击某个项目时起作用。然后,您将必须比较并启动 Intent 。

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

关于android - 从 ListView 中选择一个蓝牙设备并打开一个新的 Intent (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com