访问蓝牙"ConnectedThread".来自各种活动 [英] Accessing a Bluetooth "ConnectedThread" from various activities

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

问题描述

我已经在

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.

据我所知,通过

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:

  • 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".来自各种活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆