Jam*_*ame 9 sockets android bluetooth android-activity android-bluetooth
我正在制作一个使用蓝牙连接的应用程序.我打电话蓝牙连接onCreate()
和关闭它onDestroy()
的MainActivity
:
// Bluetooth
private Bluetooth bt;
private boolean registered = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = new Bluetooth(this);
bt.enableBluetooth();
bt.setCommunicationCallback(this);
bt.connectToName(bt.getBluetoothDevice(this));
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
registered = true;
}
@Override
public void onDestroy() {
super.onDestroy();
if(registered) {
unregisterReceiver(mReceiver);
registered=false;
}
bt.removeCommunicationCallback();
bt.disconnect();
if (handler != null && runnable != null) {
handler.removeCallbacks(runnable);
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序还支持LANDSCAPE和PORTRAIT模式(使用两种不同的布局).当屏幕旋转时,由于布局不同,MainActivity
调用onCreate()
和onDestroy
功能.出于这个原因,我收到以下错误:
@@@ ABORTING: INVALID HEAP ADDRESS IN dlfree addr=0x5a71aa38
A/libc: Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 1051 (le.bluetoothled)
Run Code Online (Sandbox Code Playgroud)
正如我在无效堆地址和致命信号11中发现的那样,它来自BluetoothSocket的close()
方法.首先,我认为旋转屏幕时我们不需要关闭蓝牙,因此,我尝试使用该方法来检测手机的旋转事件,并在旋转发生时忽略关闭,但是,它不起作用.因此,我认为我们可能需要在屏幕旋转时关闭蓝牙,但我得到了上述错误.我不知道如何解决这个问题,你能帮我解决这个问题吗?我正在使用此蓝牙库,disconnect()
如下所示:
public void disconnect() {
try {
socket.close();
} catch (IOException e) {
if(communicationCallback!=null)
communicationCallback.onError(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
因为我目前的解决方案是使用睡眠.我Thread.sleep (1000)
在关闭socket之前添加了.这是工作.但我认为这不是一个很好的解决方案.
归档时间: |
|
查看次数: |
579 次 |
最近记录: |