core
    正在准备搜索索引...

    接口 YmxDevice

    设备

    interface YmxDevice {
        detectCode(data: DetectCodeData): Promise<CodeResult[]>;
        getBatteryInfo(): Promise<GetBatteryInfoResult>;
        getKeepScreenOn(): Promise<KeepScreenOnResult>;
        getScreenBrightness(): Promise<ScreenBrightness>;
        isSupportAccelerometer(): Promise<boolean>;
        isSupportCompass(): Promise<boolean>;
        isSupportGyroscope(): Promise<boolean>;
        makePhoneCall(data: MakePhoneCallData): Promise<void>;
        offAccelerometerChange(listener?: Function): Promise<void>;
        offCompassChange(listener?: Function): Promise<void>;
        offGyroscopeChange(listener?: Function): Promise<void>;
        onAccelerometerChange(
            listener: (res: AccelerometerResult) => void,
        ): Promise<void>;
        onCompassChange(listener: (res: CompassResult) => void): Promise<void>;
        onGyroscopeChange(listener: (res: GyroscopeResult) => void): Promise<void>;
        scanCode(data: ScanCodeData): Promise<CodeResult>;
        sendSms(data: SendSmsData): Promise<void>;
        setKeepScreenOn(data: KeepScreenOnData): Promise<void>;
        setScreenBrightness(data: ScreenBrightness): Promise<void>;
        startAccelerometer(data: AccelerometerData): Promise<void>;
        startCompass(data: CompassData): Promise<void>;
        startGyroscope(data: GyroscopeData): Promise<void>;
        stopAccelerometer(): Promise<void>;
        stopCompass(): Promise<void>;
        stopGyroscope(): Promise<void>;
        vibrateLong(): Promise<void>;
        vibrateShort(data: VibrateShortData): Promise<void>;
    }

    层级 (查看层级一览)

    索引
    • 识别图片中的二维码、条码,返回结果数组。

      参数

      返回 Promise<CodeResult[]>

      //放一个 id 为 htmlFileInput 文件选择标签到页面上,<input type="file" accept="image/*" id="htmlFileInput" />
      const htmlFileInput = document.getElementById('htmlFileInput');
      if (htmlFileInput.files.length > 0) {
      const reader = new FileReader();
      reader.onload = function(event) {
      const buffer = new Uint8Array(event.target.result);
      //识别
      ymx.detectCode({
      imageData: buffer
      })
      .then(res => {
      alert(JSON.stringify(res));
      })
      .catch(err => {
      alert(JSON.stringify(err));
      });
      };
      reader.onerror = function() {
      alert('读取文件失败');
      };
      //读取文件
      const file = htmlFileInput.files[0];
      reader.readAsArrayBuffer(file);
      } else {
      alert("请选择文件");
      }
      //本地临时文件
      const localFile = 'fs://temp/img.png';
      try {
      //下载图片
      await ymx.downloadFile({
      url: 'https://www.yimenapp.com/assets/img/qr-test.png',
      filePath: localFile
      });
      //识别
      const res = await ymx.detectCode({
      filePath: localFile
      });
      alert(JSON.stringify(res));
      } catch (err) {
      alert(err.message);
      } finally {
      //删除临时文件
      const fs = ymx.getFileSystemManager();
      if (await fs.exist({ path: localFile })) {
      await fs.remove({ path: localFile });
      }
      }
    • 检测设备是否支持加速计

      返回 Promise<boolean>

      const isSupport = await ymx.isSupportAccelerometer();
      alert(isSupport ? '支持' : '不支持');
    • 检测设备是否支持罗盘

      返回 Promise<boolean>

      const isSupport = await ymx.isSupportCompass();
      alert(isSupport ? '支持' : '不支持');
    • 检测设备是否支持陀螺仪

      返回 Promise<boolean>

      const isSupport = await ymx.isSupportGyroscope();
      alert(isSupport ? '支持' : '不支持');
    • 移除加速计数据事件的监听函数,不传 listener 则移除所有监听函数

      参数

      • 可选listener: Function

      返回 Promise<void>

      //移除全部加速计监听器
      await ymx.offAccelerometerChange();
    • 移除罗盘数据事件的监听函数,不传 listener 则移除所有监听函数

      参数

      • 可选listener: Function

      返回 Promise<void>

      //移除全部罗盘监听器
      await ymx.offCompassChange();
    • 移除陀螺仪数据事件的监听函数,不传 listener 则移除所有监听函数

      参数

      • 可选listener: Function

      返回 Promise<void>

      //移除全部陀螺仪监听器
      await ymx.offGyroscopeChange();
    • 监听罗盘数据事件

      参数

      返回 Promise<void>

      await ymx.onCompassChange((res) => {
      console.log(`Compass x:${res.x}, y:${res.y}, z:${res.z}`);
      });
    • 监听陀螺仪数据事件

      参数

      返回 Promise<void>

      await ymx.onGyroscopeChange((res) => {
      console.log(`Gyroscope x:${res.x}, y:${res.y}, z:${res.z}`);
      });
    • 调起客户端扫码界面进行扫码

      参数

      返回 Promise<CodeResult>

      const res = await ymx.scanCode({
      scanType: ['barCode', 'qrCode']
      });
      alert(JSON.stringify(res));
      ymx.scanCode({
      onlyFromCamera: true
      }).then(res => {
      alert(JSON.stringify(res));
      });
    • 设置是否保持常亮状态

      参数

      返回 Promise<void>

      ymx.setKeepScreenOn({
      keepScreenOn: true
      }).then(() => {
      alert('已开启屏幕常亮');
      });
      ymx.setKeepScreenOn({
      keepScreenOn: false
      }).then(() => {
      alert('已关闭屏幕常亮');
      });
    • 停止监听加速计数据

      返回 Promise<void>

      await ymx.stopAccelerometer();
      
    • 停止监听罗盘数据

      返回 Promise<void>

      await ymx.stopCompass();
      
    • 停止监听陀螺仪数据

      返回 Promise<void>

      await ymx.stopGyroscope();
      
    • 使手机发生较长时间的振动(400 ms)

      返回 Promise<void>

      await ymx.vibrateLong();