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,?//?開啟內(nèi)測接口調(diào)用,注入wx.invoke方法,非常重要!!必須有這個 ????debug:?true,//開啟調(diào)試接口,alert運行結(jié)果 ????appId:?'',//必填,公眾號的唯一標識, ????timestamp:?'',//必填,生成簽名的時間戳 ????nonceStr:?'',//必填,生成簽名的隨機串 ????signature:?'',//必填,簽名 ????jsApiList:?[]//要調(diào)用的js函數(shù),必須把函數(shù)名字寫入數(shù)組 });
Here my jsApiList is
jsApiList:?[ ????????????'openWXDeviceLib',//初始化設(shè)備庫(只支持藍牙設(shè)備) ????????????'closeWXDeviceLib',//關(guān)閉設(shè)備庫(只支持藍牙設(shè)備) ????????????'getWXDeviceInfos',//獲取設(shè)備信息(獲取當前用戶已綁定的藍牙設(shè)備列表) ????????????'sendDataToWXDevice',//發(fā)送數(shù)據(jù)給設(shè)備 ????????????'startScanWXDevice',//掃描設(shè)備(獲取周圍所有的設(shè)備列表,無論綁定還是未被綁定的設(shè)備都會掃描到) ????????????'stopScanWXDevice',//停止掃描設(shè)備 ????????????'connectWXDevice',//連接設(shè)備 ????????????'disconnectWXDevice',//斷開設(shè)備連接 ????????????'getWXDeviceTicket',//獲取操作憑證 ????????????'onWXDeviceBindStateChange',//微信客戶端設(shè)備綁定狀態(tài)被改變時觸發(fā)此事件 ????????????'onWXDeviceStateChange',//監(jiān)聽連接狀態(tài),可以監(jiān)聽連接中、連接上、連接斷開 ????????????'onReceiveDataFromWXDevice',//接收到來自設(shè)備的數(shù)據(jù)時觸發(fā) ????????????'onScanWXDeviceResult',//掃描到某個設(shè)備時觸發(fā) ????????????'onWXDeviceBluetoothStateChange',//手機藍牙打開或關(guān)閉時觸發(fā) ????????]
If you want to test whether the WeChat version supports these APIs, you can write like this:
?wx.checkJsApi({ ????jsApiList:?['openWXDeviceLib',?'onScanWXDevicesResult',?'getWXDeviceInfos'],?//?需要檢測的JS接口列表,所有JS接口列表見附錄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重新打開設(shè)備庫==>'); ????????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è)備庫==>'); ????console.log(res); }); wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????console.debug('openWXDeviceLib重新打開設(shè)備庫==>'); ????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://m.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ā)送?請查看控制板'); });
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,?//?開啟內(nèi)測接口調(diào)用,注入wx.invoke方法,非常重要!!必須有這個 ????debug:?true,//開啟調(diào)試接口,alert運行結(jié)果 ????appId:?'',//必填,公眾號的唯一標識, ????timestamp:?'',//必填,生成簽名的時間戳 ????nonceStr:?'',//必填,生成簽名的隨機串 ????signature:?'',//必填,簽名 ????jsApiList:?[]//要調(diào)用的js函數(shù),必須把函數(shù)名字寫入數(shù)組 });
My jsApiList here is
jsApiList:?[ ????????????'openWXDeviceLib',//初始化設(shè)備庫(只支持藍牙設(shè)備) ????????????'closeWXDeviceLib',//關(guān)閉設(shè)備庫(只支持藍牙設(shè)備) ????????????'getWXDeviceInfos',//獲取設(shè)備信息(獲取當前用戶已綁定的藍牙設(shè)備列表) ????????????'sendDataToWXDevice',//發(fā)送數(shù)據(jù)給設(shè)備 ????????????'startScanWXDevice',//掃描設(shè)備(獲取周圍所有的設(shè)備列表,無論綁定還是未被綁定的設(shè)備都會掃描到) ????????????'stopScanWXDevice',//停止掃描設(shè)備 ????????????'connectWXDevice',//連接設(shè)備 ????????????'disconnectWXDevice',//斷開設(shè)備連接 ????????????'getWXDeviceTicket',//獲取操作憑證 ????????????'onWXDeviceBindStateChange',//微信客戶端設(shè)備綁定狀態(tài)被改變時觸發(fā)此事件 ????????????'onWXDeviceStateChange',//監(jiān)聽連接狀態(tài),可以監(jiān)聽連接中、連接上、連接斷開 ????????????'onReceiveDataFromWXDevice',//接收到來自設(shè)備的數(shù)據(jù)時觸發(fā) ????????????'onScanWXDeviceResult',//掃描到某個設(shè)備時觸發(fā) ????????????'onWXDeviceBluetoothStateChange',//手機藍牙打開或關(guān)閉時觸發(fā) ????????]
If you want to test whether the WeChat version supports these apis, you can write like this:
?wx.checkJsApi({ ????jsApiList:?['openWXDeviceLib',?'onScanWXDevicesResult',?'getWXDeviceInfos'],?//?需要檢測的JS接口列表,所有JS接口列表見附錄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重新打開設(shè)備庫==>'); ????????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è)備庫==>'); ????console.log(res); }); wx.invoke('openWXDeviceLib',?{connType:?'blue'},?function?(res)?{ ????console.debug('openWXDeviceLib重新打開設(shè)備庫==>'); ????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://m.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ā)送?請查看控制板'); });
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)
