android 蓝牙游戏 数据传递,该如何处理
android 蓝牙游戏 数据传递
现在弄了一个蓝牙对战的的卡牌游戏
需要通过蓝牙传递游戏数据
采用了 BluetoothChat 的demo作为基础
但是现在要传递 游戏内得一些 数据
但是不知道怎么弄
求大大解决
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 BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(CyclesOfBluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(CyclesOfBluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
这一段里面加入我想给另一端的 数据 传递 另一边接受到了后在 TextView显示
ps:
下载地址 http://163.fm/Tf4svS
提取码 WjUPpgQ2
------解决方案--------------------
注意到我的connectedthread中获取到信息之后是通过一个handler传送到另外一个类进行操作的,你可以照着使用一个handler接收message然后在里面修改textview。貌似在线程里面是无法直接修改UI的……
现在弄了一个蓝牙对战的的卡牌游戏
需要通过蓝牙传递游戏数据
采用了 BluetoothChat 的demo作为基础
但是现在要传递 游戏内得一些 数据
但是不知道怎么弄
求大大解决
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 BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(CyclesOfBluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(CyclesOfBluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
这一段里面加入我想给另一端的 数据 传递 另一边接受到了后在 TextView显示
ps:
下载地址 http://163.fm/Tf4svS
提取码 WjUPpgQ2
------解决方案--------------------
注意到我的connectedthread中获取到信息之后是通过一个handler传送到另外一个类进行操作的,你可以照着使用一个handler接收message然后在里面修改textview。貌似在线程里面是无法直接修改UI的……