通过举办多种活动的Andr​​oid蓝牙连接 [英] Holding android bluetooth connection through multiple activities

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

问题描述

我建立一个Android应用程序,它通过蓝牙与Arduino板通信,我有一个类它自己叫BlueComms的蓝牙code。要连接到我用下面的基准值确定方法的设备:

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()它抛出一个致命的错误,如果我尝试发送数据,我得到一个套接字创建失败的错误。我发送消息code是如下:

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);
    }
}

我应该如何去preventing从我当我切换不同的活动连接活动被暂停的连接,我交换活动,这code:

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);

非常感谢,
ROZZ

Many Thanks, Rozz

推荐答案

你在哪里存储您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.

这是比较容易的技巧是创建一个扩展应用使用它作为应用程序委托为您的应用程序和公共属性添加到它存储在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();

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

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