Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit , NgZone} from '@angular/core';
- import { Platform } from '@ionic/angular';
- import 'rxjs/add/operator/toPromise';
- import {ActivatedRoute} from '@angular/router';
- import { map } from 'rxjs/operators';
- import { Observable } from 'rxjs';
- import { Storage } from '@ionic/storage';
- import * as Email from '../log-in/log-in.component';
- // ble
- import { BluetoothLE} from '@ionic-native/bluetooth-le/ngx';
- import { BLE } from '@ionic-native/ble/ngx';
- import { Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
- //firebase
- import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore';
- import { AngularFireAuth } from '@angular/fire/auth';
- @Component({
- selector: 'app-add-device',
- templateUrl: './add-device.component.html',
- styleUrls: ['./add-device.component.scss'],
- })
- export class AddDeviceComponent implements OnInit {
- routeParams;
- SCAN_RESULT_COUNT = 20;
- scanResult;
- id;
- found ="not found"
- devices:any[] = [];
- constructor(
- public bluetoothle: BluetoothLE,
- public plt: Platform,
- private route: ActivatedRoute,
- private storage: Storage,
- private ble: BLE,
- private ngZone: NgZone,
- private cd: ChangeDetectorRef,
- public afs: AngularFirestore,
- ) { }
- Scan(){
- this.devices = [];
- this.ble.scan([],15).subscribe(
- device => this.onDeviceDiscovered(device)
- );
- this.cd.detectChanges();
- }
- onDeviceDiscovered(device){
- console.log('Discovered' + JSON.stringify(device,null,2));
- this.ngZone.run(()=>{
- this.devices.push(device)
- //console.log(device)
- this.cd.detectChanges();
- })
- }
- connect(){
- var count = 0;
- for (let i = 0; i < this.devices.length; i++)
- {
- if (this.devices[count].name == "Argon-956S32" || this.devices[count].name == "New_Name")
- {
- this.found = "FOUND!!!"
- this.ble.connect(this.devices[count].id).subscribe(peripheralData => {
- console.log(peripheralData);
- },
- peripheralData => {
- console.log('disconnected');
- });;
- this.id = this.devices[count].id;
- this.afs.collection("usersByEmail").doc(Email.__email).collection("devices").doc(this.id).set({ID:this.id});
- }
- count = count+1;
- }
- }
- write(){
- if (this.ble.isConnected)
- {
- console.log("WE ARE CURRENTLY CONNECTED");
- var string = "HELLO"
- var array = new Uint8Array(string.length);
- for (var x = 0, l = string.length; x < l; x++) {
- array[x] = string.charCodeAt(x);
- }
- this.ble.writeWithoutResponse(this.id, "6E400001-B5A3-F393-E0A9-E50E24DCCA9E".toLowerCase(),
- "6E400002-B5A3-F393-E0A9-E50E24DCCA9E".toLowerCase(), array.buffer);
- }
- else{
- console.log("WE ARE NOT CONNECTED HELP");
- }
- }
- ngOnInit(){}
- }
RAW Paste Data