首页 > 解决方案 > Web-Bluetooth:导航器的自定义类型

问题描述

我正在为我的设备编写一个浏览器应用程序,它将通过蓝牙进行通信。

有人可以帮我打字吗?

我正在尝试访问navigator对象中的蓝牙属性:

let mobileNavigatorObject: Navigator = navigator;
        mobileNavigatorObject.bluetooth.requestDevice(
            {
                filters: [
                    {
                        namePrefix: "My",
                    }
                ],
                optionalServices: ["service uuid"]
            }
        )

但我有:error TS2339: Property 'bluetooth' does not exist on type 'Navigator'.

即使在安装 @type/web-bluetooth 之后

有2种来源node_modules/typescript/lib/lib.dom.d.ts

interface Navigator extends NavigatorID, NavigatorOnLine, NavigatorContentUtils, NavigatorStorageUtils, MSNavigatorDoNotTrack, MSFileSaver, NavigatorBeacon, NavigatorConcurrentHardware, NavigatorUserMedia, NavigatorLanguage, NavigatorStorage, NavigatorAutomationInformation {
    readonly activeVRDisplays: ReadonlyArray<VRDisplay>;
    readonly authentication: WebAuthentication;
    readonly cookieEnabled: boolean;
    readonly doNotTrack: string | null;
    gamepadInputEmulation: GamepadInputEmulationType;
    readonly geolocation: Geolocation;
    readonly maxTouchPoints: number;
    readonly mimeTypes: MimeTypeArray;
    readonly msManipulationViewsEnabled: boolean;
    readonly msMaxTouchPoints: number;
    readonly msPointerEnabled: boolean;
    readonly plugins: PluginArray;
    readonly pointerEnabled: boolean;
    readonly serviceWorker: ServiceWorkerContainer;
    readonly webdriver: boolean;
    getGamepads(): (Gamepad | null)[];
    getVRDisplays(): Promise<VRDisplay[]>;
    javaEnabled(): boolean;
    msLaunchUri(uri: string, successCallback?: MSLaunchUriCallback, noHandlerCallback?: MSLaunchUriCallback): void;
    requestMediaKeySystemAccess(keySystem: string, supportedConfigurations: MediaKeySystemConfiguration[]): Promise<MediaKeySystemAccess>;
    vibrate(pattern: number | number[]): boolean;
}

node_modules/@types/web-bluetooth/index.d.ts


interface Navigator {
    bluetooth: Bluetooth;
}

tsConfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

如何选择正确的?

标签: javascriptangulartypescripttypesweb-bluetooth

解决方案


推荐阅读