Ror*_*pin 5 android bluetooth android-intent onpause android-activity
我正在构建一个通过蓝牙与Arduino板通信的Android应用程序,我将蓝牙代码放在一个名为BlueComms的类中.要连接到设备,我使用以下方法:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这很好连接,但是一旦我离开我通过它连接的活动就会丢弃连接,通过LogCat显示,
D/dalvikvm(21623): GC_CONCURRENT freed 103K, 10% free 2776K/3056K, paused 5ms+2ms, total 35ms
Run Code Online (Sandbox Code Playgroud)
我不能再连接到设备,但如果我调用killBt()它会抛出一个致命的错误,如果我尝试发送数据,我会收到'Socket creation failed'错误.我的发送消息代码如下:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我切换不同的活动时,我应该如何防止连接被我连接的活动暂停,我正在使用以下代码切换活动:
Intent myIntent = new Intent(v.getContext(), Timelapse.class);
startActivityForResult(myIntent, 0);
Run Code Online (Sandbox Code Playgroud)
非常感谢,Rozz
Mar*_*igo 11
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)
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.
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.
Extend Application
import android.app.Application;
public class cBaseApplication extends Application {
public BlueComms myBlueComms;
@Override
public void onCreate()
{
super.onCreate();
myBlueComms = new BlueComms();
}
}
Run Code Online (Sandbox Code Playgroud)
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" >
Run Code Online (Sandbox Code Playgroud)
Access the base app from any of your Activities like this
((cBaseApplication)this.getApplicationContext()).myBlueComms.SomeMethod();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17152 次 |
| 最近记录: |