This article describes how to implement Ble Bluetooth, a small program. The source code download is provided, which has good reference value. Next let's take a look at the small Editor. This article mainly introduces the implementation of Ble Bluetooth, a small program. The source code download is provided, which has good reference value. Let's take a look at it with the small editor.
Not for a while. I have not written any articles about applets. In March 28, the api was updated again. The long-awaited Bluetooth api update. Start to renew.
Source code address
1. Brief introduction
The Bluetooth adapter interface is supported by the base Library version 1.1.0.
Supported by iOS client 6.5.6 and not supported by Android client
A total of 18 APIs are added for Bluetooth.
2. Api classification
Search
Connection class
Communication
3. specific API usage
For details, see the official website:
Https://mp.weixin.qq.com/debug/wxadoc/dev/api/bluetooth.html#wxgetconnectedbluethoothdevicesobject
4. case studies
4.1 Search for Bluetooth devices
/*** Search for the device interface */Page ({data: {logs: [], list: [],}, onLoad: function () {console. log ('onload') var that = this; // const SDKVersion = wx. getSystemInfoSync (). SDKVersion | '1. 0.0 '// const [MAJOR, MINOR, PATCH] = SDKVersion. split ('. '). map (Number) // console. log (SDKVersion); // console. log (MAJOR); // console. log (MINOR); // console. log (PATCH); // const canIUse = apiName => {// if (apiName = 'showmodal. canc El ') {// return MAJOR> = 1 & MINOR> = 1 //} // return true //} // wx. showModal ({// success: function (res) {// if (canIUse ('showmodal. cancel ') {// console. log (res. cancel) //} // Obtain the adapter wx. openjavasthadapter ({success: function (res) {// success console. log ("----- success ----------"); console. log (res); // start searching for wx. startbluw.thdevicesdiscovery ({services: [], success: function (res) {// success Console. log ("----- startbluw.thdevicesdiscovery -- success ----------"); console. log (res) ;}, fail: function (res) {// fail console. log (res) ;}, complete: function (res) {// complete console. log (res) ;}}, fail: function (res) {console. log ("----- fail ----------"); // fail console. log (res) ;}, complete: function (res) {// complete console. log ("----- complete ----------"); console. log (res) ;}}) wx. ge Tblustmthdevices ({success: function (res) {// success // {devices: Array [11], errMsg: "getBluetoothDevices: OK"} console. log ("getpolicthdevices"); console. log (res); that. setData ({list: res. devices}); console. log (that. data. list) ;}, fail: function (res) {// fail}, complete: function (res) {// complete }}, onShow: function (){}, // Click event processing bindViewTap: function (e) {console. log (e. currentTarget. d Ataset. title); console. log (e. currentTarget. dataset. name); console. log (e. currentTarget. dataset. advertisData); var title = e. currentTarget. dataset. title; var name = e. currentTarget. dataset. name; wx. redirectTo ({url :'.. /conn? DeviceId = '+ title +' & name = '+ name, success: function (res) {// success}, fail: function (res) {// fail}, complete: function (res) {// complete }})},})
4.2 connect to obtain data
/*** Connect the device. Get data */Page ({data: {motto: 'Hello World', userInfo: {}, deviceId: '', name:'', serviceId: '', services: [], cd20: '', cd01:'', cd02: '', cd03:'', cd04: '', characteristics20: null, characteristics01: null, characteristics02: null, characteristics03: null, characteristics04: null, result,}, onLoad: function (opt) {var that = this; console. log ("onLoad"); console. log ('deviceid = '+ opt. deviceId); Console. log ('name = '+ opt. name); that. setData ({deviceId: opt. deviceId});/*** listen to the connection status of the device */wx. onBLEConnectionStateChanged (function (res) {console. log ('device $ {res. deviceId} state has changed, connected: $ {res. connected} ')})/** Connect device */wx. createBLEConnection ({deviceId: that. data. deviceId, success: function (res) {// success console. log (res);/*** the connection is successful, and then you can get the service list of the device */wx. getBLEDeviceSe Rvices ({// here the deviceId needs to be obtained from getgatethdevices above: that. data. deviceId, success: function (res) {console. log ('device services: ', res. services) that. setData ({services: res. services}); console. log ('device services: ', that. data. services [1]. uuid); that. setData ({serviceId: that. data. services [1]. uuid}); console. log ('------------------------------------'); console. log ('device id :', That. data. deviceId); console. log ('device device service id: ', that. data. serviceId);/*** latency: 3 seconds. obtain features based on the service */setTimeout (function () {wx. getBLEDeviceCharacteristics ({// The deviceId here needs to be getdomainthdevices deviceId: that. data. deviceId, // The serviceId here needs to get serviceId: that in the getBLEDeviceServices interface above. data. serviceId, success: function (res) {console. log ('000000' + that. data. serviceId); console. log (' Device getBLEDeviceCharacteristics: ', res. characteristics) for (var I = 0; I <5; I ++) {if (res. characteristics [I]. uuid. indexOf ("cd20 ")! =-1) {that. setData ({cd20: res. characteristics [I]. uuid, characteristics20: res. characteristics [I]});} if (res. characteristics [I]. uuid. indexOf ("cd01 ")! =-1) {that. setData ({cd01: res. characteristics [I]. uuid, characteristics01: res. characteristics [I]});} if (res. characteristics [I]. uuid. indexOf ("cd02 ")! =-1) {that. setData ({cd02: res. characteristics [I]. uuid, characteristics02: res. characteristics [I]});} if (res. characteristics [I]. uuid. indexOf ("cd03 ")! =-1) {that. setData ({cd03: res. characteristics [I]. uuid, characteristics03: res. characteristics [I]});} if (res. characteristics [I]. uuid. indexOf ("cd04 ")! =-1) {that. setData ({cd04: res. characteristics [I]. uuid, characteristics04: res. characteristics [I]}) ;}} console. log ('cd01 = '+ that. data. cd01 + 'cd02 = '+ that. data. cd02 + 'cd03 = '+ that. data. cd03 + 'cd04 = '+ that. data. cd04 + 'cd20 = '+ that. data. cd20);/*** call back to obtain the data sent from the device */wx. onBLECharacteristicValueChange (function (characteristic) {console. log ('characteristic value comed: ', characteristi C. value) // {value: ArrayBuffer, deviceId: "D8: 00: D2: 4F: 24: 17", serviceId: "ba11f08c-5f14-0b0d-1080-007cbe238851-0x600000460240", characteristicId: "Listen cd04-0000-1000-8000-00805f9b34fb-0x60800069fb80"}/*** listen to the results in cd04cd04 */if (characteristic. characteristicId. indexOf ("cd01 ")! =-1) {const result = characteristic. value; const hex = that. buf2hex (result); console. log (hex);} if (characteristic. characteristicId. indexOf ("cd04 ")! =-1) {const result = characteristic. value; const hex = that. buf2hex (result); console. log (hex); that. setData ({result: hex}) ;}}/*** sequential development device feature notifiy */wx. policyblecharacteristicvaluechanged ({deviceId: that. data. deviceId, serviceId: that. data. serviceId, characteristicId: that. data. cd01, state: true, success: function (res) {// success console. log ('categoryblecharacteristicvaluechanged success ', res);}, fail: function (res) {// fail}, complete: function (res) {// complete}) wx. policyblecharacteristicvaluechanged ({deviceId: that. data. deviceId, serviceId: that. data. serviceId, characteristicId: that. data. cd02, state: true, success: function (res) {// success console. log ('categoryblecharacteristicvaluechanged success ', res);}, fail: function (res) {// fail}, complete: function (res) {// complete}) wx. policyblecharacteristicvaluechanged ({deviceId: that. data. deviceId, serviceId: that. data. serviceId, characteristicId: that. data. cd03, state: true, success: function (res) {// success console. log ('categoryblecharacteristicvaluechanged success ', res);}, fail: function (res) {// fail}, complete: function (res) {// complete}) wx. policyblecharacteristicvaluechanged ({// enable devicy function // The deviceId here must be obtained from the getgatethdevices or ongatethdevicefound interface above: that. data. deviceId, serviceId: that. data. serviceId, characteristicId: that. data. cd04, state: true, success: function (res) {console. log ('categoryblecharacteristicvaluechanged success ', res) }}, fail: function (res) {console. log (res) ;}}) }, 1500) ;}}, fail: function (res) {// fail}, complete: function (res) {// complete})},/*** send data to the device */bindViewTap: function () {var that = this; var hex = 'aa5504b0000b5 'var typedArray = new Uint8Array (hex. match (/[\ da-f] {2}/gi ). map (function (h) {return parseInt (h, 16)}) console. log (typedArray) console. log ([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5]) var buffer1 = typedArray. buffer console. log (buffer1) wx. writeBLECharacteristicValue ({deviceId: that. data. deviceId, serviceId: that. data. serviceId, characteristicId: that. data. cd20, value: buffer1, success: function (res) {// success console. log ("success command sent successfully"); console. log (res) ;}, fail: function (res) {// fail console. log (res) ;}, complete: function (res) {// complete }},/*** ArrayBuffer is converted to Hex */buf2hex: function (buffer) {// buffer is an ArrayBuffer return Array. prototype. map. call (new Uint8Array (buffer), x => ('00' + x. toString (16 )). slice (-2 )). join ('');}})
5. display results
The above is the details of Ble Bluetooth, a small program. For more information, see other related articles in the first PHP community!