gpasse
gpasse

Reputation: 4489

Nativescript using android

I am new to NativeScript. I am trying to figure out how to use the Bluetooth Native Api (Classic not LE). I have a hard time finding out how I can interact with the bluetooth api.

The first wall I hit is using android in my typescript code.

For exemple in a component if i do :

ngOnInit(): void {
    console.info(android.os.Build.VERSION.SDK_INT)
} 

I get the error:

JS: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'os' of undefined

Any suggestions ?

Upvotes: 1

Views: 296

Answers (1)

Nick Iliev
Nick Iliev

Reputation: 9670

To have access (and intelliSense which is even better) via TypeScript to the native APIs you need the generated declarations files. Fortunately, you do not have to provide these files as they are already avaiable via the tns-platform-declarations plugin

To install the declarations do the fiollwing in your Angular project

npm i tns-platform-declarations --save-dev

And then create references.d.ts file with the following content:

/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />

Finally, modify the content of your tsconfig.json file to include the following:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "lib": [
            "es6",
            "dom"
        ]
    }
}

If for some reason you do not need IntelliSense you can skip all this and simply give a type to android in the beginning of your *.ts file

declare var android: any;

However, I would strongly recommend the first approach as it will give you really nice IntelliSense for the really enormous native APIs

Upvotes: 3

Related Questions