AndroidPlatformBridge.ts 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { native, sys } from 'cc';
  2. import { PlatformBridge } from './PlatformBridge';
  3. import { PlatformType } from './PlatformBridge';
  4. const LOCAL_DEVICE_CODE = "AndroidDeviceCode";
  5. export class AndroidPlatformBridge extends PlatformBridge {
  6. readonly type = PlatformType.Android;
  7. getUrlParam(_name: string): string | null {
  8. console.error('[Android] 获取深度链接参数能力未实现');
  9. return null;
  10. }
  11. getDeviceID(): string | null {
  12. let deviceCode = sys.localStorage.getItem(LOCAL_DEVICE_CODE);
  13. if (!!deviceCode && deviceCode.length > 0) return deviceCode;
  14. deviceCode = native.reflection.callStaticMethod("com/cocos/helper/Utils", "getDeviceCode", "()Ljava/lang/String;");
  15. if (!deviceCode || deviceCode.length === 0) {
  16. console.warn(`[Android] 获取设备号为空,自动创建设备号...`);
  17. deviceCode = "";
  18. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  19. for (let i = 0; i < 50; i++) {
  20. deviceCode += characters.charAt(Math.floor(Math.random() * characters.length));
  21. }
  22. }
  23. sys.localStorage.setItem(LOCAL_DEVICE_CODE, deviceCode);
  24. return deviceCode;
  25. }
  26. }