使用蓝牙套接字处理方向变化的最佳方法
我正在创建一个具有蓝牙连接的应用程序。我创建了一个库,每次连接丢失/失败/建立/等时都会使用回调。有关连接的一切都工作正常,但我很难处理方向变化。我希望当设备改变方向时连接保持打开状态。同时我希望当用户关闭应用程序时关闭连接。我设计的 API 级别是 API 5-10。以下是我遇到的问题:
如果我在
onDestroy()
上关闭连接,则每次应用程序关闭时连接都会关闭,但应用程序只是暂停时(所需的输出)不会关闭。但是,只要应用程序方向发生变化,就会调用onDestroy()
。这会导致问题 #2。如果我使用
onRetainNonConfigurationInstance()
,我可以成功保持打开的套接字。如果我没有在 onDestroy() 中关闭套接字,那么一切都会正常工作。但是,当应用程序关闭时,套接字似乎保持打开状态(即其他设备仍在从中读取数据)。
我当前的设置看起来与此类似:
BluetoothConnection btConnection;
@Override
public void onCreate(Bundle icicle)
{
/** Activity setup **/
btConnection = (BluetoothConnection) getLastNonConfigurationInstance();
if(btConnection != null) // we already have a connection from a previous state, let's set it up to work with this state
{
/** Set up the connection since it already exists **/
}
}
@Override
public Object onRetainNonConfigurationInstance()
{
BluetoothConnection saveConnection = btConnection;
return saveConnection;
}
@Override
protected void onDestroy()
{
super.onDestroy()
if(btConnection != null)
btConnection.closeConnection();
}
使用当前的实现,套接字将被保存但同时关闭因为 onRetainNonConfigurationInstance()
以及 onDestroy()< /代码>。
我想我想知道两件事:
onRetainNonConfigrationInstance()
是处理这个问题的好方法吗?- 有没有更优雅的方法来保持连接打开直到应用程序关闭?我曾考虑过创建一个像 onRetainCalled 这样的布尔值来防止它关闭,但对我来说它看起来很难看(而且可能不可靠)。
I'm creating an app that has a bluetooth connection. I've created a library that uses callbacks every time a connection is lost/failed/established/etc. Everything about the connection works just fine, but I'm having a hard time handling orientation changes. I want the connection to remain open when the device changes orientation. At the same time I want the connection to close when the user closes the application. The API levels I'm designing this for are APIs 5-10. Here's the issues I come across:
If I close my connection on
onDestroy()
, the connection will close every time the app is closed but not when the app is simply paused (desired output). However,onDestroy()
is called whenever the application orientation changes. Which leads to issue #2.If I use
onRetainNonConfigurationInstance()
, I can successfully keep the open socket. If I don't close the socket inonDestroy()
, then everything works just fine. However, the socket seems to remain open when the app closes (i.e. the other devices are still reading from it).
The setup I currently have looks similar to this:
BluetoothConnection btConnection;
@Override
public void onCreate(Bundle icicle)
{
/** Activity setup **/
btConnection = (BluetoothConnection) getLastNonConfigurationInstance();
if(btConnection != null) // we already have a connection from a previous state, let's set it up to work with this state
{
/** Set up the connection since it already exists **/
}
}
@Override
public Object onRetainNonConfigurationInstance()
{
BluetoothConnection saveConnection = btConnection;
return saveConnection;
}
@Override
protected void onDestroy()
{
super.onDestroy()
if(btConnection != null)
btConnection.closeConnection();
}
With this current implementation, the socket will be saved but closed at the same time because both onRetainNonConfigurationInstance()
is called as well as onDestroy()
.
I guess I'd like to know two things:
- Is
onRetainNonConfigrationInstance()
a good way to handle this problem to begin with? - Is there a more elegant way to keep a connection open till an app closes? I've thought about creating a boolean like onRetainCalled to prevent the thing from closing, but it seems ugly to me (and possibly unreliable).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最佳情况下,您应该将蓝牙连接代码放入
Service
中:我使用这个,它非常出色。您可以打开多个 Activity,根本无需考虑。Optimally, you should put your Bluetooth connection code into a
Service
: I use this and it is quite excellent. You can open multiple Activities without having to think about it at all.