1.ブルートゥース初期化
wx.openBluetoothAdapter({
success() {
setTimeout(()=>{
that.getBluetoothAdapterState()
}, 200)
},
fail() {
console.log('携帯電話のBluetoothがオンになっていない')
}
})
注:すべてのBluetooth操作は、最初にBluetoothを初期化する必要があります。
1.すべてのBluetooth操作は、最初に初期化する必要があります。
2.ユーザーの Bluetooth スイッチがオンになっていない場合、または電話が Bluetooth をサポートしていない場合、wx.openBluetoothAdapter を呼び出すと、電話の Bluetooth 機能が使用できないことを示すエラーが返されます。
2.Bluetoothが利用可能かどうかの検出
getBluetoothAdapterState() {
wx.getBluetoothAdapterState({
success(res) {
console.log(res)
if (res.available) {
that.startBluetoothDevicesDiscovery()
}
},
fail(error) {
}
})
},
注意事項
1.一度だけ実行、wx.onBluetoothAdapterStateChangeメソッドは、リアルタイムのBluetooth操作リスニングすることができます。
3.Bluetoothデバイス検索の初期化
startBluetoothDevicesDiscovery() {
wx.startBluetoothDevicesDiscovery({
success(res) {
console.log(res)
that.getBluetoothDevices()
},
fail(error) {
}
})
},
4.検索されたBluetoothデバイスを取得
getBluetoothDevices() {
wx.getBluetoothDevices({ //現在検索されているすべてのBluetoothデバイスを取得する
成功 {
console.log(res)
setTimeout(()=>{
if(res.devices.length < 1) { //Bluetoothをオフにし、デバイスが1台以下になったら検索を停止する
wx.stopBluetoothDevicesDiscovery()
wx.closeBluetoothAdapter()
}
}, 15000)
}
})
wx.onBluetoothDeviceFound(res=>{ //検索されたブルートゥースを聴く-常に新しい設備を探している
let devices = res.devices
for(let item of devices) {
let advertisData = that.buf2hex(item.advertisData) //Androidとiosに共通するブロードキャスト・データは本来deviceIdによって決定されるが、iosのdeviceIdは正しいものではない
if(advertisData.toUpperCase().indexOf('ブルートゥースのマックアドレス') != -1) { //ハードウェアエンジニアが提供した、またはAndroidシステムが取得したBluetooth macアドレスを検索し、BluetoothデバイスIDが存在する場合はそれを取得する。
let deviceId = item.deviceId //BluetoothデバイスIDを取得する。
wx.stopBluetoothDevicesDiscovery({ //新しいデバイスを探すのをやめる
成功 {
setTimeout(()=>{
that.createBLEConnection(deviceId)
}, 200)
}
})
}
}
})
},
5.BluetoothデバイスIDによる低消費電力Bluetoothデバイスの接続
createBLEConnection(deviceId) {
wx.createBLEConnection({
deviceId,
success(res) {
console.log(res)
that.getBLEDeviceServices(deviceId)
}
})
},
6.BluetoothデバイスIDを介してBluetoothのすべてのサービスを取得します。
getBLEDeviceServices(deviceId) {
wx.getBLEDeviceServices({
deviceId,
success(res) {
console.log(res)
that.getBLEDeviceCharacteristics(deviceId)
}
})
},
7.Bluetooth固有値サービスidとBluetoothデバイスidを通して、Bluetooth固有値の読み書きのためのuuidを取得します。
getBLEDeviceCharacteristics(deviceId) {
console.log(services)
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId: '', //Bluetooth固有値サービスID
成功 {
console.log(res) //主に、Bluetooth固有値の読み書き用のuuidがあるかどうかをチェックする
セットタイムアウト=>{
that.notifyBLECharacteristicValueChange(deviceId)
}, 200)
}
})
},
注意事項
1. bluetooth 機能の値はハードウェアエンジニアに教えてください。
2.返されるread/write uuidは配列で、どれがreadかwriteかわかりません。
8.Bluetooth通知機能を有効にします。
notifyBLECharacteristicValueChange(deviceId) {
wx.notifyBLECharacteristicValueChange({
deviceId, //ブルートゥース・デバイスID
serviceId: '', //ブルートゥース固有値サービスID
characteristicId: '', //ブルートゥース固有値読み取り uuid
state: true, //通知を有効にするかどうか
成功 {
that.onBLECharacteristicValueChange()
setTimeout(()=>{
that.writeBLECharacteristicValue(deviceId)
},200)
}
})
},
9.Bluetooth機器からのプッシュ受信
onBLECharacteristicValueChange() {
wx.onBLECharacteristicValueChange(res=>{
let data = that.buf2string(res.value) //通常のテキストを10進数で解析する
console.log(data)
})
},
10.低電力Bluetoothデバイスの機能値へのバイナリデータの書き込み
writeBLECharacteristicValue(deviceId){
let str = '{code: 1, data: {md5: gffd544, ts: 3654}, msg: "hello"}' //データを定義する
//放送データに変換する
let buffer = new ArrayBuffer(str.length)
let dataView = new DataView(buffer)
for (var i = 0; i < str.length; i++) {
dataView.setUint8(i, str.charAt(i).charCodeAt())
}
// let dataHex = buf2hex(buffer); //バイナリに変換する
wx.writeBLECharacteristicValue({
deviceId, //BluetoothデバイスID
serviceId: '', //ブルートゥース固有値サービスID
characteristicId: '', //Bluetoothの固有値はuuidと表記される
value: buffer,
success (res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
})
}
データ変換
データをバイナリにブロードキャスト
buf2hex(buffer) {
let hexArr = Array.prototype.map.call(new Uint8Array(buffer), bit=>{
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('')
},
10進数、通常のテキストにパース
buf2string(buffer) {
let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
return arr.map((char, i) => {
return String.fromCharCode(char)
}).join('')
},