藍牙音箱的連接和斷開


藍牙掃描等操作的所需要權限:

 

    <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()+"\n");
                        return device;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM