最近開(kāi)發(fā)的一款移動(dòng)app應(yīng)用基于ionic3+ng5+cordova移動(dòng)框架,需要處理藍(lán)牙電子秤的數(shù)據(jù)接收處理立砸,實(shí)現(xiàn)過(guò)程一波三折救鲤,不過(guò)最終還是能夠順利實(shí)現(xiàn)侵贵,藍(lán)牙的讀寫(xiě),及廣播的接收帕识,接收的效果圖如下。
實(shí)現(xiàn)過(guò)程如下
步驟一 安裝Cordova藍(lán)牙插件,命令行如下
ionic cordova plugin add cordova-plugin-ble-central
npm install --save @ionic-native/ble
步驟二遂铡,app.module.ts文件中引入藍(lán)牙插件
import { BLE } from '@ionic-native/ble';
providers: [ble]
步驟三肮疗,封裝藍(lán)牙基礎(chǔ)插件
import { Injectable, Version } from '@angular/core';
import { BLE } from '@ionic-native/ble';
import { NativeService } from "./NativeService";
import { TipsService } from './TipService';
/**
* 處理藍(lán)牙的業(yè)務(wù)方法
* @weijb
*/
@Injectable()
export class BleService {
static bleDievices = [];
constructor(private ble: BLE, private nativeService: NativeService, private tips: TipsService) {
}
/**
* 藍(lán)牙初始化
* @weijb
*/
init() {
return new Promise((resolve, reject) => {
if(this.nativeService.isMobile()){
this.ble.isEnabled().then(res => {
resolve(true);
}, err => {
//開(kāi)啟藍(lán)牙
this.enable().then(res => {
resolve(true);
})
})
}else{
reject(false);
}
})
}
/**
* 打開(kāi)藍(lán)牙
*/
enable() {
return new Promise((resolve, reject) => {
if (this.nativeService.isAndroid()) {
this.ble.enable().then(res => {
resolve(true)
}, err => {
this.tips.show('請(qǐng)手動(dòng)開(kāi)啟藍(lán)牙設(shè)備', 1000);
resolve(false);
})
} else {
this.tips.show('請(qǐng)手動(dòng)開(kāi)啟藍(lán)牙設(shè)備', 1000);
resolve(true);
}
})
}
//掃描設(shè)備
scan() {
//6s后停止掃描
setTimeout(res => {
this.ble.stopScan().then(res => { }, err => {
this.tips.show('停止藍(lán)牙設(shè)備掃描失敗')
})
}, 18000);
return this.ble.startScan([]);
}
//藍(lán)牙設(shè)備連接
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({});
}
);
})
}
//讀取藍(lán)牙設(shè)備信息
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對(duì)象:8 位無(wú)符號(hào)整數(shù)值的類(lèi)型化數(shù)組。內(nèi)容將初始化為 0扒接。如果無(wú)法分配請(qǐng)求數(shù)目的字節(jié)伪货,則將引發(fā)異常们衙。
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){
}
}
步驟四,直接調(diào)用藍(lán)牙工具類(lèi)方法實(shí)現(xiàn)...