本文介绍了传递BluetoothDevice类对象与另一个活动虽然意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的蓝牙客户端和我有一个问题。
我的第一个活动显示启用ListView中的设备。当一些项目单击此列表中,它应该开始新的活动,并通过一个BluetoothDevice类对象那里。我写类似的东西:

I'm writing bluetooth client and I have a problem.My first activity display enabled devices in ListView. When click on some item on this list, it should start new activity and pass a BluetoothDevice object there. I write something like that:

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类对象传递给另一个活动?如何在新的活动中提取此对象吗?

Is it possible to pass BluetoothDevice object to another activity? How to extract this object in new activity?

对不起,我的英语。如果事情是不明确的,我会尝试更好地解释。

Sorry for my English. If something isn't clear, I will try explain better.

推荐答案

是的。这是可能的,因为 BluetoothDevice类工具 Parcelable

Yes . It is possible since BluetoothDevice class implements Parcelable

您可以在其他活动对象像这样

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

确认getIntent()。getExtras()不为null。

Make sure getIntent().getExtras() is not null

这篇关于传递BluetoothDevice类对象与另一个活动虽然意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:25