How to use Bluetooth plug-in by WeChat Mini Programs
Today, the editor will share with you the relevant knowledge about how WeChat Mini Programs uses Bluetooth plug-ins. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Bluetooth.js
Function BLE (options) {this.options = options | | {locator: {}};} function ab2hex (buffer) {const hexArr = Array.prototype.map.call (new Uint8Array (buffer), function (bit) {return ('00' + bit.toString (16). Slice (- 2)}) return hexArr.join (' -')}; BLE.prototype = {open: function (callback) {/ / the adapter is opened successfully to call callback () Wx.openBluetoothAdapter ({success (res) {if (typeof callback! = "undefined") {callback ();}}, fail (res) {console.log (res)})}, scan: function (callback) {/ / scanning device successfully called callback (ls); wx.startBluetoothDevicesDiscovery ({success (res) {wx.getBluetoothDevices ({success (res) {console.log (res)) Var ls = []; for (var I = 0; I < res.devices.length; iTunes +) {console.log (res.devices [I] .name); ls [I] = {deviceId: res.devices [I] .deviceId, name: res.devices [I] .name};} if (typeof callbacks = "undefined") {callback (name) })}, fail (res) {console.log (res) return 0;}})}, link: function (deviceId, callback) {/ / connected device successfully called callback (); wx.createBLEConnection ({/ / the deviceId here needs to have established a link with the corresponding device through createBLEConnection deviceId: deviceId, success (res) {console.log (res)) Wx.getBLEDeviceServices ({/ / the deviceId here needs to have established a link with the corresponding device via createBLEConnection: deviceId: deviceId, success (res) {console.log (res)) Wx.getBLEDeviceCharacteristics ({/ / the deviceId here needs to have established a link with the corresponding device via createBLEConnection: deviceId: deviceId, / / the serviceId here needs to obtain serviceId:'', success (res) {console.log (res) if (typeof callback! = "undefined") {callback () })}, fail (res) {console.log (res)}})}, write: function (deviceId, value, callback) {/ / write data successfully called callback (); const buffer = new ArrayBuffer (value.length); const dataView = new DataView (buffer); for (var I = 0; I < value.length) CharCodeAt +) {dataView.setUint8 (I, value [I]. CharCodeAt ());} console.log (buffer); console.log (buffer); wx.getBLEDeviceServices ({/ / the deviceId here needs to have established a link with the corresponding device via createBLEConnection: deviceId, success (res) {console.log (res)) Wx.getBLEDeviceCharacteristics ({/ / the deviceId here needs to have established a link with the corresponding device through the createBLEConnection: deviceId: deviceId, / / the serviceId here needs to obtain the serviceId in the getBLEDeviceServices interface: '4FAFC201-1FB5-459ELI8FCCLY C5C9C331914B, success (res) {wx.writeBLECharacteristicValue ({deviceId: deviceId, serviceId:' 4FAFC201-1FB5-459EFCCML C5C9C331914B' CharacteristicId: 'BEB5483E-36E1-4688, B7F5, EA07361B26B8, value: buffer, success: function (res) {console.log (res) If (typeof callback! = "undefined") {callback ();}}, fail: function (res) {console.log (res);}})}})}, read: function (deviceId, callback) {/ / read data successfully called callback (xmlString) Wx.onBLECharacteristicValueChange (function (characteristic) {var board = ab2hex (characteristic.value); var bigData = board.split ('-'); var result = []; for (var I = 0; I < bigData.length; idata +) {result.push (String.fromCharCode ('0X' + bigdata [I]));} var xmlString = result.join (''); if (typeof callback! = "undefined") {callback (xmlString);}}) Wx.readBLECharacteristicValue ({/ / the deviceId here needs to have established a link with the corresponding device via createBLEConnection: deviceId: deviceId, / / the serviceId here needs to obtain the serviceId in the getBLEDeviceServices interface: '4FAFC201-1FB5-459E FCClyC5C9C331914Baking, / / the characteristicId here needs to obtain the characteristicId in the getBLEDeviceCharacteristics interface:' BEB5483E-36E1-4688murB7F5Mutual EA07361B26B8, success (res) {/ / console.log ('readBLECharacteristicValue:', res.errCode) Console.log (res);}, fail (res) {console.log (res);}}, close: function (deviceId, callback) {/ / close Bluetooth successfully called callback (); wx.closeBLEConnection ({deviceId: deviceId, success (res) {console.log (res); if (typeof callback! = "undefined") {callback () }) / / wx.closeBluetoothAdapter ({/ / success (res) {/ / console.log (res); / / if (typeof callback! = "undefined") {/ / callback (); / /})} exports.BLE = BLE
Index.js
Const Ble = require ('.. /.. / lib/bluetooch/bluetooch'); var ble = new Ble.BLE (); these are all the contents of the article "how to use the Bluetooth widget by WeChat Mini Programs". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.