更新记录

1.3.0(2022-10-19)

本次主要更新: 1.andorid 增加距离感应器,靠近屏幕变暗功能,增加相关字段返回

1.2.0(2022-10-12)

本次主要更新: 1.增加ios 相关方法 2.andorid版本修改返回的json,如果使用过上一版,需要修改

1.1.0(2022-10-10)

本次主要更新: 1.增加andorid版本,增加环境光、气压、距离数据、支持的传感器列表

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:4.4 - 12.0 armeabi-v7a:支持,arm64-v8a:支持,x86:支持 适用版本区间:9 - 16

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


KJ-MotionManager

获取传感器陀螺仪、磁场、环境光、气压、距离数据(靠近屏幕变暗)、判断传感器是否可用(ios、andorid)

导入插件

const KJMotionManager = uni.requireNativePlugin('KJ-MotionManager');

使用

<template>
    <view>
        <button type="primary" @click="getSensorList">获取当前设备支持的传感器列表(andorid)</button>
        <button type="primary" @click="gyroAvailable">陀螺仪传感器是否可用(ios)</button>
        <button type="primary" @click="magnetometerAvailable">磁场传感器是否可用(ios)</button>
        <button type="primary" @click="pressureAvailable">气压传感器是否可用(ios)</button>
        <button type="primary" @click="pressureAvailable_ios15">气压传感器是否可用,ios15新方法(ios)</button>

        <button type="primary" @click="startGyroUpdates">获取陀螺仪数据</button>
        <button type="primary" @click="stopGyroUpdates">停止获取陀螺仪数据</button>

        <button type="primary" @click="startMagnetometerUpdates">获取磁场数据</button>
        <button type="primary" @click="stopMagnetometerUpdates">停止获取磁场数据</button>

        <button type="primary" @click="startLightUpdates">获取环境光数据(andorid)</button>
        <button type="primary" @click="stopLightUpdates">停止获取环境光数据(andorid)</button>

        <button type="primary" @click="startPressureUpdates">获取气压数据</button>
        <button type="primary" @click="stopPressureUpdates">停止获取气压数据</button>
        <button type="primary" @click="startPressureUpdates_ios15">获取气压数据(ios15)</button>
        <button type="primary" @click="stopPressureUpdates_ios15">停止获取气压数据(ios15)</button>

        <button type="primary" @click="startProximityUpdates">获取距离数据</button>
        <button type="primary" @click="stopProximityUpdates">停止获取距离数据</button>

        <view>陀螺仪 是否有传感器:andorid={{isGyro}} ios={{isGyroIOS}} 数据:{{Gyro}}</view>
        <view>磁场是 否有传感器:andorid={{isMagnetometer}} ios={{isMagnetometerIOS}} 数据:{{Magnetometer}}</view>
        <view>环境光 是否有传感器(andorid):{{isLight}} 数据:{{Light}}</view>
        <view>气压 是否有传感器:andorid={{isPressure}} ios={{isPressureIOS}} ios15={{isPressureIOS15}} 数据:{{Pressure}}
            ios15={{PressureIOS15}}</view>
        <view>距离 是否有传感器:andorid={{isProximity}} 数据:{{Proximity}}</view>
    </view>
</template>

<script>
    const KJMotionManager = uni.requireNativePlugin('KJ-MotionManager');

    export default {
        data() {
            return {
                Gyro: '',
                isGyro: false,
                isGyroIOS: false,
                Magnetometer: '',
                isMagnetometer: false,
                isMagnetometerIOS: false,
                Light: '',
                isLight: false,
                Pressure: '',
                PressureIOS15: '',
                isPressure: false,
                isPressureIOS: false,
                isPressureIOS15: false,
                Proximity: '',
                isProximity: false
            }
        },
        onLoad() {
            if (plus.os.name == 'Android') {
                this.getSensorList();
            } else {
                this.gyroAvailable();
                this.magnetometerAvailable();
                this.pressureAvailable();
                this.pressureAvailable_ios15();
            }
        },
        methods: {
            getSensorList() {
                KJMotionManager.getSensorList((res) => {
                    /**
                     * 返回json字段说明
                     * type - 传感器类型 1(加速度传感器) 2(磁场传感器) 4(陀螺仪传感器) 5(环境光传感器) 6(气压传感器) 8(距离传感器) ...
                     * name - 设备名称
                     * version - 设备版本
                     * vendor - 供应商
                     * */
                    console.log("getSensorList:" + JSON.stringify(res));
                    for (var i = 0; i < res.length; i++) {
                        var dic = res[i];
                        if (dic.type == 4) {
                            this.isGyro = true
                        } else if (dic.type == 2) {
                            this.isMagnetometer = true
                        } else if (dic.type == 5) {
                            this.isLight = true
                        } else if (dic.type == 6) {
                            this.isPressure = true
                        } else if (dic.type == 8) {
                            this.isProximity = true
                        }
                    }
                });
            },
            gyroAvailable() {
                KJMotionManager.gyroAvailable((res) => {
                    this.isGyroIOS = res.result
                })
            },
            magnetometerAvailable() {
                KJMotionManager.magnetometerAvailable((res) => {
                    this.isMagnetometerIOS = res.result
                })
            },
            pressureAvailable() {
                KJMotionManager.pressureAvailable((res) => {
                    this.isPressureIOS = res.result
                })
            },
            pressureAvailable_ios15() {
                KJMotionManager.pressureAvailable_ios15((res) => {
                    this.isPressureIOS15 = res.result
                })
            },
            startGyroUpdates() {
                /**
                 * updateInterval 更新频率 单位s
                 * samplingPeriodUs - 获取传感器频率
                 * 0(最快,延迟最小,同时也最消耗资源,一般只有特别依赖传感器的应用使用该频率,否则不推荐) 0ms
                 * 1(适合游戏的频率,一般有实时性要求的应用适合使用这种频率) 20ms
                 * 2(适合普通应用的频率,这种模式比较省电,而且系统开销小,但延迟大) 60ms
                 * 3(正常频率,一般对实时性要求不高的应用适合使用这种频率) 200ms
                 * */
                KJMotionManager.startGyroUpdates({
                    "updateInterval": 0.1, //ios有效
                    "samplingPeriodUs": 3 //andorid有效
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - ios返回字典  andorid返回数组(value[0]:x,value[1]:y,value[2]:y)
                     * */
                    this.Gyro = JSON.stringify(res);
                    console.log(JSON.stringify(res));
                });
            },
            stopGyroUpdates() {
                KJMotionManager.stopGyroUpdates();
            },
            startMagnetometerUpdates() {
                KJMotionManager.startMagnetometerUpdates({
                    "updateInterval": 0.1, //ios有效
                    "samplingPeriodUs": 3 //andorid有效
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - ios返回字典  andorid返回数组(value[0]:x,value[1]:y,value[2]:y)
                     * */
                    this.Magnetometer = JSON.stringify(res);
                    console.log(JSON.stringify(res));
                });
            },
            stopMagnetometerUpdates() {
                KJMotionManager.stopMagnetometerUpdates();
            },
            startLightUpdates() {
                KJMotionManager.startLightUpdates({
                    "samplingPeriodUs": 0
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - andorid返回数组(value[0]:亮度值)
                     * */
                    this.Light = JSON.stringify(res);
                    console.log("startLightUpdates:" + JSON.stringify(res));
                });
            },
            stopLightUpdates() {
                KJMotionManager.stopLightUpdates();
            },
            startPressureUpdates() {
                KJMotionManager.startPressureUpdates({
                    //"updateInterval": 0.1, //气压数据 ios没有这个参数
                    "samplingPeriodUs": 0
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - ios返回字典(value.relativeAltitude:高度m value.pressure:气压值)  
                     * andorid返回数组(value[0]:气压值)
                     * */
                    this.Pressure = JSON.stringify(res);
                    console.log("stopPressureUpdates:" + JSON.stringify(res));
                });
            },
            stopPressureUpdates() {
                KJMotionManager.stopPressureUpdates();
            },
            startPressureUpdates_ios15() {
                KJMotionManager.startPressureUpdates_ios15({
                    //"samplingPeriodUs": 0
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - ios返回字典(value.altitude:高度m value.accuracy: value.precision:)  
                     * */
                    this.PressureIOS15 = JSON.stringify(res);
                    console.log("stopPressureUpdates:" + JSON.stringify(res));
                });
            },
            stopPressureUpdates_ios15() {
                KJMotionManager.stopPressureUpdates_ios15();
            },
            startProximityUpdates() {
                KJMotionManager.startProximityUpdates({
                    "samplingPeriodUs": 0
                }, (res) => {
                    /**
                     * 返回参数说明:
                     * value - ios返回字典(value.proximityState:是否靠近,靠近时屏幕会黑) andorid返回数组(value[0]:距离)
                     * */
                    this.Proximity = JSON.stringify(res);
                    console.log("startProximityUpdates:" + JSON.stringify(res));
                });
            },
            stopProximityUpdates() {
                KJMotionManager.stopProximityUpdates();
            },
        }
    }
</script>

隐私、权限声明

1. 本插件需要申请的系统权限列表:

andorid 距离感应器需要:android.permission.WAKE_LOCK

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问