我在我的应用程序中使用蓝牙连接热敏打印机。首先,我检查蓝牙是否启用,一切正常。但是当我在应用程序中关闭蓝牙时,我遇到了问题。我的应用程序崩溃时出现空指针异常,该异常未在BluetoothService
类中处理。
此外,我注意到即使我不在我的应用程序中并关闭蓝牙,它也会调用我的应用程序并向我显示来自我的代码的祝酒消息。
任何人都面临这个问题?这是来自蓝牙服务器套接字的空指针异常
03-03 17:13:27.576 9916-10431/co.lvlz.skiplino.provider E/AndroidRuntime: FATAL EXCEPTION: AcceptThread Process: co.lvlz.skiplino.provider, PID: 9916
java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket
android.bluetooth.BluetoothServerSocket.accept()' on a null object reference at com.zj.btsdk.BluetoothService$AcceptThread.run(BluetoothService.java:316)
此处处理蓝牙以及如何销毁活动
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED:
Toast.makeText(getApplicationContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
break;
case BluetoothService.STATE_CONNECTING:
Log.d("��������","��������.....");
break;
case BluetoothService.STATE_LISTEN:
case BluetoothService.STATE_NONE:
Toast.makeText(getApplicationContext(), "bluetooth is closed",
Toast.LENGTH_SHORT).show();
Log.d("None", "stateNone");
MainActivity.this.finish();
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST:
Log.d("None", "connection lost to device");
Toast.makeText(getApplicationContext(),"Connetion was lost to device",
Toast.LENGTH_SHORT).show();
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT:
Toast.makeText(getApplicationContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}
};
protected void onDestroy() {
super.onDestroy();
if (mService != null)
{
Log.d("None", "nullMe");
activitydestroyed=true;
mService.stop();
}
else {
Log.d("None", "nullMe2");
mService = null;
}
}