美文网首页
蓝牙帮助类

蓝牙帮助类

作者: 飞渡浮舟 | 来源:发表于2017-08-11 16:35 被阅读30次
    package com.sie.isky.android.helper;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.Intent;
    import android.util.Log;
    
    import com.sie.isky.android.bean.PrinterBean;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Set;
    
    /**
     * 时 间: 2017/8/7
     * 作 者: 郑亮
     * Q  Q : 1023007219
     */
    
    public class BlueToothHelper {
    
        private BluetoothAdapter bluetoothAdapter = null;
    
        public BlueToothHelper() {
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        }
    
        public BluetoothAdapter getAdapter() {
            return bluetoothAdapter;
        }
    
    
        public BluetoothDevice getDeviceByMac(String mac) {
            return bluetoothAdapter.getRemoteDevice(mac);
        }
    
        /**
         * 是否支持蓝牙
         */
        public boolean isSupportBlueTooth() {
            if (bluetoothAdapter != null) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * 蓝牙是否打开
         *
         * @return
         */
        @SuppressLint("Assert")
        public boolean getSwitchBlueTooth() {
            assert (bluetoothAdapter != null);
            return bluetoothAdapter.isEnabled();
        }
    
        /**
         * 打开蓝牙
         *
         * @param activity
         */
        public void trueOnBlueTooth(Activity activity) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivity(intent);
        }
    
        /**
         * 关闭蓝牙
         */
        public void trueOffBlueTooth() {
            bluetoothAdapter.disable();
        }
    
        /**
         * 查找设备
         */
        @SuppressLint("Assert")
        public void findDevice() {
            assert (bluetoothAdapter != null);
            bluetoothAdapter.startDiscovery();
        }
    
        /**
         * 取消查找设备
         */
        @SuppressLint("Assert")
        public void cancelDevice() {
            assert (bluetoothAdapter != null);
            bluetoothAdapter.cancelDiscovery();
        }
    
        /**
         * 获取绑定设备
         *
         * @return
         */
        public List<PrinterBean> getBlueToothDeviceList() {
            List<PrinterBean> printerBeans = new ArrayList<PrinterBean>();
            Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
            for (BluetoothDevice bondedDevice : bondedDevices) {
                PrinterBean printerBean = new PrinterBean();
                printerBean.setMac(bondedDevice.getAddress());
                printerBean.setName(bondedDevice.getName());
                printerBean.setUuid(Arrays.toString(bondedDevice.getUuids()));
                printerBeans.add(printerBean);
            }
            return printerBeans;
        }
    
        // 与设备解除配对
        public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception {
            Method removeBondMethod = btClass.getMethod("removeBond");
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
            return returnValue.booleanValue();
        }
    
        // 与设备配对
        public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
            Method createBondMethod = btClass.getMethod("createBond");
            Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
            return returnValue.booleanValue();
        }
    
    
        /**
         *
         * @param clsShow
         */
        public void printAllInform(Class clsShow) {
            try {
                // 取得所有方法
                Method[] hideMethod = clsShow.getMethods();
                int i = 0;
                for (; i < hideMethod.length; i++) {
                    Log.e("method name", hideMethod[i].getName());
                }
                // 取得所有常量
                Field[] allFields = clsShow.getFields();
                for (i = 0; i < allFields.length; i++) {
                    Log.e("Field name", allFields[i].getName());
                }
            } catch (SecurityException e) {
                // throw new RuntimeException(e.getMessage());
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // throw new RuntimeException(e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:蓝牙帮助类

          本文链接:https://www.haomeiwen.com/subject/jtkrrxtx.html