// pages/bleble/ble.js
var TAG = 'ble';
//监护正在运行的状态: 0,停止或初始状态 1,正在搜索蓝牙,2,正在接收数据
var mJianHuStatus = 0;
//要显示的数据
var mStreee = new Array();
///////////////////////////////////////////////////////////////////////////
////////////只需要在这里更改你的参数即可
////////////////////////////////////////////////////////////////////////////
//主服务或者 外设
var main_uuid='';
// 开始命令
var start_code = ''
//停止命令
var stop_code= ''
//需要查找的服务
var write_service =''
////////////////////////////////////////////////////////////////
//可加我微信好友单聊 w857408545
/////////////////////////////////////////////////////////////
Page({
/**
* 页面的初始数据
*/
data: {
startStopBtnText: '开始',
showdata: [],
toview:'t0'
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//观察蓝牙状态
this.watchBleStatus();
this.setShowData('123');
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
//打开蓝牙适配器
console.log("页面显示时打开蓝牙适配器");
this.openBleAdapter();
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
/////////////////////////////////////////////////////////////
////////蓝牙各种操作
/////////////////////////
/**
* 观察蓝牙各种状态
*/
watchBleStatus: function () {
var that = this;
wx.onBluetoothAdapterStateChange(function (res) {
//蓝牙已经关闭
if (res.available == false) {
that.data.switchStatus = false;
console.log(TAG, "手机蓝牙关闭")
that.setShowData("手机蓝牙关闭");
} else {
if (res.discovering == true) {
console.log(TAG, "正在搜索设备..")
that.setShowData("正在搜索设备..");
} else {
console.log(TAG, "蓝牙已打开,未搜索设备");
that.setShowData("蓝牙已打开,未搜索设备");
that.data.switchStatus = true;
}
}
})
wx.onBluetoothDeviceFound(function (devices) {
that.data.devices = devices.devices[0];
that.data.deviceID = devices.devices[0].deviceId;
//断开蓝牙搜索
that.stopBleSearch(0);
//开始进厅设备连接情况
that.autoConnect();
})
},
/**
* 搜索设备
*/
searchDevice: function (e) {
var that = this
if (that.data.switchStatus) {
//正在搜索蓝牙,还没有搜索到设备,则停止搜索
if (mJianHuStatus == 1) {
that.stopBleSearch(1);
//断开蓝牙连接
that.closeBleAdapter();
} else if (mJianHuStatus == 2)//正在接收数据,则断开蓝牙的链接
{
//发送停止监护包
//停止命令
that.writeBleData(stop_code);
// console.log("开始断开蓝牙的链接 mJianHuStatus==", mJianHuStatus);
//断开蓝牙连接
wx.closeBLEConnection({
deviceId: that.data.deviceID,
success: function (res) {
mJianHuStatus = 0;
that.setData(
{
startStopBtnText: '开始',
mBJianHuFinished: true,
}
)
that.closeBleAdapter();
console.log("蓝牙断开成功", mJianHuStatus);
that.setShowData("蓝牙断开成功");
},
})
} else if (mJianHuStatus == 0)//开始搜索设备
{
//that.setShowData('');
that.clearbuffer();
wx.openBluetoothAdapter({
success: function (res) {
console.log("打开蓝牙适配成功");
that.setShowData("打开蓝牙适配成功");
setTimeout(that.dontfindDevice, 30000);
///20s之后搜索不到设备,则停止搜索
wx.startBluetoothDevicesDiscovery({
services: [main_uuid],
success: function (res) {
mJianHuStatus = 1;
that.setData
(
{
startStopBtnColor: '#ff0000',
startStopBtnText: '停止'
}
)
},
fail: function (res) {
mJianHuStatus = 0;
that.setData
(
{
startStopBtnColor: '#ee6565',
startStopBtnText: '开始'
}
)
}
})
},
})
}
} else {
console.log('没有打开蓝牙适配器,请打开蓝牙适配器!');
that.setShowData('没有打开蓝牙适配器,请打开蓝牙适配器!');
}
},
/**
* @20s 之后如果没有找到设备,则停止搜索
*/
dontfindDevice: function () {
// console.log("触发定时器", mJianHuStatus);
if (mJianHuStatus == 1) {
this.stopBleSearch(1);
console.log("没有找到设备,请重新搜索!", mJianHuStatus);
that.setShowData('没有找到设备,请重新搜索!');
}
},
/**
* 向蓝牙通道里面写数据
*/
writeBleData: function (data) {
var that = this;
var hex = data
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
}))
var buffer1 = typedArray.buffer
console.log("发送的数据==" + typedArray)
that.setShowData("发送的数据==" + typedArray);
wx.writeBLECharacteristicValue({
deviceId: that.data.connectedDeviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.writecharacId,
value: buffer1,
success: function (res) {
console.log('writeBLECharacteristicValue success data==', res)
},
fail: function (res) {
console.log('writeBLECharacteristicValue failed data==', res)
},
complete: function (res) {
console.log('writeBLECharacteristicValue complete', res)
}
})
},
/**
* 打开蓝牙适配器
*/
openBleAdapter: function () {
var that = this;
wx.openBluetoothAdapter({
success: function (res) {
that.data.switchStatus = true;
console.log("打开蓝牙适配器成功");
that.setShowData("打开蓝牙适配器成功");
},
fail: function (res) {
that.data.switchStatus = false;
console.log("打开蓝牙适配器失败");
that.setShowData('打开蓝牙适配器失败');
}
})
},
/**
* 停止蓝牙搜索
* f:1手动停止搜索设备 0;找到设备后,主动停止搜索设备
*/
stopBleSearch: function (f) {
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
if (f == 1) {
mJianHuStatus = 0;
that.setData(
{
startStopBtnColor: '#ee6565',
//开始搜索和停止搜索文字
startStopBtnText: '开始'
}
)
}
},
fail: function (res) {
console.log('停止搜索设备失败', res)
that.setShowData('停止搜索设备失败');
}
})
},
/**
* 关闭蓝牙适配器
*/
closeBleAdapter: function () {
var that = this;
wx.closeBluetoothAdapter({
success: function (res) {
console.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论



















收起资源包目录






共 4 条
- 1
xin0089
- 粉丝: 4
- 资源: 3

上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
已下载
下载帮助

内容简介:bleble.zip bleble bleble ble.json ble.wxml ble.wxss ble.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
- 3
前往页