蓝牙微信小程序ble用那个搜索

新手学堂09

蓝牙微信小程序ble用那个搜索,第1张

连接蓝牙设备,需要使用微信的wxcreateBLEConnection接口,参数为蓝牙设备的deviceId(这个参数在搜索的设备信息里面可以直接拿到)。

2 在blejs里面新增connect、disconnect、getDevConStatus三个接口,用以统一管理设备连接和设备断开,因为在安卓平台上,重复去连接会导致设备没有办法断开连接。所以在blejs里面还维护了一个连接/正在连接的设备数组,方便统一管理设备。当调用connect的时候,会先去这个数组里面查找是否有该设备,如果有就不再去调用连接了。当调用disconnect或者设备被动断开,会把该设备从这个数组里面删除。

蓝牙读取数据问题。小程序蓝牙接收不完整是蓝牙读取数据问题导致的。微信小程序,简称小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用。

//indexjs

//获取应用实例

const app = getApp()

const util = require('//utils/utiljs')

const bletool = require('//utils/bletooljs')

Page({

data: {

// lists: [{ 'order_no': '1111', 'car_no': '321', 'car_type': '尚好捷', 'order_date': '2018-01-02 08:00', 'order_money': '1600', 'order_time': '4' }],

car_no: '',

order_no: '',

lists: [],

bleList: [], //蓝牙设备数组

serviceId: '',//592B3370-3900-9A71-4535-35D4212D2837

serviceMac: '',//C9:9B:4C:E7:DE:10

service_psd: '',//855525B837253705595800000329

service_uuid: '',

deviceId:'',

characteristics:[] //特征值

},

onLoad: function (options) {

thisinitBle();

},

onReady: function () {

// 页面渲染完成

},

onShow: function () {

if (appglobalDatacar_nolength>0){

thisgetDeviceInfo();

}

},

onHide: function () {

// 页面隐藏

},

onUnload: function () {

// 页面关闭

appglobalDatacar_no=''

},

//蓝牙相关

//初始化蓝牙

initBle: function () {

var that = this;

wxonBluetoothAdapterStateChange(function (res) {

consolelog('adapterState changed, now is', res)

appglobalDatable_state = resavailable;

if (resavailable) {

thatinitBle();

} else {

utilshowToast('手机蓝牙已关闭');

appglobalDatable_isonnectting = false;

}

})

//打开蓝牙适配器

wxopenBluetoothAdapter({

success: function (res) {

consolelog('打开蓝牙适配器成功');

thatgetBluetoothAdapterState();

appglobalDatable_state = true;

thatonBluetoothDeviceFound();

},

fail: function (res) {

// fail

consolelog(res)

utilshowToast('请打开手机蓝牙');

},

complete: function (res) {

// complete

}

})

},

onBluetoothDeviceFound:function(){

var that = this;

//监听扫描

wxonBluetoothDeviceFound(function (res) {

// res电脑模拟器返回的为数组;手机返回的为蓝牙设备对象

consolelog('监听搜索新设备:', res);

thatupdateBleList([res])

})

},

getBluetoothAdapterState: function () {

var that = this;

wxgetBluetoothAdapterState({

success: function (res) {

var available = resavailable;

var discovering = resdiscovering;

if (!available) {

utilshowToast('蓝牙不可用');

} else {

if (!discovering) {

// thatstartBluetoothDevicesDiscovery();

}

}

}

})

},

startBluetoothDevicesDiscovery: function () {

var that = this;

var services = [];

servicespush(thisdataserviceId);

wxshowLoading({

title: '设备搜索中'

});

setTimeout(function () {

wxhideLoading();

if (appglobalDatadeviceIdlength==0){

utilshowModal('设备搜索失败,请重试');

}

}, 10000)

if(bletoolisIOS()){

wxstartBluetoothDevicesDiscovery({

services: services,

allowDuplicatesKey: true,

success: function (res) {

consolelog('ios搜索成功');

consolelog(res);

},

fail: function (err) {

consolelog(err);

}

});

}else{

wxstartBluetoothDevicesDiscovery({

// services: services,

allowDuplicatesKey: true,

success: function (res) {

consolelog('Android搜索成功');

consolelog(res);

},

fail: function (err) {

consolelog(err);

wxhideLoading();

thatstartBluetoothDevicesDiscovery();

// thatgetBluetoothAdapterState();

utilshowToast('搜索失败');

}

});

}

},

startConnectDevices: function (ltype, array) {

var that = this;

clearTimeout(thatgetConnectedTimer);

thatgetConnectedTimer = null;

wxstopBluetoothDevicesDiscovery({

success: function (res) {

// success

}

})

appglobalDatable_isonnectting = true;

consolelog('连接前:'+thatdeviceId);

wxcreateBLEConnection({

deviceId: thatdeviceId,

success: function (res) {

if (reserrCode == 0) {

consolelog('连接成功:');

thatgetService(thatdeviceId);

}

},

fail: function (err) {

consolelog('连接失败:', err);

wxhideLoading();

utilshowModal('设备连接失败,请重试');

// if (ltype == 'loop') {

// thatconnectDeviceIndex += 1;

// thatloopConnect(array);

// } else {

// thatstartBluetoothDevicesDiscovery();

// thatgetConnectedBluetoothDevices();

// }

appglobalDatable_isonnectting = false;

},

complete: function () {

}

});

},

getService: function (deviceId) {

var that = this;

// 监听蓝牙连接

wxonBLEConnectionStateChange(function (res) {

consolelog(res);

appglobalDatable_isonnectting = resconnected

if (!resconnected) {

utilshowToast('连接断开');

}

});

// 获取蓝牙设备service值

wxgetBLEDeviceServices({

deviceId: deviceId,

success: function (res) {

consolelog('获取蓝牙设备service值');

consolelog(res);

thatgetCharacter(deviceId, resservices);

}

})

},

getCharacter: function (deviceId, services) {

var that = this;

servicesforEach(function (value, index, array) {

if (valueisPrimary) {

thatsetData({

service_uuid: valueuuid,

deviceId: deviceId

})

appglobalDataservice_uuid= valueuuid;

appglobalDatadeviceId=deviceId;

}

});

//监听通知

wxonBLECharacteristicValueChange(function (res) {

// callback

consolelog('value change', res)

const hex = bletoolbuf2char(resvalue)

consolelog('返回的数据:', hex)

//配对密码

if (hexindexOf('855800000106') != -1) {

wxhideLoading();

var charact_write = thatdatacharacteristics[1]

bletoolwriteDataToDevice(thatdatadeviceId, thatdataservice_uuid, charact_write, thatdataservice_psd);

wxshowToast({

title: '设备已连接',

icon: 'success',

duration: 3000

})

setTimeout(function () {

bletoolwriteDataToDevice(thatdatadeviceId, thatdataservice_uuid, charact_write, '235525B837253705590400000273');

}, 2000)

} else if (hexindexOf('23040000') != -1) {

//启动成功

thatstarRenting();

}

})

wxgetBLEDeviceCharacteristics({

deviceId: deviceId,

serviceId: thatgetServiceUUID(),

success: function (res) {

wxgetBLEDeviceCharacteristics({

deviceId: deviceId,

serviceId: thatgetServiceUUID(),

success: function (res) {

consolelog('特征', res)

thatsetData({

characteristics:rescharacteristics

})

appglobalDatacharacteristics = rescharacteristics;

var charact_read = rescharacteristics[0]

},

loopConnect: function (devicesId) {

var that = this;

var listLen = devicesIdlength;

if (devicesId[thisconnectDeviceIndex]) {

thisdeviceId = devicesId[thisconnectDeviceIndex];

thisstartConnectDevices('loop', devicesId);

} else {

consolelog('已配对的设备小程序蓝牙连接失败');

thatstartBluetoothDevicesDiscovery();

thatgetConnectedBluetoothDevices();

}

},

//更新数据 devices为数组类型

updateBleList: function (devices) {

consolelog('设备数据:',devices);

var newData = thisdatableList

var that = this

var tempDevice = null;

for (var i = 0; i < deviceslength; i++) {

//ios设备

if (devices[i]devices != null) {

if (devices[i]deviceslength > 0) {

tempDevice = devices[i]devices[0];

}

else {

continue

}

}

//安卓

else {

tempDevice = devices[i];

}

if (!thisisExist(tempDevice)) {

newDatapush(tempDevice)

}

}

consolelog('数据:');

consolelog(newData)

thissetData({

bleList: newData

})

if (!appglobalDatable_isonnectting) {

var that = this;

thisdatableListforEach(function (value, index, array) {

//找到对应id的设备,ios判断服务id安卓判断mac地址

var deviceId = value['deviceId'];

if(bletoolisIOS()){

let advertisServiceUUID = value['advertisServiceUUIDs'][0];

if (advertisServiceUUID == thatdataserviceIdtoUpperCase()){

thatdeviceId = deviceId;

consolelog(thatdeviceId);

thatstartConnectDevices();

}

}else{

if (deviceId == thatdataserviceMac) {

thatdeviceId = deviceId;

consolelog(thatdeviceId);

thatstartConnectDevices();

}

}

});

}

},

//是否已存在 存在返回true 否则false

isExist: function (device) {

var tempData = thisdatableList

for (var i = 0; i < tempDatalength; i++) {

if (tempData[i]deviceId == devicedeviceId) {

return true

}

}

return false

},

//服务uuid

getServiceUUID: function () {

return bletoolstringTransition(thisdataservice_uuid);

},

getDeviceInfo: function () {

let car_no = appglobalDatacar_no;

var that = this;

wxrequest({

url: appglobalDataserverURL + 'c=car&a=getDeviceInfo&open_id=' + appglobalDataopen_id + '&car_no=' + car_no,

method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

header: { 'content-type': 'application/json' }, // 设置请求的 header

success: function (res) {

// success

var data = resdata;

consolelog(data);

if (dataresult == 1) {

appglobalDataserviceId = datadataservice_id;

appglobalDataserviceMac = datadataservice_mac,

appglobalDataservice_psd = '85' + datadataservice_psd + '5800000329';

thatsetData({

serviceId: datadataservice_id,

serviceMac: datadataservice_mac,

service_psd: '85' + datadataservice_psd+'5800000329',

})

appstartBluetoothDevicesDiscovery();

// thatonBLECharacteristicValueChange();

} else {

utilshowModal(datamsg);

}

},

fail: function () {

},

complete: function () {

// complete

}

});

},

})

搜索不到蓝牙 的一般原因及处理方法:一,一般都会认为 打开蓝牙之后,手机就能搜索到蓝牙,并且成功与之连接,其实这是错误的。若要手机搜索到蓝牙,蓝牙就必须进入一个特殊的状态:配对状态。二,具体操作步骤:1 、确认蓝牙处于关机状态:长按多功能键2-3秒 ,蓝 牙指示红灯快速闪烁3次左右, 即关闭蓝牙。2、蓝牙进入 配对状态:长按多功能键7-8秒,蓝牙进入配对状态 表现为指示灯红蓝两种颜色交替快 速 闪烁或者 是指示灯常亮。三,蓝牙成功进 入配对状态后,接下来进入手机的操作:1、打 开手机的蓝 牙功能,搜索蓝牙 的型号。2、手机与蓝牙连接,如果提示输入pin码,一般为0000或1234。3、配对成功。这时就 可以用蓝 牙接听、打电话了,如果蓝牙支持A2DP协议,还可以用来听音乐。

微信小程序绑定手环使用蓝牙。绑定需要使用蓝牙功能。微信是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序,由张小龙所带领的腾讯广州研发中心产品团队打造。