• 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
    

      

  • 相关阅读:
    ORM开发之解析lambda实现完整查询(附测试例子)
    ORM开发之解析lambda实现group查询(附测试例子)
    ORM之殇,我们需要什么样的ORM框架?
    公开封尘已久的即时通讯源码
    javascript中的this与函数讲解
    javascript中的操作符详解1
    javascript中的继承与深度拷贝
    javascript之Object.defineProperty的奥妙
    javascript之活灵活现的Array
    jquery.Callbacks的实现
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/7840615.html
Copyright © 2020-2023  润新知