【问题标题】:Bluetooth comunication android蓝牙通讯安卓
【发布时间】:2023-03-04 14:47:01
【问题描述】:

我需要通过蓝牙传递一个 Intent。以前我创建了游戏,并将游戏的特征保存在类类型单例中。

我想要做的是将此信息传递给我已经连接的其他设备并开始我或他/她之前创建的游戏。

如何发送此信息和游戏活动?

我使用 andengine 来制作所有游戏。

谢谢!

【问题讨论】:

  • 我也想要这个信息,那么如何在两个连接的设备之间传递对象?

标签: android eclipse bluetooth andengine multiplayer


【解决方案1】:

我认为this site 可能会对您有所帮助。有几种方法,但您需要 OP 中提到的最后一种方法。

public void sendFile(Uri uri, BluetoothSocket bs) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(getContentResolver().openInputStream(uri));
        OutputStream os = bs.getOutputStream();
    try {
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;
        while ((len = bis.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
    } finally {
        bis.close();
        os.flush();
        os.close();
        bs.close();
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多