最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught (in promise) Error: "Device" plugin is not implemented on android - Stack Overflow

programmeradmin0浏览0评论

I am using the Device API from Capacitor () along with Ionic Framework and VueJS. The goal is to retrieve the UUID from an Android device.

When opening the application in my browser, I can see the device info that I'm logging. However, when opening it on an Android Device / Android Studio I get this error:

Uncaught (in promise) Error: "Device" plugin is not implemented on android

As far as I know the plugin is supported on Android.. What am I doing wrong?

Code:

import { Device } from "@capacitor/device";
async getDeviceInfo() {
  const info = await Device.getId();
  this.uuid = info.uuid;
  alert(this.uuid);
  const moreInfo = await Device.getInfo();
  const battery = await Device.getBatteryInfo();
  alert(JSON.stringify(moreInfo));
  alert(JSON.stringify(battery));
  alert("test");
},

I am using the Device API from Capacitor (https://capacitorjs./docs/apis/device#deviceinfo) along with Ionic Framework and VueJS. The goal is to retrieve the UUID from an Android device.

When opening the application in my browser, I can see the device info that I'm logging. However, when opening it on an Android Device / Android Studio I get this error:

Uncaught (in promise) Error: "Device" plugin is not implemented on android

As far as I know the plugin is supported on Android.. What am I doing wrong?

Code:

import { Device } from "@capacitor/device";
async getDeviceInfo() {
  const info = await Device.getId();
  this.uuid = info.uuid;
  alert(this.uuid);
  const moreInfo = await Device.getInfo();
  const battery = await Device.getBatteryInfo();
  alert(JSON.stringify(moreInfo));
  alert(JSON.stringify(battery));
  alert("test");
},
Share Improve this question edited Jun 1, 2021 at 9:02 Vercors asked Jun 1, 2021 at 8:30 VercorsVercors 1633 silver badges12 bronze badges 4
  • "import { Device } from "@capacitor/device"; this is work for capacitor 3.0 and this import { Plugins } from '@capacitor/core'; const { Device } = Plugins; capacitor 2.0 – Ravi Ashara Commented Jun 1, 2021 at 12:16
  • Odd: I did update to Capacitor 3, however the first line doesn't work. I tried the second one and indeed it works ;).. – Vercors Commented Jun 1, 2021 at 13:10
  • check stackoverflow./a/67671607/1351469 – jcesarmobile Commented Jun 10, 2021 at 13:04
  • Thanks, that answers my question – Vercors Commented Jun 10, 2021 at 13:09
Add a ment  | 

4 Answers 4

Reset to default 3

the same error happened to me and I fixed it based on the documentation of the capacitor update for Android, removing the onCreate method from the MainActivity class.

 public class MainActivity extends BridgeActivity {

 }

With Capacitor 4, you need to call registerPlugin before super.onCreate.

public class MainActivity extends BridgeActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  registerPlugin(MyPlugin.class);
  super.onCreate(savedInstanceState);
}

Instead of:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  registerPlugin(MyPlugin.class);
}

It's interesting to know that with Capacitor 3.x that they have moved the uuid into it's own function.

import { Device } from '@capacitor/device';

  async getdevinfo(){
    console.log('trying to get device info');
// works in browser
      const info = await Device.getId();
      console.log(info);
      console.log(info.uuid);

//wont work in browser
      const moreInfo = await Device.getInfo();
      const battery = await Device.getBatteryInfo();
      console.log(JSON.stringify(moreInfo));
      console.log(JSON.stringify(battery));

    console.log('post device info request');
  }

Did you run npx cap sync after installing @capacitor/device? The mand is always needed when you add new plugins.

发布评论

评论列表(0)

  1. 暂无评论