【问题标题】:How can I get the Bluetooth details in listview?如何在列表视图中获取蓝牙详细信息?
【发布时间】:2015-12-19 09:00:12
【问题描述】:

我创建了一个简单的蓝牙应用程序。点击listview时如何将配对设备的名称和地址等信息放入数组中?

public class BluetoothConnection extends Activity {

Button listbt;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;
ArrayList<String> list = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth);

    listbt=(Button)findViewById(R.id.listbutton);

    BA = BluetoothAdapter.getDefaultAdapter();
    lv = (ListView)findViewById(R.id.listView);

}

public void list(View v){
    pairedDevices = BA.getBondedDevices();

    for(BluetoothDevice bt : pairedDevices)
    {
        list.add(bt.getName() + "\n" + bt.getAddress());
    }
    Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show();

    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);
}
}

【问题讨论】:

  • 你现在遇到了什么问题。解释
  • 对这个列表(null); onCreate() 结束;
  • 我没有遇到问题。我想要实现的是获取值(配对的设备名称和地址)并在单击列表视图时将其存储到一个数组中。
  • 您是指在 Listview 项目上吗?
  • 是的。单击列表视图中的项目时。

标签: android listview bluetooth


【解决方案1】:

试试这个:

lv.setOnItemClickListener(mDeviceClickListener);

mDeviceClickListener 声明。

private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            // Get the device MAC address, which is the last 17 chars in the View
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);   
        }
    };

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多