How to check if the device runs Capacitor?

Hi,
I have an app done with Ionic5 and Capacitor, I need to have different code if the app runs on a browser or on a real device.

However, using the Platform the command:

this.plaform.is(‘capacitor’))

always returns false.

How can I understand if I’m on a real device?
Thank you very much

cld

While you wait for more direct answers, I’d like to explore the possibility that you might be able to approach this from another angle. What exactly necessitates different code? What bad thing happens if you don’t do this check?

1 Like

Hi @rapropos, the reason why it wasn’t working was that I removed the www directory and didn’t launch again the commands:

    - ionic build
    - npx cap add android
    - npx cap copy android
    - npx cap update android

Now it works.
This is my first app with Ionic5/Capacitor and I don’t know how to do many things.

The different code is to manage the fact that on the web the app can’t use the Barcode reader.

Thank you

cld

Is that really necessarily true, though? Laptops have cameras, and I’m pretty sure Capacitor can feed images to barcode scanners even without being on a device. I use the MediaDevices API to discover if an app is running somewhere that it can potentially scan barcodes.

1 Like

I didn’t know these Api, thank you.
How are these Api usable on a Ionic app?
I can’t see how to import them, or are they simply already included in the Javascript core?

cld

I do this:

export class CapacitiesService {
  private mediaDevices$: Observable<MediaDeviceInfo[]>;
  private hasCamera$: Observable<boolean>;
  constructor(...) {
    this.mediaDevices$ = from(navigator.mediaDevices.enumerateDevices());
    this.hasCamera$ = this.mediaDevices$.pipe(map(mds => {
      for (let md of mds) {
        if (md.kind === "videoinput") {
          return true;
        }
      }
      return false;
    }));
  }
 
  hasCamera(): Observable<boolean> {
    return this.hasCamera$;
  }
}
1 Like

thank you for sharing this