编程回忆之Android回忆(蓝牙BluetoothAdapter的搜索和连接)
2014-02-27 11:34
621 查看
Android的搜索和连接
近期项目设计到一个蓝牙打印机的模块,故查阅了相关资料,发现蓝牙连接和传输的方式跟socket其实很像,设置在蓝牙服务端的设计跟socket服务器端的设计更是相像,只是简单很多而已。下面通过代码简单介绍一下蓝牙搜索和连接。
一、蓝牙权限的配置
在AndroidManifest.xml中配置蓝牙的权限,加上以下两行代码
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
二、配置本机的蓝牙模块
在配置蓝牙模块时,我们首先要对蓝牙模块的一个核心类BluetoothAdapter有深刻的了解。
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //直接打开系统的蓝牙设置面板 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
三、蓝牙的搜索
在进行蓝牙搜索开发的时候,我们应该首先了解Android的广播机制,在搜索时使用BluetoothAdapter的startDiscovery()方法来搜索蓝牙设备,startDiscovery()方法是一个异步方法,调用后会立即返回。该方法会进行对其他蓝牙设备的搜索,该过程会持续12秒。该方法调用后,搜索过程实际上是在一个System Service中进行的,所以可以调用cancelDiscovery()方法来停止搜索(该方法可以在未执行discovery请求时调用)。
请求Discovery后,系统开始搜索蓝牙设备,在这个过程中,系统会发送以下三个广播:ACTION_DISCOVERY_START:开始搜索
ACTION_DISCOVERY_FINISHED:搜索结束
ACTION_FOUND:找到设备,这个Intent中包含两个extra fields:EXTRA_DEVICE和EXTRA_CLASS,分别包含BluetooDevice和BluetoothClass。
我们通过自己配置广播接收器来实现Android蓝牙的搜索
// 设置广播信息过滤 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); // 注册广播接收器,接收并处理搜索结果 context.registerReceiver(receiver, intentFilter);
/** * 蓝牙广播接收器 */ private BroadcastReceiver receiver = new BroadcastReceiver() { ProgressDialog progressDialog = null; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { addBandDevices(device); } else { addUnbondDevices(device); } } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { progressDialog = ProgressDialog.show(context, "请稍等...", "搜索蓝牙设备中...", true); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED .equals(action)) { System.out.println("设备搜索完毕"); progressDialog.dismiss(); addUnbondDevicesToListView(); addBondDevicesToListView(); // bluetoothAdapter.cancelDiscovery(); } if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) { System.out.println("--------打开蓝牙-----------"); searchDevices.setEnabled(true); bondDevicesListView.setEnabled(true); unbondDevicesListView.setEnabled(true); } else if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) { System.out.println("--------关闭蓝牙-----------"); searchDevices.setEnabled(false); bondDevicesListView.setEnabled(false); unbondDevicesListView.setEnabled(false); } } } };
四、蓝牙的连接
如果打算建议两个蓝牙设备之间的连接,则必须实现服务器端与客户端的机制。当两个设备在同一个RFCOMM channel下分别拥有一个连接的BluetoothSocket,这两个设备才可以说是建立了连接。
服务器设备与客户端设备获取BluetoothSocket的途径是不同的。服务器设备是通过accepted一个incoming connection来获取的,而客户端设备则是通过打开一个到服务器的RFCOMM channel来获取的。
/** * 连接蓝牙设备 */ public boolean connect() { if (!this.isConnection) { try { boolean isOldVersion = false; if (Build.VERSION.SDK_INT < 15) { isOldVersion = true; } // 兼容旧版本 if (isOldVersion) { bluetoothSocket = this.device .createRfcommSocketToServiceRecord(uuid); } else { bluetoothSocket = this.device .createInsecureRfcommSocketToServiceRecord(uuid); } bluetoothSocket.connect(); outputStream = bluetoothSocket.getOutputStream(); this.isConnection = true; if(isConnection){ PRT=new PRTAndroidPrint(outputStream); } if (this.bluetoothAdapter.isDiscovering()) { System.out.println("关闭适配器!"); this.bluetoothAdapter.isDiscovering(); } } catch (Exception e) { Toast.makeText(this.context, "连接失败!", 1).show(); return false; } Toast.makeText(this.context, this.device.getName() + "连接成功!", Toast.LENGTH_SHORT).show(); return true; } else { return true; } }
五、蓝牙的断开
使用蓝牙后记得关闭蓝牙连接,释放内存。
/** * 断开蓝牙设备连接 */ public static void disconnect() { System.out.println("断开蓝牙设备连接"); try { if(bluetoothSocket!=null){ bluetoothSocket.close(); } if(outputStream!=null){ outputStream.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
相关文章推荐
- android bluetooth——蓝牙的开启、搜索、配对与连接
- android bluetooth——蓝牙的开启、搜索、配对与连接
- android BluetoothAdapter无法搜索到蓝牙问题
- Android中BluetoothAdapter.startDiscovery方法搜索蓝牙设备不起作用的解决办法
- android bluetooth——蓝牙的开启、搜索、配对与连接
- android bluetooth——蓝牙的开启、搜索、配对与连接
- Android蓝牙API之BluetoothAdapter类(1)
- Android BLE与终端通信(二)——Android Bluetooth基础搜索蓝牙设备显示列表
- Android 蓝牙( Bluetooth)耳机连接分析及实现
- android 蓝牙bluetooth 4.2.2搜索蓝牙功能实现
- ZT android -- 蓝牙 bluetooth (三)搜索蓝牙
- Android蓝牙开发,报BluetoothAdapter﹕ Can't create handler inside thread that has not called Looper.prepare
- 如何在 Windows Mobile (WinCE 5.0) 中用Vc++编程查找并连接周围的蓝牙(Bluetooth)设备并实现数据通信
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- Android蓝牙API之BluetoothAdapter类(2)
- android 蓝牙搜索、配对连接通信总结
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- Android 蓝牙( Bluetooth)耳机连接分析及实现
- android6.0 蓝牙搜索不到设备 BluetoothAdapter.getDefaultAdapter().startDiscovery()不起作用
- Android 6.0后蓝牙的开发,搜索,配对,连接发送数据等(一)