实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.himi; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; /** * @author Himi * @蓝牙 需要在AndroidMainfest.xml中添加权限 */ public class MainActivity extends Activity implements OnClickListener { //按钮组件 public static Button btConnect, btOpen, btIsVisible, btSearch; //蓝牙适配器 public static BluetoothAdapter btAda; //UUID协议 public static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; //蓝牙连接 public static BluetoothSocket btSocket; //单利本类 public static MainActivity ma; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ma = this; //这里没有设置全屏,为了便于观察当前蓝牙是否打开的状态变化 //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); //实例按钮 btOpen = (Button) findViewById(R.id.Btn_OpenBt); btIsVisible = (Button) findViewById(R.id.Btn_BtIsVisible); btSearch = (Button) findViewById(R.id.Btn_SearchDrives); btConnect = (Button) findViewById(R.id.Btn_ConnectDrives); //为按钮绑定监听器 btOpen.setOnClickListener(this); btIsVisible.setOnClickListener(this); btSearch.setOnClickListener(this); btConnect.setOnClickListener(this); //实例蓝牙适配器 btAda = BluetoothAdapter.getDefaultAdapter(); if (btAda.getState() == BluetoothAdapter.STATE_OFF) { btOpen.setText("打开蓝牙"); } else if (btAda.getState() == BluetoothAdapter.STATE_ON) { btOpen.setText("关闭蓝牙"); } // 注册Receiver来获取蓝牙设备相关的结果 IntentFilter intent = new IntentFilter(); intent.addAction(BluetoothDevice.ACTION_FOUND);// 远程设备发现动作。 intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//远程设备的键态的变化动作。 intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//蓝牙扫描本地适配器模改变动作。 intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//状态改变动作 registerReceiver(searchDevices, intent);//注册接收 } //监听动作 private BroadcastReceiver searchDevices = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //搜索设备时,取得设备的MAC地址 if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String str = "设备:" device.getName() "*" device.getAddress(); if (MySurfaceView.vc_str != null) { if (MySurfaceView.vc_str.size() != 0) { for (int j = 0; j < MySurfaceView.vc_str.size(); j ) { // 防止重复添加 if (MySurfaceView.vc_str.elementAt(j).equals(str) == false) { // 容器添加发现的设备名称和mac地址 MySurfaceView.vc_str.addElement(str); } } } else { MySurfaceView.vc_str.addElement(str); } } } } }; @Override protected void onDestroy() { this.unregisterReceiver(searchDevices); super.onDestroy(); System.exit(0); } @Override public void onClick(View v) { if (MySurfaceView.gameState != MySurfaceView.CONNTCTED) { if (v == btOpen) {//蓝牙开关 if (btAda.getState() == BluetoothAdapter.STATE_OFF) { btAda.enable(); btOpen.setText("关闭蓝牙"); } else if (btAda.getState() == BluetoothAdapter.STATE_ON) { btAda.disable(); btOpen.setText("打开蓝牙"); } } else if (v == btIsVisible) {//蓝牙是否可见 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 110); //第二个参数是本机蓝牙被发现的时间,系统默认范围[1-300],超过范围默认300,小于范围默认120 startActivity(intent); } else if (v == btSearch) {//搜索蓝牙 if (btAda.getState() == BluetoothAdapter.STATE_OFF) {// 如果蓝牙还没打开 Toast.makeText(MainActivity.this, "请先打开蓝牙", 1000).show(); return; } setTitle("本机蓝牙地址:" btAda.getAddress()); MySurfaceView.vc_str.removeAllElements(); btAda.startDiscovery(); } else if (v == btConnect) { if (MySurfaceView.vc_str.size() == 0) { Toast.makeText(MainActivity.this, "当前没有设备", 1000).show(); } else { Intent intent = new Intent();//别忘记声明咱们要打开的这个Activity intent.setClass(this, ChoiceDrivesList.class); this.startActivity(intent); } } } else { Toast.makeText(this, "这里只是一个Demo示例,很多情况没有进行处理,为了等出现误操作造成异常,请重新运行项目!", Toast.LENGTH_LONG).show(); this.finish(); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论