Win4Win

BLE Ionic

Feb 24th, 2021
799
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import { Component, OnInit , NgZone} from '@angular/core';
  3.  
  4. import { Platform } from '@ionic/angular';
  5. import 'rxjs/add/operator/toPromise';
  6. import {ActivatedRoute} from '@angular/router';
  7. import { map } from 'rxjs/operators';
  8. import { Observable } from 'rxjs';
  9. import { Storage } from '@ionic/storage';
  10. import * as Email from '../log-in/log-in.component';
  11. // ble
  12. import { BluetoothLE} from '@ionic-native/bluetooth-le/ngx';
  13. import { BLE } from '@ionic-native/ble/ngx';
  14. import { Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
  15. //firebase
  16. import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from '@angular/fire/firestore';
  17. import { AngularFireAuth } from '@angular/fire/auth';
  18.  
  19. @Component({
  20.   selector: 'app-add-device',
  21.   templateUrl: './add-device.component.html',
  22.   styleUrls: ['./add-device.component.scss'],
  23. })
  24. export class AddDeviceComponent implements OnInit {
  25.  
  26.   routeParams;
  27.   SCAN_RESULT_COUNT = 20;
  28.   scanResult;
  29.   id;
  30.  
  31.   found ="not found"
  32.   devices:any[] = [];
  33.  
  34.   constructor(
  35.     public bluetoothle: BluetoothLE,
  36.     public plt: Platform,
  37.     private route: ActivatedRoute,
  38.     private storage: Storage,
  39.     private ble: BLE,
  40.     private ngZone: NgZone,
  41.     private cd: ChangeDetectorRef,
  42.     public afs: AngularFirestore,
  43.     ) { }
  44.  
  45.     Scan(){
  46.       this.devices = [];
  47.       this.ble.scan([],15).subscribe(
  48.         device => this.onDeviceDiscovered(device)
  49.       );
  50.       this.cd.detectChanges();
  51.     }
  52.     onDeviceDiscovered(device){
  53.       console.log('Discovered' + JSON.stringify(device,null,2));
  54.       this.ngZone.run(()=>{
  55.         this.devices.push(device)
  56.         //console.log(device)
  57.         this.cd.detectChanges();
  58.        
  59.       })
  60.     }
  61.     connect(){
  62.     var count = 0;
  63.     for (let i = 0; i < this.devices.length; i++)
  64.     {
  65.       if (this.devices[count].name == "Argon-956S32" || this.devices[count].name == "New_Name")
  66.       {
  67.         this.found = "FOUND!!!"
  68.         this.ble.connect(this.devices[count].id).subscribe(peripheralData => {
  69.  
  70.          
  71.           console.log(peripheralData);
  72.          
  73.         },
  74.         peripheralData => {
  75.           console.log('disconnected');
  76.         });;
  77.        
  78.         this.id = this.devices[count].id;
  79.  
  80.         this.afs.collection("usersByEmail").doc(Email.__email).collection("devices").doc(this.id).set({ID:this.id});
  81.  
  82.  
  83.        
  84.       }
  85.       count = count+1;
  86.     }
  87.   }
  88.  
  89.   write(){
  90.     if (this.ble.isConnected)
  91.     {
  92.       console.log("WE ARE CURRENTLY CONNECTED");
  93.     var string = "HELLO"
  94.  
  95.     var array = new Uint8Array(string.length);
  96.     for (var x = 0, l = string.length; x < l; x++) {
  97.       array[x] = string.charCodeAt(x);
  98.     }
  99.     this.ble.writeWithoutResponse(this.id, "6E400001-B5A3-F393-E0A9-E50E24DCCA9E".toLowerCase(),
  100.     "6E400002-B5A3-F393-E0A9-E50E24DCCA9E".toLowerCase(), array.buffer);
  101.   }
  102.   else{
  103.     console.log("WE ARE NOT CONNECTED HELP");
  104.   }
  105.      
  106.   }
  107.  
  108.   ngOnInit(){}
  109.  
  110. }
  111.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×