マイクロチャンネルBluetooth接続アプレット小さなチケットプリンタ

1. Bluetooth接続

(最初に出版ブログ)
 
//プリンタを開き、近くのBluetoothデバイスを検索するための最初のステップ
startSearch:関数(){
これを= VAR
wx.openBluetoothAdapter({
成功:機能(RES){
wx.getBluetoothAdapterState({
成功:機能(RES){
IF(res.available){
{(res.discovering)場合
wx.stopBluetoothDevicesDiscovery({
成功:機能(RES){
console.log(RES)
}
})
}
that.checkPemission()
}他{
wx.showModal({
タイトル:[ダイアログを表示します]
内容:「使用できないローカルのBluetooth」、
})
}
}、
})
}、
失敗:関数(){
wx.showModal({
タイトル:[ダイアログを表示します]
内容:「Bluetoothの初期化に失敗しました、オープンブルートゥース」
})
}
})
}

2.検索デバイスと結合するために接続され、クリックイベントのリスト

bindViewTap:関数(E){
これを= VAR
wx.stopBluetoothDevicesDiscovery({
成功:機能(RES){
console.log(RES)
}、
})
that.setData({
サービスID:0、
writeCharacter:偽、
readCharacter:偽、
notifyCharacter:偽
})
VAR shebei = e.currentTarget.dataset.title
wx.setStorageSync( 'shebei'、shebei)
wx.showLoading({
タイトル:「接続」、
})
wx.createBLEConnection({
DEVICEID:e.currentTarget.dataset.title、
成功:機能(RES){
console.log(RES)
app.BLEInformation.deviceId = e.currentTarget.dataset.title
console.log(e.currentTarget.dataset.title)
that.getSeviceId()
}、
失敗:関数(E){
wx.showModal({
タイトル:[ダイアログを表示します]
内容:「接続に失敗しました」
})
console.log(E)
wx.hideLoading()
}、
完全:関数(E){
console.log(E)
}
})
}

接続状態を保存するための接続に成功した後、3。

getSeviceId:関数(){
これを= VAR
プラットフォーム= app.BLEInformation.platform
console.log(app.BLEInformation.deviceId)
wx.getBLEDeviceServices({
DEVICEID:app.BLEInformation.deviceId、
成功:機能(RES){
that.setData({
サービス:res.services
})
that.getCharacteristics()
}、
失敗:関数(E){
console.log(E)
}、
完全:関数(E){
console.log(E)
}
})
}
getCharacteristics:関数(){
これを= VAR
リスト= that.data.servicesました
VAR NUM = that.data.serviceId
VaRの書き込み= that.data.writeCharacter
varが読ん= that.data.readCharacter
varが通知= that.data.notifyCharacter
wx.getBLEDeviceCharacteristics({
DEVICEID:app.BLEInformation.deviceId、
サービスID:リスト[NUM] .uuid、
成功:機能(RES){
console.log(RES)
ため(VAR i = 0; iがres.characteristics.lengthを<; ++ I){
VARプロパティ=のres.characteristics [I]の.properties
VAR項目= res.characteristics [i]は.uuid
(もし!通知){
IF(properties.notify){
app.BLEInformation.notifyCharaterId =アイテム
app.BLEInformation.notifyServiceId =リスト[NUM] .uuid
真=通知
}
}
(もし!書き込み){
IF(properties.write){
app.BLEInformation.writeCharaterId =アイテム
app.BLEInformation.writeServiceId =リスト[NUM] .uuid
真=書きます
}
}
(もし!読み){
IF(properties.read){
app.BLEInformation.readCharaterId =アイテム
app.BLEInformation.readServiceId =リスト[NUM] .uuid
= trueを読みます
}
}
}
{(!||!通知||!読みを書き込み)の場合
A ++
that.setData({
writeCharacter:書き込み、
readCharacter:読み取り、
notifyCharacter:通知し、
サービスID:NUM
})
IF(NUM == list.length){
wx.showModal({
タイトル:[ダイアログを表示します]
内容:「読み書きの見られない特性値」
})
}他{
that.getCharacteristics()
}
}他{
that.openControl()
}
}、
失敗:関数(E){
console.log(E)
}、
完全:関数(E){
console.log( "書き込み:" + app.BLEInformation.writeCharaterId)
console.log( "読み:" + app.BLEInformation.readCharaterIdを)
console.log( "通知:" + app.BLEInformation.notifyCharaterId)
}
})
}
/ **
*ライフサイクル機能 - ページのロードのために聞きます
* /
onLoad:機能(オプション){
app.BLEInformation.platform = app.getPlatform()
}
 
 

おすすめ

転載: www.cnblogs.com/zhajinhua/p/10959429.html