How to make the perfect Singleton to handle Bluetooth services?












0














I'm creating e bill printing application for Android mobile devices. I have successfully connected application and small Bluetooth POS printer. I can also print a bill through this.



I have used fragments for this application and all fragments generating through main activity.



My question is, if I change the fragment and press the print button it will ask to connect with printer again. I need to avoid that process, because once user hit the print button in the previous fragment (this works if user print something using that fragment, otherwise it should ask here to connect with printer) he/she is already connected to a printer.



The problem occurs here when I enter the new fragment, then that fragment creates a new object for handler, I need to put that handler as a singleton. This handler is used to connect with printer through Bluetooth. I have tried a few solutions, but I was failed. Is there any way to do this. Please help me to find a better solution for this problem. Thanks in advance.



here is the code --
Handler and fragment's onCreateView,



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(getContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
isDeviceConnected = true;

// updateConnectBtn();
// btnClose.setEnabled(true);
// btnSendDraw.setEnabled(true);
Log.v("Pasindu", "Connected!");
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������", "��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������", "�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
isDeviceConnected = false;
// btnClose.setEnabled(false);
// btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�޷������豸
Toast.makeText(getContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}

};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.data_fragment, container, false);
btn_proceed = (Button) view.findViewById(R.id.btn_proceed);
mService = new BluetoothService(getContext(), mHandler);
cmd = new byte[3];
isDeviceConnected = ((MainActivity)this.getActivity()).isDeviceConnected;

btn_proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
print();
}
});
return view;
}









share|improve this question




















  • 1




    Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
    – zapl
    Nov 20 at 16:20


















0














I'm creating e bill printing application for Android mobile devices. I have successfully connected application and small Bluetooth POS printer. I can also print a bill through this.



I have used fragments for this application and all fragments generating through main activity.



My question is, if I change the fragment and press the print button it will ask to connect with printer again. I need to avoid that process, because once user hit the print button in the previous fragment (this works if user print something using that fragment, otherwise it should ask here to connect with printer) he/she is already connected to a printer.



The problem occurs here when I enter the new fragment, then that fragment creates a new object for handler, I need to put that handler as a singleton. This handler is used to connect with printer through Bluetooth. I have tried a few solutions, but I was failed. Is there any way to do this. Please help me to find a better solution for this problem. Thanks in advance.



here is the code --
Handler and fragment's onCreateView,



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(getContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
isDeviceConnected = true;

// updateConnectBtn();
// btnClose.setEnabled(true);
// btnSendDraw.setEnabled(true);
Log.v("Pasindu", "Connected!");
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������", "��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������", "�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
isDeviceConnected = false;
// btnClose.setEnabled(false);
// btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�޷������豸
Toast.makeText(getContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}

};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.data_fragment, container, false);
btn_proceed = (Button) view.findViewById(R.id.btn_proceed);
mService = new BluetoothService(getContext(), mHandler);
cmd = new byte[3];
isDeviceConnected = ((MainActivity)this.getActivity()).isDeviceConnected;

btn_proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
print();
}
});
return view;
}









share|improve this question




















  • 1




    Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
    – zapl
    Nov 20 at 16:20
















0












0








0







I'm creating e bill printing application for Android mobile devices. I have successfully connected application and small Bluetooth POS printer. I can also print a bill through this.



I have used fragments for this application and all fragments generating through main activity.



My question is, if I change the fragment and press the print button it will ask to connect with printer again. I need to avoid that process, because once user hit the print button in the previous fragment (this works if user print something using that fragment, otherwise it should ask here to connect with printer) he/she is already connected to a printer.



The problem occurs here when I enter the new fragment, then that fragment creates a new object for handler, I need to put that handler as a singleton. This handler is used to connect with printer through Bluetooth. I have tried a few solutions, but I was failed. Is there any way to do this. Please help me to find a better solution for this problem. Thanks in advance.



here is the code --
Handler and fragment's onCreateView,



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(getContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
isDeviceConnected = true;

// updateConnectBtn();
// btnClose.setEnabled(true);
// btnSendDraw.setEnabled(true);
Log.v("Pasindu", "Connected!");
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������", "��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������", "�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
isDeviceConnected = false;
// btnClose.setEnabled(false);
// btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�޷������豸
Toast.makeText(getContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}

};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.data_fragment, container, false);
btn_proceed = (Button) view.findViewById(R.id.btn_proceed);
mService = new BluetoothService(getContext(), mHandler);
cmd = new byte[3];
isDeviceConnected = ((MainActivity)this.getActivity()).isDeviceConnected;

btn_proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
print();
}
});
return view;
}









share|improve this question















I'm creating e bill printing application for Android mobile devices. I have successfully connected application and small Bluetooth POS printer. I can also print a bill through this.



I have used fragments for this application and all fragments generating through main activity.



My question is, if I change the fragment and press the print button it will ask to connect with printer again. I need to avoid that process, because once user hit the print button in the previous fragment (this works if user print something using that fragment, otherwise it should ask here to connect with printer) he/she is already connected to a printer.



The problem occurs here when I enter the new fragment, then that fragment creates a new object for handler, I need to put that handler as a singleton. This handler is used to connect with printer through Bluetooth. I have tried a few solutions, but I was failed. Is there any way to do this. Please help me to find a better solution for this problem. Thanks in advance.



here is the code --
Handler and fragment's onCreateView,



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(getContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
isDeviceConnected = true;

// updateConnectBtn();
// btnClose.setEnabled(true);
// btnSendDraw.setEnabled(true);
Log.v("Pasindu", "Connected!");
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������", "��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������", "�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
isDeviceConnected = false;
// btnClose.setEnabled(false);
// btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�޷������豸
Toast.makeText(getContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}

};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.data_fragment, container, false);
btn_proceed = (Button) view.findViewById(R.id.btn_proceed);
mService = new BluetoothService(getContext(), mHandler);
cmd = new byte[3];
isDeviceConnected = ((MainActivity)this.getActivity()).isDeviceConnected;

btn_proceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
print();
}
});
return view;
}






android bluetooth handler android-bluetooth printers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 16:24









Fantômas

32.3k156288




32.3k156288










asked Nov 20 at 16:09









wm_pasindu

2851421




2851421








  • 1




    Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
    – zapl
    Nov 20 at 16:20
















  • 1




    Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
    – zapl
    Nov 20 at 16:20










1




1




Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
– zapl
Nov 20 at 16:20






Moving the long running logic & state keeping into a Service would be one solution I guess. E.g. developer.android.com/guide/components/bound-services#Messenger the activity just needs to display the state and instruct the service to change state.
– zapl
Nov 20 at 16:20



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397066%2fhow-to-make-the-perfect-singleton-to-handle-bluetooth-services%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397066%2fhow-to-make-the-perfect-singleton-to-handle-bluetooth-services%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno