Nativescript-bluetooth does not find out nearby devices

plugins

#1

Hi,
I’m working on a Nativescript application developed with Typescript and Angular 2 and I’m using this plugin https://github.com/EddyVerbruggen/nativescript-bluetooth to interact with bluetooth. The problem is that: sometimes the service finds out devices, sometimes not, while the system application for bluetooth always find them all (so it’s not a phone problem). I’ve tried everything but it seems that the code is correct. For testing I’m using Huawei P9 Lite with Android 7. That’s my service code:

import { Injectable } from '@angular/core';
import { LoadingIndicator } from "nativescript-loading-indicator";
import { DialogService } from './dialog.service';
import * as bluetooth from 'nativescript-bluetooth';
import { ObservableArray } from "tns-core-modules/data/observable-array/observable-array";
import { Peripheral } from 'nativescript-bluetooth';
import { AsyncPipe } from '@angular/common';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/distinctUntilChanged';

@Injectable()
export class BluetoothService {

    private blthDevices: ObservableArray<Peripheral> = new ObservableArray<Peripheral>();
    private loader = new LoadingIndicator();

    constructor(private dialogService: DialogService) {
    }

    public startAndScan(loadingMode: boolean) {
        loadingMode ? this.loader.show({
            message: 'Scanning, please wait...'
        }) : null;
        // abilitazione android
        return bluetooth.enable().then(function (enabled) {
            if (enabled) {
                // azzero array
                this.blthDevices = [];
                // parte la scansione (sempre promise)
                return bluetooth.startScanning({
                    // I've tried also with this: serviceUUIDs: [],
                    seconds: 20,
                    onDiscovered: (host: Peripheral) => {
                        loadingMode ? alert(host.UUID) : null;
                        // aggiunta all'array
                        this.blthDevices.push(host);
                    }
                }).then(function () {
                    // schermata di loading solo se è attiva la modalità di loading
                    loadingMode ? this.loader.hide() : null;
                }.bind(this), function (err) {
                    loadingMode ? this.loader.hide() : null;
                    throw new Error("Bluetooth error");
                }.bind(this));
            }
            else {
                loadingMode ? this.loader.hide() : null;
                throw new Error("Bluetooth disabled");
            }
        }.bind(this));

    }

    public isEnabled(): Observable<boolean> {   
        return Observable.fromPromise(bluetooth.isBluetoothEnabled());
    }

    public getDevices(): ObservableArray<Peripheral> {
        return this.blthDevices;
    }
}

Thanks

(Sorry for my english…)


#2

I recently did just a little testing with an ngBeacon and the bluetooth plugin. I feel like it was finding my device. I guess you’d have to do some experiments - in what condition does the phone not find the device? What kind of bluetooth device? Have you tested the demo code on the app to ensure that you get consistent results (or not?)


#3

Hi,
there is no particolar condition in which the smartphone doesn’t find out the devices. I’m trying mainly with an HC-06 bluetooth device that is found once on 10, I don’t know maybe it’s a HC-06 problem. I will try with the demo app so maybe I understand where the problem can be. In the meantime,the code is fine?
Thanks a lot!


#4

I just tried … The demo application does not find the HC06 device but it finds a notebook. Maybe it’s a problem of the HC06 device (but if so why is it found by the system application?) …


#5

NativeScript Bluetooth LE plugin

Maybe it works only with Bluetooth LE (ver. 4) devices?


#6

hi, I’d encourage you in this situation to post this question as an issue for @Eddy to take a look at, as it’s his plugin. You might want to append to this issue: https://github.com/EddyVerbruggen/nativescript-bluetooth/issues/18