通过多个活动保持android蓝牙连接 [英] Holding android bluetooth connection through multiple activities

查看:32
本文介绍了通过多个活动保持android蓝牙连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个通过蓝牙与 Arduino 板通信的 Android 应用程序,我在一个名为 BlueComms 的类中拥有蓝牙代码.要连接到设备,我使用以下方法:

I am building an Android app that communicates with an Arduino board via bluetooth, I have the bluetooth code in a class of it's own called BlueComms. To connect to the device I use the following methord:

public boolean connectDevice() {
    CheckBt();
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    Log.d(TAG, "Connecting to ... " + device);
    mBluetoothAdapter.cancelDiscovery();
    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        btSocket.connect();
        outStream = btSocket.getOutputStream();
        Log.d(TAG, "Connection made.");
        return true;

    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
            Log.d(TAG, "Unable to end the connection");
            return false;
        }
        Log.d(TAG, "Socket creation failed");
    }
    return false;

}
    private void CheckBt() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!mBluetoothAdapter.isEnabled()) {
        System.out.println("Bt dsbld");
    }

    if (mBluetoothAdapter == null) {
        System.out.println("Bt null");
    }
}

这可以正常连接,但是一旦我离开我通过它连接的活动就会断开连接,通过 LogCat 显示这一点,

This connects fine but as soon as I leave the activity I connected through it drops the connection, showing this through LogCat,

 D/dalvikvm(21623): GC_CONCURRENT freed 103K, 10% free 2776K/3056K, paused 5ms+2ms, total 35ms

我无法再连接到设备,但是如果我调用 killBt() 它会抛出一个致命错误,如果我尝试发送数据,我会收到套接字创建失败"错误.我的发送消息代码如下:

I can no longer connect to the device, but if I call killBt() it throws a fatal error and if I try to send data I get a 'Socket creation failed' error. My send message code is as follows:

public void sendData(String data, int recvAct) {
    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        Log.d(TAG, "Bug BEFORE Sending stuff", e);
    }

    String message = data;
    byte[] msgBuffer = message.getBytes();

    try {
        outStream.write(msgBuffer);
    } catch (IOException e) {
        Log.d(TAG, "Bug while sending stuff", e);
    }
}

当我切换不同的活动时,我应该如何防止连接被我连接的活动暂停,我正在使用以下代码切换活动:

How should I go about preventing the connection from being paused by the activity I connect with when I switch a different activity, I am switching activities with this code:

Intent myIntent = new Intent(v.getContext(), Timelapse.class);
    startActivityForResult(myIntent, 0);

非常感谢,罗兹

推荐答案

您在哪里存储 BlueComms 类的实例?如果你把它放在第一个活动中,那么当你离开它并移动到下一个活动时该活动被破坏时,类实例就会被杀死(NB活动也会在屏幕旋转时被破坏)

Where did you store the instance of your BlueComms class? If you put it in the first activity then the class instance would have been killed when that activity was destroyed as you left it and moved to the next activity (NB activities also get destroyed on screen rotation)

因此,您需要找到一种方法,使 BlueComms 类的实例在需要时一直保持活动状态.您可以通过公共属性在活动之间传递它,并在轮换期间将其存储在 onRetainNonConfigurationInstance() 中.

So you need to find a way to keep the instance of BlueComms class alive for as long as you need it. You could pass it between activities via public properties and store it in onRetainNonConfigurationInstance() during rotations.

一个更简单的技巧是创建一个扩展 Application 的类,将其用作应用程序的应用程序委托,并向其添加公共属性以在其中存储 BlueComms 类的实例.这样,BlueComms 类的实例将在您的应用程序的整个生命周期内都处于活动状态.

An easier trick is to create a class that extends Application use it as the application delegate for your app and add public property to it to store the instance of BlueComms class within it. That way the instance of BlueComms class would be alive for the lifetime of you app.

扩展应用

import android.app.Application;

public class cBaseApplication extends Application {

    public BlueComms myBlueComms;

    @Override
    public void onCreate() 
    {
        super.onCreate();
        myBlueComms = new BlueComms();
    }

}

使您的类成为应用清单中的应用委托

Make your class the application delegate in the app manifest

<application
    android:name="your.app.namespace.cBaseApplication"
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

从您的任何活动访问基本应用程序

Access the base app from any of your Activities like this

((cBaseApplication)this.getApplicationContext()).myBlueComms.SomeMethod();

这篇关于通过多个活动保持android蓝牙连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆