阅读外部android应用程序中的蓝牙消息



我是安卓蓝牙的新手,我想使用内部存储或sqlite在外部安卓应用程序(我的)中读取并存储蓝牙消息。我尝试过GitHub的android蓝牙聊天示例,但我不知道如何实现我的想法。

任何帮助都会很有帮助,感谢

蓝牙消息的交换在api的android.bluetooth部分中介绍。

http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection

以下是管理连接和发送/接收消息的基本示例:

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) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}

}

最新更新