【问题标题】:Android bluetooth game, need to transfer int[] across byte streamAndroid蓝牙游戏,需要跨字节流传输int[]
【发布时间】:2011-12-02 02:05:41
【问题描述】:

情况如下:我正在为 android 制作多人游戏版本的 pong。当我的球拍与球相连时,我想向其他玩家发送球的撞击点(作为 x,y 坐标),以便其他玩家的屏幕可以更新以显示球的来源以及球的来源我的桨的位置。另一个设备将计算球的轨迹并最终重复这个过程。基本上我想要的是这样的东西--

static final int RESPONSE_CODE = (some #);

int[] return_data = {RESPONSE_CODE, myPaddle.x, myPaddle.y, ballImpactPosition.x, ballImpactPosition.y};

blueToothConnection.write(return_data);

这里的问题是bluetoothConnection.write()依赖于java.io.OutputStream,所以只支持写一个byte[]。

那么,有什么方法可以将我的 int[] 转换为 byte[](稍后再返回)?如果有帮助,我的 int[] 将始终包含 5 个整数,所有整数始终为正数。我不确定我的整数的最大值是多少,因为这取决于设备的屏幕尺寸。

【问题讨论】:

    标签: android bluetooth bytearrayoutputstream iobluetooth


    【解决方案1】:

    在这种情况下,ByteBuffer 是你的朋友:

    Android API link for ByeBuffer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多