2

I am using the Device API from Capacitor (https://capacitorjs.com/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");
},
Vercors
  • 123
  • 2
  • 7

4 Answers4

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 {

 }
1

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');
  }
1

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);
}
JMK
  • 27,273
  • 52
  • 163
  • 280
0

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