背景中的Android蓝牙服务?

时间:2014-12-19 09:14:23

标签: java android bluetooth arduino

我是android编程的新手。我想构建一个可以通过蓝牙与arduino通信的应用程序。

我已经开始了,我有一个活动,我可以设置连接并维护它。它工作得很好。对于连接和连接的线程,我编写了一个自己的类,通过我的活动中的实例验证调用。

但现在出现了问题: 在我成功连接到我的Arduino之后,当前的Activity(连接)应该停止并打开一个新的Activity,我可以开始发送消息。 但是如何从我在上一个Activity中创建的线程发送/接收数据?

我读过有关服务等的内容? 解决这个问题的最佳方法是什么?

当我完成应用程序时,它应该有许多活动,我可以通过蓝牙传递不同的数据。因此,我希望第一个活动设置连接,然后我可以从其他活动中使用它。

公共班级blueTooth {

private ConnectThread connection;
private ConnectThread mConnectThread;
private ConnectedThread mConnectedThread;

private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");



public synchronized void connect(BluetoothDevice device) {

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device);
    mConnectThread.start();

}

public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server
            // code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
        }
        mmSocket = tmp;
        }

    public void run() {
        // Cancel discovery because it will slow down the connection


        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) {
            }
            return;
        }
        // Reset the ConnectThread because we're done
        synchronized (blueTooth.this) {
            mConnectThread = null;
        }

        // Do work to manage the connection (in a separate thread)
    manageConnectedSocket(mmSocket);
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }

}

public void manageConnectedSocket (BluetoothSocket socket) {
    // Cancel the thread that completed the connection
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket);

    mConnectedThread.start();
}

/** .................................*/
private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                //mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                //        .sendToTarget();
            } catch (IOException e) {
                break;
            }
      }

    }


    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

}

}

期待您的帮助!

0 个答案:

没有答案