1.Introduce JS library
?<script type='text/javascript' src='//res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
2.Inject configuration information into the page
wx.config({ ????beta:?true,?//?開(kāi)啟內(nèi)測(cè)接口調(diào)用,注入wx.invoke方法,非常重要!!必須有這個(gè) ????debug:?true,//開(kāi)啟調(diào)試接口,alert運(yùn)行結(jié)果 ????appId:?'',//必填,公眾號(hào)的唯一標(biāo)識(shí), ????timestamp:?'',//必填,生成簽名的時(shí)間戳 ????nonceStr:?'',//必填,生成簽名的隨機(jī)串 ????signature:?'',//必填,簽名 ????jsApiList:?[]//要調(diào)用的js函數(shù),必須把函數(shù)名字寫(xiě)入數(shù)組 });
Here my jsApiList is
jsApiList:?[ ????????????'openWXDeviceLib',//初始化設(shè)備庫(kù)(只支持藍(lán)牙設(shè)備) ????????????'closeWXDeviceLib',//關(guān)閉設(shè)備庫(kù)(只支持藍(lán)牙設(shè)備) ????????????'getWXDeviceInfos',//獲取設(shè)備信息(獲取當(dāng)前用戶(hù)已綁定的藍(lán)牙設(shè)備列表) ????????????'sendDataToWXDevice',//發(fā)送數(shù)據(jù)給設(shè)備 ????????????'startScanWXDevice',//掃描設(shè)備(獲取周?chē)械脑O(shè)備列表,無(wú)論綁定還是未被綁定的設(shè)備都會(huì)掃描到) ????????????'stopScanWXDevice',//停止掃描設(shè)備 ????????????'connectWXDevice',//連接設(shè)備 ????????????'disconnectWXDevice',//斷開(kāi)設(shè)備連接 ????????????'getWXDeviceTicket',//獲取操作憑證 ????????????'onWXDeviceBindStateChange',//微信客戶(hù)端設(shè)備綁定狀態(tài)被改變時(shí)觸發(fā)此事件 ????????????'onWXDeviceStateChange',//監(jiān)聽(tīng)連接狀態(tài),可以監(jiān)聽(tīng)連接中、連接上、連接斷開(kāi) ????????????'onReceiveDataFromWXDevice',//接收到來(lái)自設(shè)備的數(shù)據(jù)時(shí)觸發(fā) ????????????'onScanWXDeviceResult',//掃描到某個(gè)設(shè)備時(shí)觸發(fā) ????????????'onWXDeviceBluetoothStateChange',//手機(jī)藍(lán)牙打開(kāi)或關(guān)閉時(shí)觸發(fā) ????????]
If you want to test whether the WeChat version supports these APIs, you can write like this:
?wx.checkJsApi({ ????jsApiList:?['openWXDeviceLib',?'onScanWXDevicesResult',?'getWXDeviceInfos'],?//?需要檢測(cè)的JS接口列表,所有JS接口列表見(jiàn)附錄2, ????success:?function?(res)?{ ????????console.log(res); ????} });
3. Initialize the device library function
Through the ready interface Handling successful verification
wx.ready(function?()?{?????????? ????wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????????console.debug('openWXDeviceLib重新打開(kāi)設(shè)備庫(kù)==>'); ????????console.log(res); ????}); })
Pitfall: Rescanning the device can’t scan out anything at all, even refreshing the page doesn’t work
Solution: Before each scan, call closeWXDeviceLib Close the device library, and then call openWXDeviceLib to open the device library. This is equivalent to reinitializing the device library. If you scan again now, you can scan the device.
Code:
wx.invoke("stopScanWXDevice",?{},?function?(res)?{ ????console.debug('stopScanWXDevice'); ????console.log(res); ?}); wx.invoke("closeWXDeviceLib",?{},?function?(res)?{ ????console.debug('closeWXDeviceLib關(guān)閉設(shè)備庫(kù)==>'); ????console.log(res); }); wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????console.debug('openWXDeviceLib重新打開(kāi)設(shè)備庫(kù)==>'); ????console.log(res); });
4. Listen to the information returned by the device
wx.on('onReceiveDataFromWXDevice',?function?(res)?{ ????console.warn('onReceiveDataFromWXDevice=>'); ????console.log(JSON.stringify(res)); });
5.Send a message to the device
Base64 encoding and decoding is required before sending and receiving data.
Here, I use a library:
????<script type='text/javascript' src='base64.js'></script>
Source:
http://www.miracleart.cn/
var?data={"deviceId":deviceId,"base64Data":?Base64.encode('你要發(fā)送的數(shù)據(jù)')}; console.log(data); wx.invoke('sendDataToWXDevice',data?,?function(res){ ????//回調(diào) ????console.info('發(fā)消息到設(shè)備sendMsg'); ????console.log(data); ????console.log(res); ????$('#dataFromDevice').append('發(fā)送消息的結(jié)果:'+JSON.stringify(res)); ????alert('已發(fā)送?請(qǐng)查看控制板'); });
Description:
1. You need to be in the corresponding device number of WeChat to use the corresponding API.
2. The api must be used normally under the secure domain name set by the device number
3. All console.log and other output to the console in this article use the vconsole debugging tool accomplish.
1.Introduce JS library
?<script type='text/javascript' src='//res.wx.qq.com/open/js/jweixin-1.0.0.js'></script>
2.Inject configuration information into the page
wx.config({ ????beta:?true,?//?開(kāi)啟內(nèi)測(cè)接口調(diào)用,注入wx.invoke方法,非常重要!!必須有這個(gè) ????debug:?true,//開(kāi)啟調(diào)試接口,alert運(yùn)行結(jié)果 ????appId:?'',//必填,公眾號(hào)的唯一標(biāo)識(shí), ????timestamp:?'',//必填,生成簽名的時(shí)間戳 ????nonceStr:?'',//必填,生成簽名的隨機(jī)串 ????signature:?'',//必填,簽名 ????jsApiList:?[]//要調(diào)用的js函數(shù),必須把函數(shù)名字寫(xiě)入數(shù)組 });
My jsApiList here is
jsApiList:?[ ????????????'openWXDeviceLib',//初始化設(shè)備庫(kù)(只支持藍(lán)牙設(shè)備) ????????????'closeWXDeviceLib',//關(guān)閉設(shè)備庫(kù)(只支持藍(lán)牙設(shè)備) ????????????'getWXDeviceInfos',//獲取設(shè)備信息(獲取當(dāng)前用戶(hù)已綁定的藍(lán)牙設(shè)備列表) ????????????'sendDataToWXDevice',//發(fā)送數(shù)據(jù)給設(shè)備 ????????????'startScanWXDevice',//掃描設(shè)備(獲取周?chē)械脑O(shè)備列表,無(wú)論綁定還是未被綁定的設(shè)備都會(huì)掃描到) ????????????'stopScanWXDevice',//停止掃描設(shè)備 ????????????'connectWXDevice',//連接設(shè)備 ????????????'disconnectWXDevice',//斷開(kāi)設(shè)備連接 ????????????'getWXDeviceTicket',//獲取操作憑證 ????????????'onWXDeviceBindStateChange',//微信客戶(hù)端設(shè)備綁定狀態(tài)被改變時(shí)觸發(fā)此事件 ????????????'onWXDeviceStateChange',//監(jiān)聽(tīng)連接狀態(tài),可以監(jiān)聽(tīng)連接中、連接上、連接斷開(kāi) ????????????'onReceiveDataFromWXDevice',//接收到來(lái)自設(shè)備的數(shù)據(jù)時(shí)觸發(fā) ????????????'onScanWXDeviceResult',//掃描到某個(gè)設(shè)備時(shí)觸發(fā) ????????????'onWXDeviceBluetoothStateChange',//手機(jī)藍(lán)牙打開(kāi)或關(guān)閉時(shí)觸發(fā) ????????]
If you want to test whether the WeChat version supports these apis, you can write like this:
?wx.checkJsApi({ ????jsApiList:?['openWXDeviceLib',?'onScanWXDevicesResult',?'getWXDeviceInfos'],?//?需要檢測(cè)的JS接口列表,所有JS接口列表見(jiàn)附錄2, ????success:?function?(res)?{ ????????console.log(res); ????} });
3. Initialization Device library function
Processes successful verification through ready interface
wx.ready(function?()?{?????????? ????wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????????console.debug('openWXDeviceLib重新打開(kāi)設(shè)備庫(kù)==>'); ????????console.log(res); ????}); })
Pitfall: Nothing can be found at all by rescanning the device, even refreshing the page is useless
Solution: Before each scan, call closeWXDeviceLib to close the device library, and then call openWXDeviceLib to open the device library. This is equivalent to reinitializing the device library. If you scan again now, you can scan the device.
Code:
wx.invoke("stopScanWXDevice",?{},?function?(res)?{ ????console.debug('stopScanWXDevice'); ????console.log(res); ?}); wx.invoke("closeWXDeviceLib",?{},?function?(res)?{ ????console.debug('closeWXDeviceLib關(guān)閉設(shè)備庫(kù)==>'); ????console.log(res); }); wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????console.debug('openWXDeviceLib重新打開(kāi)設(shè)備庫(kù)==>'); ????console.log(res); });
4. Listen to the information returned by the device
wx.on('onReceiveDataFromWXDevice',?function?(res)?{ ????console.warn('onReceiveDataFromWXDevice=>'); ????console.log(JSON.stringify(res)); });
5.Send a message to the device
Base64 encoding and decoding is required before sending and receiving data.
Here, I use a library:
????<script type='text/javascript' src='base64.js'></script>
Source:
http://www.miracleart.cn/
var?data={"deviceId":deviceId,"base64Data":?Base64.encode('你要發(fā)送的數(shù)據(jù)')}; console.log(data); wx.invoke('sendDataToWXDevice',data?,?function(res){ ????//回調(diào) ????console.info('發(fā)消息到設(shè)備sendMsg'); ????console.log(data); ????console.log(res); ????$('#dataFromDevice').append('發(fā)送消息的結(jié)果:'+JSON.stringify(res)); ????alert('已發(fā)送?請(qǐng)查看控制板'); });
Description:
1. You need to be in the corresponding device number of WeChat to use the corresponding API.
2. The api must be used normally under the secure domain name set by the device number
3. All console.log and other output to the console in this article use the vconsole debugging tool accomplish.
For more articles related to the Incomplete Guide to WeChat Hardware JS-Api Development, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
