在多个类别的Andr​​oid BlutoothChatService使用多个、类别、Andr、oid

2023-09-08 08:33:08 作者:苍阶

为了建立一个蓝牙通信使用BluetoothChat样品IM。香港专业教育学院创建SecondView.java现在,我想送而无需重新连接到蓝牙接收来自那里的数据。有什么办法来访问发送和接收的BluetoothChat.java例子我SecondView.java?I使用的方法发现了一个工作方法是使用绑定服务,但我不知道如何实现这个..

im using the BluetoothChat sample in order to establish a bluetooth communication. Ive created SecondView.java now and i want to send and receive data from there without having to reconnect to the bluetooth. Is there any way to access the send and receive methods used in the BluetoothChat.java example to my SecondView.java?I found that a working method is to use Bound Service but i dont know how to implement this..

推荐答案

如果您遵循蓝牙聊天的例子,那么你将使用线程进行蓝牙通信,例如一个connectedthread具有读写蓝牙通信方法。

If you are following the bluetooth chat example, then you will be using threads for bluetooth communication, e.g. a connectedthread that has the read and write methods for bluetooth communication.

它实际上很简单,能够读取并在您的应用程序的任何地方写。你只需要有一个全局引用该线程的应用程序。

Its actually really simple to be able to read and write from anywhere in your app. You just need to have a global reference to that thread for your application.

在一个Android应用程序,该应用程序有一个背景下,是在整个应用程序的全局。您可以从任何活动getApplication()方法得到这个。要设置的应用程序背景下自己的变量,你需要扩展应用程序类,并朝它指向你的清单。

In a android app, the application has a context that is a global across your entire app. You can get this with the getApplication() method from any activity. To set your own variables on the 'Application' context, you need to extend the application class, and point your manifest towards it.

下面有一个例子。我已经扩展了应用程序类,并提出了连接线的变量,具有getter和setter方法​​。

Heres an example. I've extended the application class, and made a variable for the connected thread, with getter and setter methods.

class MyAppApplication extends Application {
        private ConnectedThread mBluetoothConnectedThread;

        @Override
        public void onCreate() {
            super.onCreate();
        }

        public ConnectedThread getBluetoothConnectedThread() {
            return mBluetoothConnectedThread;
        }

        public void setBluetoothConnectedThread(ConnectedThread mBluetoothConnectedThread) {
            this.mBluetoothConnectedThread = mBluetoothConnectedThread;
        }

    }

和指向你对这个类的清单,你需要到Android设置:应用程序元素的name属性来我们上面创建的应用程序类的类名。例如。

And to point your manifest towards that class, you need to set the android:name attribute of the application element to the class name of the application class we created above. E.g.

 <application
        android:name="com.myapp.package.MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

在多数民众赞成做,你可以通过拨打任何访问ConnectedThread在活动

Once thats done you can access the ConnectedThread at any in a activity by calling

MyAppApplication.getBluetoothConnectedThread().write()
MyAppApplication.getBluetoothConnectedThread().read()

请注意,你需要线程首先设置到模型一旦其创建的:

Note that you need to set the thread to the model first once its created:

MyAppApplication.setBluetoothConnectedThread(MyNewConnectedThread);

希望有所帮助。