Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

16.11. 接收 USB 设备广播

		
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            // 设备连接
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            // 设备断开
        }
    }
};

// 注册receiver
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(usbReceiver, filter);		
		
		

16.11.1. 

			
package com.netkiller.conference.receiver

import android.Manifest
import android.app.DownloadManager
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.usb.UsbManager
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.telephony.TelephonyManager
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.Utils


class StaticBroadcastReceiver : BroadcastReceiver() {
    //    private val cache = Cache()
    private val aigcSpeech: AigcSpeech = AigcSpeech.getInstance()
    private val TAG: String = StaticBroadcastReceiver::class.java.simpleName

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        Log.d(TAG, "Static broadcast Action: " + action)
        val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
//        var device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)

        try {
            when (action) {

                UsbManager.ACTION_USB_DEVICE_ATTACHED -> {
                    if (AndroidManager.Device.usbAudio(context)) {
                        AndroidManager.Device.scanMicrophone()
                        aigcSpeech.speaking("USB麦克风连接")
                    }
                }

                UsbManager.ACTION_USB_DEVICE_DETACHED -> {
                    AndroidManager.Device.resetMicrophone()
                    aigcSpeech.speaking("USB麦克风断开")
                    Handler(Looper.getMainLooper()).postDelayed({
                        if (AndroidManager.Device.scanMicrophone()) {
                            aigcSpeech.speaking("切换到蓝牙麦克风")
                        }
                        Log.d(TAG, "Bluetooth connected")
                    }, 1000)
                }

            }

        } catch (e: Exception) {
            Log.d(TAG, e.toString())
        }
    }


}