通过意图将BluetoothDevice对象传递给另一个活动

时间:2014-04-12 01:29:47

标签: android android-intent android-activity bluetooth

我正在写蓝牙客户端,我遇到了问题。 我的第一个活动显示在ListView中启用了设备。单击此列表中的某个项目时,它应该启动新活动并在那里传递BluetoothDevice对象。我写了类似的东西:

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    if(btAdapter.isDiscovering()) {
        btAdapter.cancelDiscovery();
    }
    if(listAdapter.getItem(position).contains("Paired")) {

        BluetoothDevice selectedDevice = devices.get(position);
        Intent intent = new Intent (this, BTActivity.class);
        intent.putExtra("btdevice", selectedDevice);
        startActivity(intent);

是否可以将BluetoothDevice对象传递给另一个活动?如何在新活动中提取此对象?

抱歉我的英文。如果事情不清楚,我会尝试更好地解释。

1 个答案:

答案 0 :(得分:11)

是的。自BluetoothDevice课程implements Parcelable

以来,这是可能的

您可以像其他Activity一样获取该对象

BluetoothDevice bluetoothDevice = getIntent().getExtras().getParcelable("btdevice");

确保getIntent()。getExtras()不为空