cordova蓝牙发数据和接收处理数据

发数据

function sendData(){
    var a = [0xAB,0xAC,0xAD];
    var sendData = new Uint8Array(a);

    ble.write(bleId,service,character,sendData.buffer,function(data){
        console.log("数据发成功");
        console.log(data);
    },function(data){
        console.log("数据发失败");
        console.log(data);
    })
}

接收处理数据

1.连接

 ble.connect(data.id,function(connectSuc){
        console.log("连接成功");
        bleId = data.id;
        bleName = data.name;
        console.log(connectSuc);
        receive();
        var timer = setTimeout(function(time){
            sendData();
            clearTimeout(timer);
            },1000);
       
        
    },function(connectFail){
        console.log("连接失败"+connectFail);
    })

2.接收处理数据

function receive(){
    console.log("开始接收数据");
    ble.startNotification(bleId,service,character,receiveData,function(data){
        console.log("读取数据失败");
        console.log(data);
    })
}

function receiveData(buffer){
    console.log("接收到数据");
    console.log(buffer);
    var data = new Uint8Array(buffer);
    console.log(data);
    console.log(data[0]);//这里打出来是十进制的数据
    if(data[0]==0xAB){
        console.log("就是AA");
    }
    var flag=0xAB;
    for(var i=0;i<data.length;i++){
        console.log(data)
        switch(data[i]){
            case 0xAB:
            console.log("进来2");
            flag = 0xAC;
            break;
            case 0xAC:
            console.log("进来3");
            flag = 0xAD;
            break;
            case 0xAD:
            console.log("进来4");
            if(data.length == 4){
                blueResponse = true;
                rightData();
            }
            break;
        }
    }

参考博客(包含插件下载)

ionic蓝牙插件(cordova-plugin-ble-central)

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页