简体   繁体   English

Android:如何将蓝牙连接传递给另一个活动?

[英]Android: How to pass a Bluetooth connection to another Activity?

I have my first Activity in which the BT connection is established. 我有我的第一个活动,其中建立了BT连接。 There is an option presented to the user and, based on their selection, a different Activity will load. 有一个选项呈现给用户,并根据他们的选择,加载不同的活动。

The problem is, both activities need a BT connection and I don't see the point in destroying one connection just to make another. 问题是,这两个活动都需要BT连接,而我没有看到破坏一个连接只是为了制造另一个连接。

Is there a way that I could pass the connection between Activities? 有没有办法可以传递活动之间的联系?

Does anyone have some example for me or perhaps a link? 有没有人为我或某个链接提供一些例子?

I've tried " class MyApplication extends Application ", but then I can't use: 我试过“ class MyApplication extends Application ”,但后来我不能使用:

super.onCreate(savedInstanceState);
setContentView(R.layout.blablabla);

This may be a pretty silly question but I've only been at Android +- 2 weeks. 这可能是一个非常愚蠢的问题,但我只是在Android + - 2周。

Have you tried using the Application object to store the Bluetooth connection in an object and using your Activities to get it? 您是否尝试使用Application对象将蓝牙连接存储在对象中并使用您的活动来获取它?

Try something like this. 尝试这样的事情。 (Note: I have never worked with Bluetooth on Android, so I don't know which relevant classes to use. In this case, I'll use BluetoothDevice , since it seems to be the right class based on the library documentation) (注意:我从未在Android上使用蓝牙,所以我不知道使用哪些相关类。在这种情况下,我将使用BluetoothDevice ,因为它似乎是基于库文档的正确类)

public class MyApplication extends Application {
    BluetoothDevice device;
    ...
    public synchronized BluetoothDevice getBtConnection() {
        if (device == null) {
            // construct a BluetoothDevice object and put it into variable device
        }
        return device;
    }
}

That way, your first activity just has to do this: 这样,你的第一个活动必须这样做:

public class FirstActivity extends Activity {
    private BluetoothDevice device;
    ...
    @Override
    protected void onCreate(Bundle b) {
        super(b);
        ...
        device = ((MyApplication) getApplication()).getBtDevice();
        ...
    }
    ...
}

And then, any time your other Activities need to use that connection, they just need to call getBtDevice() , because FirstActivity already instantiated it. 然后,只要你的其他活动需要使用该连接,他们只需要调用getBtDevice() ,因为FirstActivity已经实例化了它。

I know it's an old question, but for the new people visiting this topic: 我知道这是一个老问题,但对于访问此主题的新人来说:

I think Kibibyte's answer would also work, but otherwise there is the option to use a (Bound)Service . 我认为Kibibyte的答案也可行,但除此之外还有使用(Bound)Service的选项。 This would run even if the app closes 即使应用程序关闭,这也会运行

Official Android Service documentation 官方Android服务文档

Have you tried using a Bundle? 你尝试过使用Bundle吗?

Check relevant topic 检查相关主题

http://www.anddev.org/putting_an_object_into_a_bundle-t6431.html http://www.anddev.org/putting_an_object_into_a_bundle-t6431.html

i had same problem ,and finally solve it! 我有同样的问题,最后解决了! so at first you should create your connection in an activity and be sure that the connection store in public static variable and you can call that connection variable in each activity that you want to have Bluetooth Connection. 所以首先你应该在一个活动中创建你的连接,并确保连接存储在公共静态变量中,你可以在你希望拥有蓝牙连接的每个活动中调用该连接变量。 I suggest you to use service class to create connection and use connection variable like this 我建议你使用服务类来创建连接并使用这样的连接变量

BluetoothChatService mChatService=DeviceListActivity.chatService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM