• 蓝牙音箱的连接和断开


    蓝牙扫描等操作的所需要权限:

        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

      

    1.需要用到的类

        public BluetoothAdapter bluetoothAdapter;
        private BluetoothA2dp bluetoothA2dp;
    

    2.初始化

      bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
      bluetoothA2dp.getProfileProxy(MainActivity.this, mListener, BluetoothProfile.A2DP); 

      

    3.监听

        private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceDisconnected(int profile) {
                if (profile == BluetoothProfile.A2DP) {
                    bluetoothA2dp = null;
                }
            }
    
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                if (profile == BluetoothProfile.A2DP) {
                    bluetoothA2dp = (BluetoothA2dp) proxy;
                }
            }
        };
    

    4.设置优先权

      /**
         * bluetooth
         */
        public void setPriority(BluetoothDevice device, int priority) {
            try {
                Method connectMethod = BluetoothA2dp.class.getMethod("setPriority", BluetoothDevice.class, int.class);
                connectMethod.invoke(bluetoothA2dp, device, priority);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

     

    5.断开蓝牙连接

      public static void disConnected(BluetoothDevice device,BluetoothA2dp bluetoothA2dp){
            try {
                Method connectMethod = BluetoothA2dp.class.getMethod("disconnect", BluetoothDevice.class);
                connectMethod.invoke(bluetoothA2dp, device);
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("Tag", "closeProiority: "+e.toString());
            }
        }
    

    6.配对

        private void pairBluetooth(BluetoothDevice device){
            Method createBond = null;
            try {
                createBond = BluetoothDevice.class.getMethod("createBond");
                createBond.invoke(device);
                //device.createBond();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

      

    7.连接操作

        /**
         * bluetooth 连接操作
         */
        private void connectA2dp(BluetoothDevice bluetoothDevice) {
    
            if (bluetoothA2dp == null || bluetoothDevice == null) {
                return;
            }
            setPriority(bluetoothDevice, 100);
            try {
                Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class);
                connectMethod.invoke(bluetoothA2dp, bluetoothDevice);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    8. 获取当前连接的蓝牙设备

        /**
         * bluetooth    获取已经连接的蓝牙的信息
         */
        private BluetoothDevice getBluetoothConnectedDevice() {
    
            Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
            try {
                Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
                //打开权限
                method.setAccessible(true);
                int state = (int) method.invoke(mBluetoothAdapter, (Object[]) null);
                if (state == BluetoothAdapter.STATE_CONNECTED) {
                    Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
                    for (BluetoothDevice device : devices) {
                        Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
                        method.setAccessible(true);
                        boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
                        if (isConnected) {
                            //textViewBluetooth.append("当前连接的蓝牙信息:"+device.getName()+"
    ");
                            return device;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    

      

     

  • 相关阅读:
    Appium+python自动化17-appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器
    Appium+python自动化18-启动iOS模拟器APP源码案例
    Appium+python自动化16-在Mac上环境搭建
    Appium+python自动化14-native和webview切换
    Appium+python自动化15-查看webview上元素(DevTools)
    Appium+python自动化13-appium元素定位
    Appium+python自动化11-AVD 模拟器
    Appium+python自动化12-adb必知必会的几个指令
    Appium+python自动化10-SDK Manager
    jenkins+gitlab配置
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/9332376.html
Copyright © 2020-2023  润新知