开发者

Android: Multithreading-Bluetooth SPP/RFCOMM-How to keep my BluetoothSocket and OutputStream active when changing Activities

开发者 https://www.devze.com 2023-01-16 06:58 出处:网络
I am pretty new to Android and very new to Multithreading. I am writing an android application that contains multiple activities that implement onClickListners which sends a Bluetooth message on butt

I am pretty new to Android and very new to Multithreading.

I am writing an android application that contains multiple activities that implement onClickListners which sends a Bluetooth message on button click to . I have successfully been able to connect and keep my BluetoothSocket and OutputStream open while in one activity. I do not do any receiving and will always be connected as a client to a known MAC address. When the user wants to go into a submenu, which is also prompted by a button click, how do I keep my BluetoothSocket open?

I know I will need to have a thread running in the background managing the connection and I will use handlers/messages to exchange data between the BluetoothService thread and the currently active UI thread, but I am not sure what classes and methods I would use to start the BluetoothService thread by the application so that it will accessible by all activities.

Would it be something I start in the first activity of the program and have t开发者_运维百科o implement it in a way that would prevent it from ending onPause() and onStop() and if so how would I start it and what classes would be recommended?

Or is it something I could define in the AndroidManifest.xml file so that it starts and runs at all times once the application is started. Run as a service and use messages/handlers to pass data?

These are just two ideas I had but I know I am probably way off.

Thanks,

Jonathan

0

精彩评论

暂无评论...
验证码 换一张
取 消