本文介绍了访问蓝牙"ConnectedThread".来自各种活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在示例.该对象在连接活动中实例化,该活动是通过单击连接"按钮从主要活动到达的,该按钮提供了可选设备的ListView.到目前为止效果很好.

I have created a BluetoothManager much like the one in this example. This object is instantiated in a connection activity, reached from the main acitivty by clicking on a "Connect" button, which provides a ListView of selectable devices. Works great so far.

我现在已连接并且正在运行BluetoothManager.ConnectedThread并设置了流.我现在希望能够在其他活动正在运行时从它们发送蓝牙数据.例如,当图表活动运行时,我将要绘制实时值.

I am now connected and have a BluetoothManager.ConnectedThread running and the streams set up. I would now like to be able to send Bluetooth data from/to various other activities when they are running. For example, I will want to chart realtime values when the charting activity is running.

据我所知,通过处理程序,这对我来说是一个新话题.我不清楚他的其他活动如何访问ConnectedThread的write()函数.

As far as I can tell, the pushing of the data out from the ConnectedThread will occur via a Handler, which is a new topic for me. What I am unclear on his how other activities might access the ConnectedThread's write() function.

推荐答案

首先,即使单例可能是一个解决方案,android Service也可以用于此目的,因为这些是可以持续运行的元素当您的用户界面退出时.所以我的建议是创建一个粘性服务,然后您有两个选择:

First of all, even though a singleton could be a solution, android Service's are there for this purpose, since these are elements that can keep running when your UI is out. So my suggestion would be to create a sticky service an then you have two options:

  • 使用活动和Service之间的处理程序处理数据.也许如果您不太熟悉Handler api,这将需要一些时间.在官方文档的示例中,您还可以查看如何使用处理程序.
  • 创建一个绑定服务,您可以从活动中绑定到该服务,并在需要时发送一些数据. 此处,您具有有关绑定服务的官方信息.
  • Handle data using a handler between the activity and the Service. Maybe if you are not too familiar with the Handler api this will take some time to you. In this example of the official documentation you can also check how to use the handler.
  • Create a bound service, to which you can bind from the activities and send some data when required. Here you have the official information about bound services.

您可以查看教程以获取更多信息有关处理程序的信息.

You can have a look to this tutorial to get more information about handlers.

这篇关于访问蓝牙"ConnectedThread".来自各种活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:56