android上的双人蓝牙联机手机游戏游戏有哪些

关注我们:重庆市渝中区/手机型号:&&
个性签名:泽拉图
玩家原创专辑推荐最热/最新
共10个游戏
共12个游戏
共15个游戏
共30个游戏
共18个游戏
共14个游戏
共328个游戏
专辑推荐最热/最新
共237个游戏
共118个游戏
共62个游戏
共35个游戏
共25个游戏
共32个游戏
共32个游戏
共38个游戏
共38个游戏
共44个游戏
蓝牙wifi联机对战
创建时间:
浏览次数:263673次
专辑作者:引见龙舞
蓝牙wifi联机对战 好友对战专题
专辑游戏(共10个游戏)
版本:v2.2.9大小:35.89M
时间:厂商:Gameabc
版本:v1.0.7大小:1914.06M
时间:厂商:Gameloft
版本:v1.1.1大小:823.46M
时间:厂商:Gameloft
版本:v4.5.4大小:291.59M
时间:厂商:世嘉
版本:v1.0.1大小:1264.95M
时间:厂商:Wizards of the Coast LLC
版本:v1.1.0大小:1200.6M
时间:厂商:Gameloft
版本:v1.4.0大小:391.22M
时间:厂商:Gameloft
版本:v1.0大小:585.7M
时间:厂商:2K Games, Inc.cocos 游戏(1)
前段时间写了个安卓蓝牙联机对战的游戏,写的也不怎么好,现在想写成个博客记录下
public class AppActivity extends Cocos2dxActivity {
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
return glSurfaceV
// work for not static func
private static Cocos2dxActivity sContext =
public static Context getContext() {
//服务端和游戏端
enum ServerOrCilent {
//安卓调用 c++
向游戏中发送消息 private
native void StringFromJni(ArrayList&ChatMessage& str);
public native int get();
public native void set(int i);
private native void SendTo(String msg);
private native void SendMsgForServer(String msg);
private native void SendMsgForClient(String msg);
public static AppA
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothD
private ListView mListV
private ArrayList&ChatMessage& list = new ArrayList&ChatMessage&();
private ServerOrCilent serverorcilent = ServerOrCilent.NONE;
private BluetoothS
// 客户端socket
private ClientThread mClientT // 客户端运行线程
private BluetoothServerSocket mserverS
// 服务端socket
private ServerThread mserverT // 服务端运行线程
private ReadThread mreadT
// 读取流线程
public static final String PROTOCOL_SCHEME_L2CAP = &btl2cap&;
public static final String PROTOCOL_SCHEME_RFCOMM = &btspp&;
public static final String PROTOCOL_SCHEME_BT_OBEX = &btgoep&;
public static final String PROTOCOL_SCHEME_TCP_OBEX = &tcpobex&;
//UUID可以看做一个端口号
private static final UUID MY_UUID =
UUID.fromString(&0-805F9B34FB&);
public static Object getInstance()
if (instance==null){
instance = new AppActivity();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance =
Log.e(&leeeee&,&AppActivity onCreate ------------------------&);
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
* 打开蓝牙
* 返回0 : 没有蓝牙硬件或驱动
* 返回1 : 打开了蓝牙
* 返回2 : 以打开
String openBlueToot(int type)
if (mBluetoothAdapter == null) {
return &0&;
if(!mBluetoothAdapter.isEnabled())
//请求用户开启
//Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(intent, RESULT_FIRST_USER);
//使蓝牙设备可见,方便配对
//直接开启,不经过提示
mBluetoothAdapter.enable();
if(type == 1)
Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(in);
String name = mBluetoothAdapter.getName();
serverorcilent = ServerOrCilent.SERVICE;
if(type == 1)
Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(in);
return mBluetoothAdapter.getName();
/* 关闭蓝牙 */
public void CloseBlueToot()
mBluetoothAdapter.disable();
* 扫描蓝牙
public void findBuleToots()
Log.e(&leeeee&,&findBuleToots ------------------------&);
serverorcilent = ServerOrCilent.CILENT;
IntentFilter filterFound = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filterFound.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filterFound.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filterFound.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filterFound.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filterFound.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(mReceiver, filterFound);
mBluetoothAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e(&leeeee&,&BroadcastReceiver
------------------------&);
if (BluetoothDevice.ACTION_FOUND.equals(action))
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(&leeeee&,&BluetoothDevice addddd 2------------------------&+device.getName()+&_&+device.getAddress());
if(device.getName()!= null)
ChatMessage chmsg = new ChatMessage(device.getName(), device.getAddress());
if(list.indexOf(chmsg) == -1)// 防止重复添加
list.add(chmsg);
StringFromJni(list);
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
Log.e(&leeeee&,&BluetoothDevice nooooooooo 2------------------------&);
if(list.size() == 0){
//list.add(new ChatMessage(&no_device&, &00&));
//StringFromJni(list);
//搜索到所有的蓝牙设备后 选择一个服务器的地址
public void chooseBlueDevice(String addr)
Log.d(&leeeee&, &chooseBlueDevice 1..........................&);
mBluetoothAdapter.cancelDiscovery(); //取消搜索
//判断给定地址下的device是否已经配对
BluetoothDevice device1 = mBluetoothAdapter.getRemoteDevice(addr);
if(device1.getBondState() == BluetoothDevice.BOND_NONE){
Method createBondMethod = BluetoothDevice.class
.getMethod(&createBond&);
Log.d(&leeeee&, &chooseBlueDevice 2..........................&);
returnValue = (Boolean) createBondMethod.invoke(device1);
if(returnValue)
device = device1;
Log.e(&leeeee&,&chooseBlueDevice success------------------------&+device1.getName());
MyThread mt = new MyThread();
mt.start();
Log.e(&leeeee&,&chooseBlueDevice failed------------------------&+device1.getName());
}catch(Exception e){
Log.e(&leeeee&,&chooseBlueDevice bagin------------------------&+device1.getName());
} else if(device1.getBondState() == BluetoothDevice.BOND_BONDED){ //已经配对成功
Log.d(&leeeee&, &已经配对成功 2..........................&);
device = device1;
buildClientConnect();
private class MyThread extends Thread {
public void run()
boolean istemp =
while(istemp)
Set&BluetoothDevice& set = mBluetoothAdapter.getBondedDevices();
for(BluetoothDevice dev : set)
if(dev.getAddress().equals(address))
buildClientConnect();
this.interrupt();
* 建立服务器 选择的一个用户
public void buildServerConnect()
mserverThread = new ServerThread();
mserverThread.start();
* 建立客户端 选择的一个用户
public void buildClientConnect()
mClientThread = new ClientThread();
mClientThread.start();
//开启服务器
private class ServerThread extends Thread {
public void run() {
/* 创建一个蓝牙服务器
* 参数分别:服务器名称、UUID
mserverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM,
Log.e(&leeeee&, &Server wait cilent connect*************************&);
/* 接受客户端的连接请求 */
socket = mserverSocket.accept();
Log.e(&leeeee&, &Server accept success***********************************&);
//启动接受数据
mreadThread = new ReadThread();
mreadThread.start();
} catch (IOException e) {
e.printStackTrace();
// 开启客户端连接服务端
private class ClientThread extends Thread {
public void run() {
if (device != null) {
socket = device.createRfcommSocketToServiceRecord(MY_UUID);
Log.e(&leeeee&, &Client loading***********************************&);
// 通过socket连接服务器,这是一个阻塞过程,直到连接建立或者连接失效
if (!socket.isConnected()) {
socket.connect();
Log.e(&leeeee&, &ClientThread success***********************************&);
sendMessageHandle(&ClientThread_success_Server_BubbleGame&);
// 可以开启读数据线程
mreadThread = new ReadThread();
mreadThread.start();
} catch (IOException e) {
Log.e(&leeeee&, &ClientThread error***********************************&+e.getMessage());
if(serverorcilent == ServerOrCilent.CILENT)
SendMsgForClient(&Connect_error&);
SendMsgForServer(&Connect_error&);
//发送数据
public void sendMessageHandle(String msg)
if (socket == null)
//Toast.makeText(mContext, &没有连接&, Toast.LENGTH_SHORT).show();
Log.e(&Leeeeee&,&send... ------------------------&+msg);
OutputStream os = socket.getOutputStream();
os.write(msg.getBytes());
} catch (IOException e) {
//如果断开连接 发送错误代码 -5
if(serverorcilent == ServerOrCilent.CILENT)
SendMsgForClient(&State_-5&);
SendMsgForServer(&State_-5&);
e.printStackTrace();
// 通过socket获取InputStream流
private class ReadThread extends Thread {
public void run() {
// TODO Auto-generated method stub
byte[] buffer = new byte[1024];
InputStream is =
is = socket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
while(true) {
if ((bytes = is.read(buffer)) & 0) {
byte[] data = new byte[bytes];
for (int i = 0; i & data. i++) {
data[i] = buffer[i];
String s = new String(data);
Log.e(&Leeeeee&,&read... ------------------------&+s);
if(serverorcilent == ServerOrCilent.CILENT)
if(s.equals(&ClientThread_success&))
SendTo(&ClientThread_success&);
SendMsgForClient(s);
if(s.equals(&ClientThread_success&)){
SendTo(&ClientThread_success&);
sendMessageHandle(&ClientThread_success&);
SendMsgForServer(s);
} catch (IOException e) {
is.close();
} catch (IOException e1) {
e1.printStackTrace();
/* 停止服务器 */
private void shutdownServer() {
new Thread() {
public void run() {
if(mserverThread != null)
mserverThread.interrupt();
mserverThread =
if(mreadThread != null)
mreadThread.interrupt();
mreadThread =
if(socket != null)
socket.close();
if (mserverSocket != null)
mserverSocket.close();/* 关闭服务器 */
mserverSocket =
} catch (IOException e) {
Log.e(&server&, &mserverSocket.close()&, e);
}.start();
/* 停止客户端连接 */
private void shutdownClient() {
new Thread() {
public void run() {
if(mClientThread!=null)
mClientThread.interrupt();
mClientThread=
if(mreadThread != null)
mreadThread.interrupt();
mreadThread =
//modify begin ---------
if (mBluetoothAdapter != null && mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
//modify end ---------
if (socket != null) {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}.start();
public class ChatMessage {
public ChatMessage(String name, String address) {
this.name =
this.address =
public String getName() {
public void setName(String name) {
this.name =
public String getAddress() {
public void setAddress(String address) {
this.address =
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1034次
排名:千里之外

我要回帖

更多关于 双人蓝牙联机手机游戏 的文章

 

随机推荐