美文网首页
关于基于cordova-plugin-ble-central 蓝

关于基于cordova-plugin-ble-central 蓝

作者: 薪火设计 | 来源:发表于2018-07-07 16:31 被阅读96次
最近开发的一款移动app应用基于ionic3+ng5+cordova移动框架,需要处理蓝牙电子秤的数据接收处理,实现过程一波三折,不过最终还是能够顺利实现,蓝牙的读写,及广播的接收,接收的效果图如下。
TIM图片20180707163304.jpg
实现过程如下
步骤一 安装Cordova蓝牙插件,命令行如下
ionic cordova plugin add cordova-plugin-ble-central
npm install --save @ionic-native/ble
步骤二,app.module.ts文件中引入蓝牙插件
import { BLE } from '@ionic-native/ble';
 providers: [ble]
步骤三,封装蓝牙基础插件
import { Injectable, Version } from '@angular/core';
import { BLE } from '@ionic-native/ble';
import { NativeService } from "./NativeService";
import { TipsService } from './TipService';
/**
 * 处理蓝牙的业务方法
 * @weijb
 */
@Injectable()
export class BleService {
    static bleDievices = [];
    constructor(private ble: BLE, private nativeService: NativeService, private tips: TipsService) {

    }
    /**
     * 蓝牙初始化
     * @weijb
     */
    init() {
        return new Promise((resolve, reject) => {
            if(this.nativeService.isMobile()){
                this.ble.isEnabled().then(res => {
                    resolve(true);
                }, err => {
                    //开启蓝牙
                    this.enable().then(res => {
                        resolve(true);
                    })
                })
            }else{
                reject(false);
            }
        })
    }
    /**
     * 打开蓝牙
     */
    enable() {
        return new Promise((resolve, reject) => {
            if (this.nativeService.isAndroid()) {
                this.ble.enable().then(res => {
                    resolve(true)
                }, err => {
                    this.tips.show('请手动开启蓝牙设备', 1000);
                    resolve(false);
                })
            } else {
                this.tips.show('请手动开启蓝牙设备', 1000);
                resolve(true);
            }
        })
    }
    //扫描设备
    scan() {
        //6s后停止扫描
        setTimeout(res => {
            this.ble.stopScan().then(res => { }, err => {
                this.tips.show('停止蓝牙设备扫描失败')
            })
        }, 18000);
        return this.ble.startScan([]);
    }

    //蓝牙设备连接
    connect(divice:string) {
        //alert(divice);
        return new Promise((resolve,reject)=>{
            this.ble.connect(divice).subscribe((data:any) => {
                // console.log('日志',data);
                // console.log('日志',data.characteristics[7]);
              resolve(data.characteristics[7]);
               if(this.nativeService.isIos()){
                    resolve(data.characteristics[1]);
               }else{
                  resolve(data.characteristics[7]);
               }
                // resolve(data.characteristics.find(res => {
                //     return res.hasOwnProperty('properties') && res.properties.find(res=>{
                //         return res =='Read';
                //     })!=null;
                // })) ;
            },
                error => {
                    this.tips.show(error.errorMessage,1200);
                    console.log('链接失败error',error);
                    reject({});
                }
            );
        })
    }
    //读取蓝牙设备信息
    read(divice: string, serviceUUID: string, characteristicUUID: string){
      //  {"id":"65:F9:FD:CA:76:A8","advertising":{},"rssi":-70,"services":["1800","1801","d0611e78-bbb4-4591-a5f8-487910ae4366","9fa480e0-4967-4542-9390-d343dc5d04ae"],"characteristics":[{"service":"1800","characteristic":"2a00","properties":["Read"]},{"service":"1800","characteristic":"2a01","properties":["Read"]},{"service":"1801","characteristic":"2a05","properties":["Indicate"],"descriptors":[{"uuid":"2902"}]},{"service":"d0611e78-bbb4-4591-a5f8-487910ae4366","characteristic":"8667556c-9a37-4c91-84ed-54ee27d90049","properties":["Write","Notify","ExtendedProperties"],"descriptors":[{"uuid":"2900"},{"uuid":"2902"}]},{"service":"9fa480e0-4967-4542-9390-d343dc5d04ae","characteristic":"af0badb1-5b99-43cd-917a-a77bc549e3cc","properties":["Write","Notify","ExtendedProperties"],"descriptors":[{"uuid":"2900"},{"uuid":"2902"}]}]}
     // this.ble.read(divice)
    // ble.read(device.id, $scope.serviceUUID, $scope.counterCharacteristic,
    //var data = new Uint8Array(buffer);//Uint8Array对象:8 位无符号整数值的类型化数组。内容将初始化为 0。如果无法分配请求数目的字节,则将引发异常。
    return  this.ble.read(divice,serviceUUID,characteristicUUID);
    }
    //屏幕
    startNotification(divice: string, serviceUUID: string, characteristicUUID: string){
        return this.ble.startNotification(divice,serviceUUID,characteristicUUID);
    }
    stopNotification(divice: string, serviceUUID: string, characteristicUUID: string){
        return   this.ble.stopNotification(divice,serviceUUID,characteristicUUID);
    }
    notify(divice: string, serviceUUID: string, characteristicUUID: string){

    }

}
步骤四,直接调用蓝牙工具类方法实现...

相关文章

网友评论

      本文标题:关于基于cordova-plugin-ble-central 蓝

      本文链接:https://www.haomeiwen.com/subject/bqfguftx.html