编程回忆之Android回忆(蓝牙BluetoothAdapter的搜索和连接)

来源:互联网 发布:center os 安装软件 编辑:程序博客网 时间:2024/05/04 12:46

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 blocke.printStackTrace();}}


 

0 0