//pages/ble_ui/ble_ui.js
var util=require('../../utils/util.js')
const app = getApp()
var power_flag = ""
var joke = ""
var bt_change_f=0
var BuleAVD_dataBuffer=''
var jsondeviceId=""
var BuffinputValue=""
var BuffinputValue1=""
var saveinputuuid_f=0;
const innerAudioContext = wx.createInnerAudioContext();
Page({
data: {
server: {},
ble_adv_sta: false,
timer_id_adv: 0,
Rx_datadsplay:"0000000000000000",
Tx_datadsplay:"0000000000000000",
focus: false,
inputValue: '',
check:false,
option: [
/* {
id: 'car',
icon: 'bluetooth',
name: 'Bluetooth Car',
open: false,
},*/
{
id: 'index',
icon: 'bluetooth',
name: 'Nebulizer',
open: false,
},
{
id: 'ble_radio',
icon: 'bluetooth',
name: 'BLE_radio',
open: false,
}
],
},
///++++++++++++++++++++++
switch1Change: function (e) {
// console.log('switch1 发生 change 事件,携带值为', e.detail.value);
if(e.detail.value) {
// this.openBluetoothAdapter();
var that = this;
this.startPerphiAdv();
console.log('打开广播');
wx.showToast({
title: '打开广播',
icon: 'none',
duration: 2000
})
} else {
this.stopAdvertising();
console.log('关闭广播');
wx.showToast({
title: '关闭广播',
icon: 'none',
duration: 2000
})
}
},
///=====
switch2Change: function (e) {
// console.log('switch1 发生 change 事件,携带值为', e.detail.value);
if (e.detail.value) {
// this.openBluetoothAdapter();
var that = this;
wx.openBluetoothAdapter({
mode:'peripheral',
success: function (res) {
console.log('初始化蓝牙适配器成功')
//页面日志显示
// that.setData({
// info: '初始化蓝牙适配器成功'
// })
power_flag = true;
wx.showToast({
title: '初始化蓝牙适配器成功',
icon: 'none',
duration: 2000
})
},
fail: function (res) {
console.log('请打开手机蓝牙和定位功能')
// that.setData({
// info: '请打开蓝牙和定位功能'
// })
wx.showToast({
title: '蓝牙开启失败,请打开手机蓝牙功能',
icon: 'none',
duration: 2000
})
}
})
}else {
this.closeConnect();
wx.showToast({
title: '蓝牙已断开',
icon: 'none',
duration: 2000
})
power_flag = false;
}
},
//__________________________
bindKeyInput: function(e) {
// this.setData({
// inputValue: e.detail.value
// })
BuffinputValue= e.detail.value;
console.log("输入的uuid是:",BuffinputValue)
},
bindKeyInput_confirm:function(e){
saveinputuuid_f=1;
BuffinputValue1= BuffinputValue;
console.log("确认的uuid是:",BuffinputValue)
},
radiocon:function(e){
this.setData({
check: !this.data.check
})
console.log("用户打勾的是 ",this.data.check)
if(this.data.check==true)
{
saveinputuuid_f=1;
BuffinputValue1= BuffinputValue;
}else{
saveinputuuid_f=0;
}
},
//__________________________
//++++++++++++++++++++++
kindToggle: function (e) {
var id = e.currentTarget.id
wx.navigateTo({
url: '../' + id + '/' + id
})
/*
var id = e.currentTarget.id, list = this.data.option;
for (var i = 0, len = list.length; i < len; ++i) {
if (list[i].id == id) {
list[i].open = !list[i].open
} else {
list[i].open = false
}
}
this.setData({
option: list
});
*/
},
//=================
query:function(){
if(!!wx.getStorageSync('userInfo')){
let _this=this;
setTimeout(()=> {
_this.query();
},500);
return;
}
},
//-------=======
Bt01: function (e){
var that = this;
bt_change_f=1;
that.Bt_gg();
},
Bt02: function (e){
var that = this;
bt_change_f=2;
that.Bt_gg();
},
Bt03: function (e){
var that = this;
bt_change_f=3;
that.Bt_gg();
},
Bt04: function (e){
var that = this;
bt_change_f=4;
that.Bt_gg();
},
Bt05: function (e){
var that = this;
bt_change_f=5;
that.Bt_gg();
},
Bt06: function (e){
var that = this;
bt_change_f=6;
that.Bt_gg();
},
Bt_gg: function (){
var that = this;
// power_flag = true;
if(power_flag==true)
{
that.starAdvertise_loop();
//-----------------------------
wx.playVoice({
filePath: '/utils/di.mp3',
complete: function (res) {
console.log('playVoice res')
console.log(res)
}
})
//---------------------------------
}else{
wx.showToast({
title: '请打开蓝牙开关',
icon: 'none',
duration: 2000
})
}
},
//=====
addClickAudio:function(){
wx.stopVoice();
innerAudioContext.autoplay = true // 是否自动开始播放,默认为 false
innerAudioContext.loop = false // 是否循环播放,默认为 false
wx.setInnerAudioOption({ // ios在静音状态下能够正常播放音效
obeyMuteSwitch: false, // 是否遵循系统静音开关,默认为 true。当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音。
success: function (e) {},
fail: function (e) {}
})
innerAudioContext.src = '/utils/di.mp3'; // this.globalData.Audiopath; // 音频资源的地址
//innerAudioContext.onPlay()
console.log("播放了di")
},
keyplay: function () {
// innerAudioContext .onStop();
innerAudioContext .play();
console.log("播放了di")
},
//点击 停止
keystop: function () {
innerAudioContext .pause();
},
//================================================send
starAdvertise_loop: function (){
if(power_flag==true)
{
this. stopAdvertising();
if (JSON.stringify(this.data.server) == "{}") {
this.createBLEPeripheralServer();
} else {
console.log("准备广播,已有服务无需再建,serverId:", this.data.server.serverId)
console.log("开始广播")
this.startAdvertising_0();
}
}else{
wx.showToast({
title: '蓝牙初始化失败,请打开BuleTooth',
icon: 'none',
duration: 2000
})
}
/*
setInterval (function() {
//循环执行代码
//------------------------------------------
this.data.server.startAdvertising();
//------------------------------------------
console.log('1秒');
}, 1000) //循环时间 这里是1秒
*/
},
///==============
///=====================================
res_search:function() {
var that = this;
that.startBluetoothDevicesDiscovery();
},
//==================================
//-----
startAdvertising_0() {
var that=this;
let m_id = "013A";
//let m_spdata1 = new Uint8Array(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]);
let m_uuid = '00001111-5555-6666-7777-FFFFFFFF8888';
let m_spdata1 = 'buu1';
let m_spdata2=""
switch (bt_change_f)
{
case 1: m_uuid='00001111-5555-6666-7777-FFFFFFFF8888'; break;
case 2: m_uuid='00002222-5555-6666-7777-FFFFFFFF8888'; break;
case 3: m_uuid='00003333-5555-6666-7777-FFFFFFFF8888'; break;
case 4: m_uuid='00004444-5555-6666-7777-FFFFFFFF8888'; break;
case 5: m_uuid='00005555-5555-6666-7777-FFFFFFFF8888'; break;
case 6: m_uuid='00006666-5555-6666-7777-FFFFFFFF8888'; break;
}
if(saveinputuuid_f)
{
m_uuid=BuffinputValue1;
console.log('测试这里为发送Buffinputuuid',BuffinputValue1);
console.log('测试这里为发送m_uuid',m_uuid);
}
// m_uuid = '00001111-77-FFFFFFFF8888';
that.setData({
Tx_datadsplay: m_uuid, //更新
});
this.data.serv