简体   繁体   English

保持连接状态?

[英]Keep connection alive?

In bluetooth chat example, I want to add another activity. 在蓝牙聊天示例中,我想添加另一个活动。 When the connection is made and when the new activity is loaded, how do I still keep the connection alive?? 建立连接时以及加载新活动时,如何仍保持连接活动?

After I load new activity, the onStop method is called and I canned send message using the connection made before. 加载新活动后,将调用onStop方法,并使用之前建立的连接来固定发送消息。 I tried the following code in my new activity: 我在新活动中尝试了以下代码:

    BluetoothChat bt = new BluetoothChat();        
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.special_keys);
    bt.sendMessage("hello");
    }

I manage to send the 'hello' but it force closes after that.. There must be an efficient way to handle this situation. 我设法发送了“ hello”,但此后它强制关闭了。必须有一种有效的方法来处理这种情况。 Please help. 请帮忙。

Thanks in advance. 提前致谢。

I have recently created an Android application that communicates to a hardware device over Bluetooth. 我最近创建了一个Android应用程序,可通过蓝牙与硬件设备进行通信。 In my application, I have many Activity classes that communicate through the connection, and the application also performs continuous data logging from the connection in the background, too. 在我的应用程序中,我有许多通过连接进行通信的Activity类,并且该应用程序还从后台在连接中执行连续的数据记录。 Therefore, hopefully I might be able to help here. 因此,希望我可以在这里提供帮助。 (This is my first attempt at posting an answer on StackOverflow, so go easy on me.) (这是我第一次尝试在StackOverflow上发布答案,所以请放轻松。)

In my application, I have first of all put all of the Bluetooth code into a separate dedicated class file (which I call BluetoothIF.java). 在我的应用程序中,我首先将所有蓝牙代码放入一个单独的专用类文件(我称为BluetoothIF.java)中。 The constructor in this class performs basic adapter initialization, and further methods are provided for tasks like making connections, etc. It contains the Runnable classes for establishing a connection and then transmitting / receiving over the established connection. 此类中的构造函数执行基本的适配器初始化,并为诸如建立连接等任务提供其他方法。它包含Runnable类,用于建立连接,然后通过已建立的连接进行发送/接收。 (Essentially it is based upon the BluetoothChat example.) (本质上是基于BluetoothChat示例。)

I don't need to perform a massive amount of communication over the Bluetooth connection, and I also needed to have a Service in the background to continuously monitor and log data from the connection as previously mentioned. 我不需要通过蓝牙连接进行大量的通信,而且我还需要在后台设置一个服务来连续监视和记录来自连接的数据,如前所述。 So, I instantiated my BluetoothIF within the Service. 因此,我在服务中实例化了BluetoothIF。 Any Activity classes that need to exchange data over the existing Bluetooth connection do so in my application by exchanging messages with the Service. 在我的应用程序中,任何需要通过现有蓝牙连接交换数据的Activity类都通过与服务交换消息来进行交换。

Alternatively, if you have no need for using a Service, perhaps you should share the Bluetooth object instance across Activitys using the singleton model or by extending the Application class. 另外,如果您不需要使用服务,则也许应该使用单例模型或通过扩展Application类在Activity中共享Bluetooth对象实例。

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

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