HMS-Core / hms-scan-demo

Sample code for demonstrating Huawei HMS ScanKit capabilities. It illustrates how to help developers quickly build code scanning capabilities.
https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/scan-introduction-4?ha_source=hms1
Apache License 2.0
302 stars 56 forks source link

扫描过程中调用系统振动无效 #62

Open Nathanielnim opened 2 years ago

Nathanielnim commented 2 years ago

需求:在扫描或识别成功后调用系统振动以便提示用户操作完成 环境:Vivo Y32、Android 11。使用Customized View Mode方式接入统一扫码服务 相关代码:

scanView?.setOnResultCallback { result ->
            scanView?.pauseContinuouslyScan() // 暂停扫码功能,避免重复扫码
            vibrate()
        }

// 振动
private fun vibrate() {
        val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        if (vibrator.hasVibrator()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vibrator.vibrate(
                    VibrationEffect.createOneShot(
                        500L,
                        VibrationEffect.DEFAULT_AMPLITUDE
                    )
                )
            } else {
                vibrator.vibrate(500L)
            }
        }
    }

振动代码是没问题的,我在其他页面中都是调用成功的

Xuejiao-Shi commented 2 years ago

权限:
<uses-permission android:name="android.permission.VIBRATE"/> 将下列代码放到您想要实现震动的地方 代码:

Vibrator vibrator = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(50);
Nathanielnim commented 2 years ago

权限在之前就已经加过了

Xuejiao-Shi commented 2 years ago

您好,您可以排查下Vivo Y32是否有振动器。 tract boolean hasVibrator():判断硬件是否有振动器

Nathanielnim commented 2 years ago

有的,这段代码我在其他页面运行过,振动是正常的,在扫描页就不行

Xuejiao-Shi commented 2 years ago

您好,请在vibrate方法中打印日志,确认是否调用了vibrate();方法。 没有调用vibrate()方法就是您存放的位置、场景不对。

Nathanielnim commented 2 years ago

之前加过日志也debug看了的,代码是调了的