Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding accessibility to AltBeacon SDK options #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ demo/platforms
demo/node_modules
node_modules
.idea
/.project
30 changes: 28 additions & 2 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Observable} from 'data/observable';
import {
BeaconRegion, Beacon, BeaconCallback,
BeaconParserType, RangingOptions,
BeaconLocationOptions, BeaconLocationOptionsIOSAuthType, BeaconLocationOptionsAndroidAuthType
} from "nativescript-ibeacon/nativescript-ibeacon.common";
import {NativescriptIbeacon} from "nativescript-ibeacon";
Expand All @@ -17,17 +18,42 @@ export class HelloWorldModel extends Observable implements BeaconCallback {
super();

console.log('Hello World Model constructed');

let options: BeaconLocationOptions = {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType.Always,
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType.Coarse,
androidAuthorisationDescription: "Location permission needed"
androidAuthorisationDescription: "Location permission needed",
parserTypes: []
};

let rangingOptions: RangingOptions = {
// example of 1 sec foreground scanning interval, null will use default interval
// foregroundScanInterval: 1000,
foregroundScanInterval: null,
// example of 3 sec background scanning interval, null will use default interval
// backgroundScanInterval: 3000
backgroundScanInterval: null
};

// example of adding specific parser types. An empty array (default) will look for all parserTypes
// options.parserTypes.push(BeaconParserType.AltBeacon);
// options.parserTypes.push(BeaconParserType.EddystoneTLM);
// options.parserTypes.push(BeaconParserType.EddystoneUID);
// options.parserTypes.push(BeaconParserType.EddystoneURL);
// options.parserTypes.push(BeaconParserType.IBeacon);
this.nativescriptIbeacon = new NativescriptIbeacon(this, options);
this.region = new BeaconRegion("HelloID", "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6");

// example of ranging for a specific UUID
// this.region = new BeaconRegion("HelloID", "61687109-905f-4436-91f8-e602f514c96d", null, null, rangingOptions);

//example of ranging across all UUIDs
this.region = new BeaconRegion("HelloID", null, null, null, rangingOptions);

}

start() {
this.message = "start";

if (!this.nativescriptIbeacon.isAuthorised()) {
console.log("NOT Authorised");
this.nativescriptIbeacon.requestAuthorization()
Expand Down
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
},
"dependencies": {
"nativescript-ibeacon": "file:///Users/denny/workspaceNW/nativescript-ibeacon",
"nativescript-ibeacon": "file:///home/ebjerkelund/workspace/nativescript-ibeacon-fork",
"nativescript-theme-core": "^1.0.2",
"nativescript-unit-test-runner": "^0.3.4",
"tns-core-modules": "~2.5.2"
Expand Down
8 changes: 5 additions & 3 deletions nativescript-ibeacon.android.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Beacon, BeaconCallback, BeaconRegion, Common } from './nativescript-ibeacon.common';
import { Beacon, BeaconCallback, BeaconLocationOptions, BeaconRegion, Common } from './nativescript-ibeacon.common';
export declare class LocationService extends java.lang.Object {
private beaconManager;
delegate: BeaconCallback;
Expand All @@ -8,7 +8,9 @@ export declare class LocationService extends java.lang.Object {
private monitorNotifierAdded;
private pendingBeaconRegion;
private pendingBeaconMonitor;
constructor(context: android.content.Context);
private options;
private beaconParserTypes;
constructor(context: android.content.Context, options: BeaconLocationOptions);
bind(): void;
unbind(): void;
private getBeaconManager();
Expand All @@ -26,7 +28,7 @@ export declare class LocationService extends java.lang.Object {
}
export declare class NativescriptIbeacon extends Common {
private locationService;
constructor(beaconCallback: BeaconCallback);
constructor(beaconCallback: BeaconCallback, options?: BeaconLocationOptions);
requestAuthorization(): Promise<any>;
isAuthorised(): boolean;
private getPermission();
Expand Down
66 changes: 56 additions & 10 deletions nativescript-ibeacon.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Beacon, BeaconCallback, BeaconLocationOptionsAndroidAuthType, BeaconRegion,
Common
Beacon, BeaconCallback, BeaconLocationOptions, BeaconLocationOptionsAndroidAuthType, BeaconRegion,
Common, BeaconParserTypes
} from './nativescript-ibeacon.common';
import * as utils from "utils/utils";
import {setActivityCallbacks, AndroidActivityCallbacks} from "ui/frame";
Expand All @@ -24,11 +24,14 @@ export class LocationService extends java.lang.Object {
private pendingBeaconRegion: BeaconRegion = null;
private pendingBeaconMonitor: BeaconRegion = null;

constructor(context: android.content.Context) {
private options: BeaconLocationOptions = null;
private beaconParserTypes: BeaconParserTypes = new BeaconParserTypes();

constructor(context: android.content.Context, options: BeaconLocationOptions) {
super();
console.log("constructor");
this.context = context;

this.options = options;
return global.__native(this);
}

Expand All @@ -44,7 +47,29 @@ export class LocationService extends java.lang.Object {
private getBeaconManager() {
if (this.beaconManager == null) {
this.beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this.context/*utils.ad.getApplicationContext()*/);
this.beaconManager.getBeaconParsers().add(new org.altbeacon.beacon.BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); //iBeacon Layout
this.beaconManager.getBeaconParsers().clear();
// no parsers defined? Add all default types
if (this.options.parserTypes == null
|| this.options.parserTypes == null
|| this.options.parserTypes.length < 1) {
this.beaconParserTypes.getTypes().forEach(type => {
this.beaconManager.getBeaconParsers().add(
new org.altbeacon.beacon.BeaconParser().
setBeaconLayout(type)
);
});
console.log(this.beaconParserTypes.getTypes().length + " default parsers added");
}
// else add all passed parser types
else {
this.options.parserTypes.forEach(typeId => {
this.beaconManager.getBeaconParsers().add(
new org.altbeacon.beacon.BeaconParser().
setBeaconLayout(this.beaconParserTypes.getType(typeId))
);
});
console.log(this.options.parserTypes.length + " parsers added");
}
console.log("beaconManager created");
}
return this.beaconManager;
Expand All @@ -54,6 +79,16 @@ export class LocationService extends java.lang.Object {
if (this.beaconManagerReady) {
console.log("startRanging");
try {
if (beaconRegion.rangingOptions.foregroundScanInterval != null
&& beaconRegion.rangingOptions.foregroundScanInterval > 0) {
this.beaconManager.setForegroundBetweenScanPeriod(beaconRegion.rangingOptions.foregroundScanInterval);
console.log("foregroundScanInterval: " + beaconRegion.rangingOptions.foregroundScanInterval);
}
if (beaconRegion.rangingOptions.backgroundScanInterval != null
&& beaconRegion.rangingOptions.backgroundScanInterval > 0) {
this.beaconManager.setBackgroundBetweenScanPeriod(beaconRegion.rangingOptions.backgroundScanInterval);
console.log("backgroundScanInterval: " + beaconRegion.rangingOptions.backgroundScanInterval);
}
console.log("startRangingBeaconsInRegion");
this.getBeaconManager().startRangingBeaconsInRegion(this.getRegionFromBeaconRegion(beaconRegion)); //
//console.log("startRangingBeaconsInRegion2");
Expand Down Expand Up @@ -167,6 +202,7 @@ export class LocationService extends java.lang.Object {
}

getRegionFromBeaconRegion(beaconRegion: BeaconRegion): any {

let minor = null;
if (beaconRegion.minor) {
minor = org.altbeacon.beacon.Identifier.fromInt(beaconRegion.minor);
Expand All @@ -177,10 +213,20 @@ export class LocationService extends java.lang.Object {
major = org.altbeacon.beacon.Identifier.fromInt(beaconRegion.major);
}

return new org.altbeacon.beacon.Region(beaconRegion.identifier, org.altbeacon.beacon.Identifier.fromUuid(java.util.UUID.fromString(beaconRegion.proximityUUID)), major, minor);
if (beaconRegion.proximityUUID) {
return new org.altbeacon.beacon.Region(beaconRegion.identifier, org.altbeacon.beacon.Identifier.fromUuid(java.util.UUID.fromString(beaconRegion.proximityUUID)), major, minor);
}
else {
return new org.altbeacon.beacon.Region(beaconRegion.identifier, null, major, minor);
}

}

getBeaconRegionFromRegion(region: any /*org.altbeacon.beacon.Region*/): BeaconRegion {
let uuid = null;
if (region.getId1()) {
uuid = region.getId1().toString();
}
let major = null;
if (region.getId2()) {
major = Number(region.getId2().toString())
Expand All @@ -189,8 +235,8 @@ export class LocationService extends java.lang.Object {
if (region.getId3()) {
minor = Number(region.getId3().toString())
}
let breagion = new BeaconRegion(region.getUniqueId(), region.getId1().toString(), major, minor);
return breagion;
let bregion = new BeaconRegion(region.getUniqueId(), uuid, major, minor);
return bregion;
}

getBeaconFromNativeBeacon(nativeBeacon: any): Beacon {
Expand Down Expand Up @@ -219,9 +265,9 @@ export class LocationService extends java.lang.Object {
export class NativescriptIbeacon extends Common {
private locationService: LocationService = null;

constructor(beaconCallback: BeaconCallback) {
constructor(beaconCallback: BeaconCallback, options?: BeaconLocationOptions) {
super(beaconCallback);
this.locationService = new LocationService(utils.ad.getApplicationContext());
this.locationService = new LocationService(utils.ad.getApplicationContext(), options);
this.locationService.delegate = beaconCallback;
}

Expand Down
28 changes: 26 additions & 2 deletions nativescript-ibeacon.common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ export declare enum BeaconLocationOptionsAndroidAuthType {
Coarse = 0,
Fine = 1,
}
export declare enum BeaconParserType {
AltBeacon = 0,
EddystoneTLM = 1,
EddystoneUID = 2,
EddystoneURL = 3,
IBeacon = 4,
}
export interface BeaconLocationOptions {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType;
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType;
androidAuthorisationDescription: string;
parserTypes: number[];
}
export interface RangingOptions {
foregroundScanInterval: number;
backgroundScanInterval: number;
}
export declare class Common extends Observable {
protected options: BeaconLocationOptions;
Expand All @@ -33,10 +45,11 @@ export declare class Common extends Observable {
}
export declare class BeaconRegion {
identifier: string;
proximityUUID: string;
proximityUUID?: string;
major?: number;
minor?: number;
constructor(identifier: string, proximityUUID: string, major?: number, minor?: number);
rangingOptions?: RangingOptions;
constructor(identifier: string, proximityUUID?: string, major?: number, minor?: number, rangingOptions?: RangingOptions);
}
export declare class Beacon {
proximityUUID: string;
Expand All @@ -47,3 +60,14 @@ export declare class Beacon {
txPower_accuracy: number;
constructor(proximityUUID: string, major: number, minor: number);
}
export declare class BeaconParserTypes {
private types;
private altBeacon;
private eddystoneTLM;
private eddystoneUID;
private eddystoneURL;
private iBeacon;
constructor();
getTypes(): string[];
getType(id: number): string;
}
45 changes: 39 additions & 6 deletions nativescript-ibeacon.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@ export enum BeaconLocationOptionsIOSAuthType {
export enum BeaconLocationOptionsAndroidAuthType {
Coarse, Fine
}
export enum BeaconParserType {
AltBeacon, EddystoneTLM, EddystoneUID, EddystoneURL, IBeacon
}

export interface BeaconLocationOptions {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType;
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType;
androidAuthorisationDescription: string;
androidAuthorisationDescription: string
parserTypes: number[];
}

export interface RangingOptions {
foregroundScanInterval: number;
backgroundScanInterval: number;
}

export class Common extends Observable {

protected options: BeaconLocationOptions = {
iOSAuthorisationType: BeaconLocationOptionsIOSAuthType.WhenInUse,
androidAuthorisationType: BeaconLocationOptionsAndroidAuthType.Coarse,
androidAuthorisationDescription: "Location permission needed"
androidAuthorisationDescription: "Location permission needed",
parserTypes: []
};

constructor(beaconCallback: BeaconCallback, options?: BeaconLocationOptions) {
Expand Down Expand Up @@ -73,15 +83,17 @@ export class Common extends Observable {

export class BeaconRegion {
public identifier: string;
public proximityUUID: string;
public proximityUUID?: string;
public major?: number;
public minor?: number;
public rangingOptions?: RangingOptions;

constructor(identifier: string, proximityUUID: string, major?: number, minor?: number) {
constructor(identifier: string, proximityUUID?: string, major?: number, minor?: number, rangingOptions?: RangingOptions) {
this.identifier = identifier;
this.proximityUUID = proximityUUID;
if (proximityUUID) this.proximityUUID = proximityUUID;
if (major) this.major = major;
if (minor) this.minor = minor;
if (rangingOptions) this.rangingOptions = rangingOptions;
}
}

Expand All @@ -101,4 +113,25 @@ export class Beacon {
}
}


export class BeaconParserTypes {
private types: string[];
private altBeacon: string = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";
private eddystoneTLM: string = "x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15";
private eddystoneUID: string = "s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19";
private eddystoneURL: string = "s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v";
private iBeacon: string = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";
constructor() {
this.types = [];
this.types[BeaconParserType.AltBeacon] = this.altBeacon;
this.types[BeaconParserType.EddystoneTLM] = this.eddystoneTLM;
this.types[BeaconParserType.EddystoneUID] = this.eddystoneUID;
this.types[BeaconParserType.EddystoneURL] = this.eddystoneURL;
this.types[BeaconParserType.IBeacon] = this.iBeacon;
}
public getTypes(): string[] {
return this.types;
}
public getType(id: number): string {
return this.types[id];
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@
"tns-platform-declarations": "^3.0.1",
"typescript": "~2.2.1"
}
}
}