您是否尝试使用Application对象将蓝牙连接存储在对象中并使用您的活动来获取它?
尝试这样的事情. (注意:我从未在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;
}
}
这样,你的第一个活动必须这样做:
public class FirstActivity extends Activity {
private BluetoothDevice device;
...
@Override
protected void onCreate(Bundle b) {
super(b);
...
device = ((MyApplication) getApplication()).getBtDevice();
...
}
...
}
然后,只要你的其他活动需要使用该连接,他们只需要调用getBtDevice(),因为FirstActivity已经实例化了它.