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

use*_*593 5 android bluetooth android-intent android-activity

我正在写蓝牙客户端,我有一个问题.我的第一个活动显示在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);
Run Code Online (Sandbox Code Playgroud)

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

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

Lib*_*bin 11

是的 这是可能的,因为BluetoothDevice上课implements Parcelable

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

BluetoothDevice bluetoothDevice = getIntent().getExtras().getParcelable("btdevice");
Run Code Online (Sandbox Code Playgroud)

确保getIntent().getExtras()不为null