1. 程式人生 > >微信小程式藍芽連線傳送資訊

微信小程式藍芽連線傳送資訊

///獲取應用例項

var app = getApp()

Page({

data: {

lanya: "",

state: "",

msg: "",

sousuo: "",

status: "",

connectedDeviceId: "",//連線裝置Id

devices: [],

serviceId: "",

writeCharacteristicId: "",

notifyCharacteristicId: "",

},

onLoad: function () {

var that = this;

wx.openBluetoothAdapter({

success: function (res) {

console.log(res.errMsg);

that.setData({

lanya: "初始化小程式藍芽" + res.errMsg

});



},

fail: function (res) {

console.log(res);

that.setData({

lanya: "初始化小程式藍芽" + res.errMsg

});

}

});

},

adapterState: function () {

//獲取本機藍芽介面卡狀態

var that = this;

wx.getBluetoothAdapterState({

success: function (res) {

console.log(res);

that.setData({

state: res.errMsg

});

}

});

},

selectDevices: function () {//搜尋裝置

var that = this;

wx.startBluetoothDevicesDiscovery({

allowDuplicatesKey: false,

success: function (res) {

//監聽藍芽介面卡狀態

wx.onBluetoothAdapterStateChange(function (res) {

that.setData({

sousuo: res.discovering ? "在搜尋" : "未搜尋",

status: res.available ? "可用" : "不可用",

});

});

setTimeout(function () {

that.getDevices();

}, 1000);



}

});

},

getDevices: function () {

//獲取裝置列表

var that = this;

wx.getBluetoothDevices({

success: function (res) {

that.setData({

devices: res.devices

});

//that.test(res.devices);

}

});

},

//停止搜尋周邊裝置

stop: function () {

var that = this;

wx.stopBluetoothDevicesDiscovery({

success: function (res) {

that.setData({

sousuo: res.discovering ? "在搜尋" : "未搜尋",

status: res.available ? "可用" : "不可用",

});



}

})

},



test: function (devices) {

wx.onBluetoothDeviceFound(function (devices) {

console.log(devices);

function ab2hex(buffer) {

var hexArr = Array.prototype.map.call(

new Uint8Array(buffer),

function (bit) {

return ('00' + bit.toString(16)).slice(-2)

}

)

return hexArr.join('');

}

console.log('new device list has founded');

console.log(ab2hex(devices.devices[0].advertisData));



});

},

connect: function (e) {//連線藍芽

var that = this;

wx.showModal({

title: '提示',

content: '確定連線此裝置嗎?',

success: function (res) {

if (res.confirm) {

console.log('使用者點選確定');

var clickId = e.currentTarget.id;

wx.createBLEConnection({

// 這裡的 deviceId 需要已經通過 createBLEConnection 與對應裝置建立連結

deviceId: clickId,

success: function (res) {

that.setData({

connectedDeviceId: clickId

});

wx.showToast({

title: '成功',

icon: 'success',

duration: 2000

});

wx.getBLEDeviceServices({

// 這裡的 deviceId 需要已經通過 createBLEConnection 與對應裝置建立連結

deviceId: clickId,

success: function (res) {

console.log('device services:', res.services);

that.setData({

service: res.services[1]

});

}

});

that.getService(clickId);

setTimeout(function () {

that.getCharacteristics(clickId, that.data.serviceId);

}, 3000);

wx.onBLEConnectionStateChange(function (res) {

// 該方法回撥中可以用於處理連線意外斷開等異常情況

console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)

});

}, fail: function (res) {

console.log(res.errMsg);

}

});

} else if (res.cancel) {

console.log('使用者點選取消')

}

}

});

},

getService: function (deviceId) {

var that = this;

wx.getBLEDeviceServices({

// 這裡的 deviceId 需要已經通過 createBLEConnection 與對應裝置建立連結

deviceId: deviceId,

success: function (res) {

console.log('device services:', res.services);

that.setData({

serviceId: res.services[1].uuid

});

}

});

},

getCharacteristics: function (deviceId, serviceId) {

var that = this;

wx.getBLEDeviceCharacteristics({

// 這裡的 deviceId 需要已經通過 createBLEConnection 與對應裝置建立連結

deviceId: deviceId,

// 這裡的 serviceId 需要在上面的 getBLEDeviceServices 介面中獲取

serviceId: serviceId,

success: function (res) {

console.log(res);

console.log('device getBLEDeviceCharacteristics:', res.characteristics);

for (var i = 0; i < res.characteristics.length; i++) {

if (res.characteristics[i].properties.write && !res.characteristics[i].properties.read &&

!res.characteristics[i].properties.notify && !res.characteristics[i].properties.indicate) {

that.setData({

writeCharacteristicId: res.characteristics[i].uuid

});

} else if (!res.characteristics[i].properties.write && !res.characteristics[i].properties.read &&

res.characteristics[i].properties.notify && !res.characteristics[i].properties.indicate) {

that.setData({

notifyCharacteristicId: res.characteristics[i].uuid

});

}

}

console.log("writeCharacteristicId=" + that.data.writeCharacteristicId);

console.log("notifyCharacteristicId=" + that.data.notifyCharacteristicId);

wx.notifyBLECharacteristicValueChange({

state: true, // 啟用 notify 功能

// 這裡的 deviceId 需要已經通過 createBLEConnection 與對應裝置建立連結

deviceId: deviceId,

// 這裡的 serviceId 需要在上面的 getBLEDeviceServices 介面中獲取

serviceId: serviceId,

// 這裡的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 介面中獲取

characteristicId: that.data.notifyCharacteristicId,

success: function (res) {

console.log('notifyBLECharacteristicValueChange success', res.errMsg);

}

});

that.onBLECharacteristicValueChange();

}

});

},

writeMsg: function () {

var that = this;

let buffer = new ArrayBuffer(7);

let dataView = new DataView(buffer);

dataView.setUint8(0, 0xaa);

dataView.setUint8(1, 0x04);

dataView.setUint8(2, (1 << 7));

dataView.setUint8(3, (1 << 3 | 1));

dataView.setUint8(4, 1);

dataView.setUint8(5, 1 << 2);



// var buffer = new ArrayBuffer(7);

// buffer[0] = ;

// buffer[1] = ;

// buffer[2] = (1 << 7)

// buffer[3] = (1 << 3) | 1

// buffer[4] = 1;

// buffer[5] = 10;

console.log(buffer);

wx.writeBLECharacteristicValue({

// 這裡的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 介面中獲取

deviceId: that.data.connectedDeviceId,

// 這裡的 serviceId 需要在上面的 getBLEDeviceServices 介面中獲取

serviceId: that.data.serviceId,

// 這裡的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 介面中獲取

characteristicId: that.data.writeCharacteristicId,

// 這裡的value是ArrayBuffer型別

value: buffer,

success: function (res) {

console.log('writeBLECharacteristicValue success', res.errMsg)

}

});

},

onBLECharacteristicValueChange: function () {

console.log("start change listen");

wx.onBLECharacteristicValueChange(function (res) {

console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)

console.log(ab2hext(res.value))

});

},

closeBLEConnection: function () {

//斷開藍芽連線

var that = this;

wx.closeBLEConnection({

deviceId: that.data.connectedDeviceId,

success: function (res) {

console.log(res);

}

})



}



});