对战射击游戏戏中那些能蓝牙对战或wlan对战?

关注我们:1141人阅读
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网站的观点或立场
访问:1858次
排名:千里之外求大神推荐几个可以WLAN对战 不要流量的游戏【手机游戏吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
求大神推荐几个可以WLAN对战 不要流量的游戏收藏
这一阵子和同学一直在玩NOVA3 因为可以WLAN对战 但是有些腻了 听说 现代战争4可以WLAN对战 却发现我的手机闪退 求大神推荐几款射击类型的WLAN对战游戏 不是射击的也行
三国杀kill
泰拉瑞亚   --给个粉不介意吧= ̄ω ̄=。这个不是小尾巴!
你去试试就知道了   --给个粉不介意吧= ̄ω ̄=。这个不是小尾巴!
卡特尔,铁锈战争,mc系列,狂野之血等等
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或对战游戏|手机对战游戏|多人对战游戏|安卓wifi对战游戏
安卓多人对战游戏合集
(更新日期: 11:53:29)
玩够了单机游戏,玩够了手游,可以试试小编今天推荐的这些安卓多人对战游戏,包括蓝牙对战,包括wifi对战,在2014春节即将到来之际,为自己的爱机备上几款对战类的游戏,在无聊的假期里和自己的家人朋友一起度过一个快乐的春节吧。当前位置: >
> 蓝牙对战游戏合集
蓝牙对战游戏合集蓝牙对战游戏是众多游戏中非常有趣的一个游戏,一个人游戏多孤单多无聊,有朋友一起对战才叫爽,酣畅淋漓大战一场!本期专题应广大用户需求特别定制精选多款多人支持蓝牙对战的游戏,让你在公车上、火车上、飞机上、床铺上、聚会时都能与朋友一决高下!
标签合集:
202MB推荐理由:经典游戏不容错过,这里有你的回忆,同时也有着热血的战斗。
134MB推荐理由:曾经风靡到在网吧都是玩这个游戏,现在登录手机端,一场卡丁车风暴即将来袭!
124MB推荐理由:游戏制作品质和震撼的玩法突破,受到业内市场的认可,被誉为“真正的格斗游戏”。
43MB推荐理由:极品飞车系列一向深受玩家们喜爱,海量赛道,丰富关卡,定会让你爱不释手。
527MB推荐理由:战争题材手游中的佼佼者,很有大片的感觉,必玩。
712MB推荐理由:经典的格斗游戏需要和好友一起体验!
712MB内容简介:街霸4安卓市场通用版(Street Fighter IV)是一款老少皆宜的经典格斗手游,本游戏适用于...
48MB内容简介:三国大时代3内购破解版让玩家在这款像素风格手机游戏中免费购买到更多的物品,玩家将在游戏中体验到崭新的...
26MB内容简介:小小枪战反恐精英2无限金币安卓破解版让玩家体验前作中的精彩,游戏中玩家可以免费获得更多的金币,现在你...
893MB内容简介:《狂野飙车8:极速凌云》(Asphalt 8 Airborne)含数据包 安卓商店版 是一款完美解决...
41MB内容简介:洪荒扑鱼手游官方版是一款简约风格的捕鱼游戏,游戏在集成经典捕鱼玩法的同时也加入了众多全新的内容,创意...
43MB内容简介:星际人类3手机安卓版(Stellaren III)玩家将带领众多妹子参加星际战斗,故事发生在 234...
47MB内容简介:空间争夺手机安卓版 DiceTris 是一款简约风格的益智休闲游戏,在游戏中玩家需要努力争夺空间,以...
45MB内容简介:坦克大战巷战内购破解版(Tank)将让玩家更轻松打败敌人,游戏采用卡通风格的游戏画面,在游戏中玩家可...
250MB内容简介:线上赛车无限钞票破解版(Cyberline Racing)这是一款非常刺激暴力的赛车游戏,将赛车玩法...
48MB内容简介:星际人类4手机安卓版(Stellaren IV)将延续星际太空故事第四部,玩家将被带到 2340年,...
241MB内容简介:细菌生存破解版是一款细菌吞食类型的手机休闲游戏,游戏有趣且恶搞式的动画效果,简单粗暴的大鱼吃小鱼式的...
56MB内容简介:吃豆大作战手机游戏安卓版是一款可以多名玩家一起参与得劲竞技类物理性休闲类游戏,一指性的操作都可以给玩...

我要回帖

更多关于 网络对战射击游戏 的文章

 

随机推荐