BluetoothA2dp藍牙音箱的連接


1:權限

<uses-feature
            android:name="android.hardware.bluetooth_le"
            android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

2:打開藍牙

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

 if (bluetoothAdapter == null) {
      finish();
     } else if (!bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.enable();
 }

 3:注冊廣播以及掃描

 // 開始掃描
bluetoothAdapter.startDiscovery(); /*** 注冊廣播**/ IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(broadcastReceiver,filter); IntentFilter filter2=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(broadcastReceiver,filter2); private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action == BluetoothDevice.ACTION_FOUND) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() == BluetoothDevice.BOND_BONDED) { String s = device.getName() + "*" + device.getAddress(); if(device.getAddress().equals(blueId)){ bluetoothDevice=device; Log.d(TAG, "onReceive: --空2"); } datas.add(device.getName() + "*" + device.getAddress()); Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress()); }else if(device.getBondState()!=BluetoothDevice.BOND_BONDED){ String s=device.getName()+"*"+device.getAddress(); Log.d(TAG, "onReceive: ---"+device.getName()+"---"+device.getAddress()); if(device.getAddress().equals(blueId)){ bluetoothDevice=device; Log.d(TAG, "onReceive: --空1"); } if(datas.indexOf(s)==-1){ datas.add(device.getName()+"*"+device.getAddress()); } } }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ Log.d(TAG, "onReceive: ---搜索完成"); Toast.makeText(MainActivity.this,"搜索完成!",Toast.LENGTH_SHORT).show(); } } };

  

4:配對

 /***傳入的參數是 需要配對的bluetoothdevice */
   public void blePairing(BluetoothDevice bluetoothDevice){

           this.bluetoothDevice=bluetoothDevice;
           Method m = null;
           try {
               m = bluetoothDevice.getClass().getDeclaredMethod("createBond",new Class[]{});
               m.setAccessible(true);
               Boolean originalResult = null;
               try {
                   originalResult = (Boolean) m.invoke(bluetoothDevice);
                   boolean result = originalResult.booleanValue();
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               } catch (InvocationTargetException e) {
                   e.printStackTrace();
               }

           } catch (NoSuchMethodException e) {
               e.printStackTrace();
           }


       }

5:連接

連接  connectA2dp(bluetoothDevice);

 /**
     * 連接 音箱device
     * @param bluetoothDevice
     */
    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();
        }
    }

6: 連接需要的BluetoothA2dp對象需要通過監聽得到

   /***
         * blueA2dp 監聽
         */
        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;
                }
            }
        };

7:code 

http://pan.baidu.com/s/1bBuYPS

  


免責聲明!

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



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