Skip to content

Commit c95b22d

Browse files
committed
chore: demo app update
1 parent 9a5c612 commit c95b22d

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

demo/app/App_Resources/Android/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
1414
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1515
<uses-permission android:name="android.permission.INTERNET"/>
16+
<uses-permission android:name="android.permission.BLUETOOTH"/>
17+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
18+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
19+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
20+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
1621

1722
<application
1823
android:name="com.tns.NativeScriptApplication"

demo/app/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { BleTraceCategory } from '@nativescript-community/ble';
2+
import { PermsTraceCategory } from '@nativescript-community/perms';
23
import { Application, Trace } from '@nativescript/core';
34
Trace.addCategories(BleTraceCategory);
5+
Trace.addCategories(PermsTraceCategory);
46
Trace.enable();
57
Application.run({ moduleName: 'app-root' });

demo/app/main-page.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<TabViewItem.view>
4343
<ScrollView>
4444
<StackLayout class="tab-content">
45-
<Image margin="10" src="~/res/telerik-logo.png" />
45+
<!-- <Image margin="10" src="~/res/telerik-logo.png" /> -->
4646
<Label text="Bluetooth plugin demo" class="title"/>
4747
<Label text="The Bluetooth plugin allows your app to scan for Bluetooth LE devices. Once found you can connect to a peripheral and read values. You can also write values and get notified when a value changes." textWrap="true"/>
4848
</StackLayout>

demo/app/main-view-model.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class DemoAppModel extends Observable {
2020
// using an event listener instead of the 'onDiscovered' callback of 'startScanning'
2121
this._bluetooth.on(Bluetooth.device_discovered_event, (eventData: any) => {
2222
const perip = eventData.data as Peripheral;
23+
console.log('Peripheral found:', JSON.stringify(eventData.data));
2324
let index = -1;
2425
this.peripherals.some((p, i) => {
2526
if (p.UUID === perip.UUID) {
@@ -28,7 +29,6 @@ export class DemoAppModel extends Observable {
2829
}
2930
return false;
3031
});
31-
console.log('Peripheral found:', JSON.stringify(eventData.data), index);
3232
if (index === -1) {
3333
this.peripherals.push(perip);
3434
} else {
@@ -94,6 +94,7 @@ export class DemoAppModel extends Observable {
9494
// this one 'manually' checks for permissions
9595
public doScanForHeartrateMontitor() {
9696
this._bluetooth.hasLocationPermission().then((granted) => {
97+
console.log('hasLocationPermission', granted);
9798
if (!granted) {
9899
this._bluetooth.requestLocationPermission().then(
99100
// doing it like this for demo / testing purposes.. better usage is demonstrated in 'doStartScanning' below
@@ -189,35 +190,36 @@ export class DemoAppModel extends Observable {
189190
});
190191
}
191192

192-
193193
// this one uses automatic permission handling
194194
public async doStartScanning() {
195195
try {
196-
// this._bluetooth.hasLocationPermission().then((granted) => {
197-
// if (!granted) {
198196
const r = await request({ bluetoothConnect: {}, bluetoothScan: {} });
199-
// this._bluetooth.requestLocationPermission().then(
200-
// // doing it like this for demo / testing purposes.. better usage is demonstrated in 'doStartScanning' below
201-
// (granted2) => {
202-
// dialogs.alert({
203-
// title: 'Granted?',
204-
// message: granted2 ? 'Yep - now invoke that button again' : 'Nope',
205-
// okButtonText: 'OK!'
206-
// });
207-
// }
208-
// );
209-
// } else {
210-
this.isLoading = true;
211-
// reset the array
212-
this.peripherals.length = 0;
213-
await this._bluetooth.startScanning({
214-
seconds: 50, // passing in seconds makes the plugin stop scanning after <seconds> seconds
215-
onDiscovered: peripheral => {
216-
console.log("peripheral discovered. Not adding it here because we're using a listener.");
217-
},
197+
console.log('r', r);
198+
this._bluetooth.hasLocationPermission().then(async (granted) => {
199+
console.log('granted', granted);
200+
if (!granted) {
201+
this._bluetooth.requestLocationPermission().then(
202+
// doing it like this for demo / testing purposes.. better usage is demonstrated in 'doStartScanning' below
203+
(granted2) => {
204+
dialogs.alert({
205+
title: 'Granted?',
206+
message: granted2 ? 'Yep - now invoke that button again' : 'Nope',
207+
okButtonText: 'OK!'
208+
});
209+
}
210+
);
211+
} else {
212+
this.isLoading = true;
213+
// reset the array
214+
this.peripherals.length = 0;
215+
await this._bluetooth.startScanning({
216+
seconds: 50, // passing in seconds makes the plugin stop scanning after <seconds> seconds
217+
onDiscovered: (peripheral) => {
218+
console.log("peripheral discovered. Not adding it here because we're using a listener.");
219+
}
220+
});
221+
}
218222
});
219-
// }
220-
// });
221223
} catch (error) {
222224
dialogs.alert({
223225
title: 'Whoops!',

0 commit comments

Comments
 (0)