ブルートゥースに接続されたマイクロチャネルは、(電子スケールブランド:マクロくん)電子体重計テーブルアプレット:先行詞は、現在の体重データは、Bluetoothを介して直接に秤量し、画面上に表示取得しました。
PS:記録時は、まだ開発段階では、計量上のデータを、リアルタイムで変化しますが、実際のデータではなくなっています
PS:記録時は、まだ開発段階では、計量上のデータを、リアルタイムで変化しますが、実際のデータではなくなっています
注⚠️
- データのみ、何もデータが書き込まれていない、アプレット公式文書ビューア特定のAPIを読み込む必要
- Bluetooth対応の携帯電話がオンになっていることを確認し、あなたは、Bluetoothデバイスの電子スケールを検索することができ、アンドロイド、IOSサーチ未満に見つけることができます
- 多くのBluetoothデバイスを検索するための小型のマイクロチャネルプログラム、アンドロイド、ディスプレイ上のBluetoothデバイスのメインサービスのDEVICEIDショーのMACアドレスは、iOSのBluetoothデバイスのメインサービスをのuuid
- 最終結果は、16進数の文字列の必要性に、ArrayBufferデータ型を取得し、その後、バイナリデータ10に変換されます。
(1)初期設定
Bluetoothモジュールの初期化--- wx.openBluetoothAdapter
// 定义数据
data: {
devices: [], // 搜索到的蓝牙设备 deviceId 数组
deviceId: '', // 目标蓝牙设备 deviceId
services: [] // 设备服务列表 serviceId 数组
serviceId: '',
characteristics: [] // 特征值列表
characteristicId: '' // 选择某一个特征值
value: '' // 16 进制数据值
}
// 蓝牙 API 调用步骤
openBluetoothAdapter() {
wx.openBluetoothAdapter({ // (1)
success: res => {
console.log('openBluetoothAdapter初始化蓝牙模块成功:', res)
this.startBluetoothDevicesDiscovery() // (2) 开始搜索
},
fail: err => {
console.log('openBluetoothAdapter初始化蓝牙模块失败:', err)
if (err.errCode === 10001) { // 当前蓝牙适配器不可用
wx.onBluetoothAdapterStateChange( res => {
if (res.available) {
this.startBluetoothDevicesDiscovery()
}
})
}
}
})
}
(2)Bluetoothデバイスを検索
wx.startBluetoothDevicesDiscovery ---のBluetooth周辺機器近くを検索
- 主なサービスのリストを検索するためのアクションへのリファレンスサービスはこのパラメータを設定している場合、一部のBluetoothデバイスは、UUIDの彼らの主なサービスをブロードキャストすることができますBluetoothデバイスをのuuid、放送などのBluetoothデバイスのための唯一の検索がを通じて、主なサービスに対応してのuuid Bluetoothデバイスを取り巻くアウトフィルタに他のパラメータは、治療を必要としません
- 効果参照にAllowDuplicatesKeyが報告、報告はメソッドを複数回、同じデバイスをwx.onBlueToothDeviceFoundが、RSSI値がfalseにデフォルトとして、異なるものになる場合は、繰り返しが可能、同じデバイスを繰り返したレポートに許可されている
サービス::[「料金7」]主要サービスEG UUIDは料金7で、このパラメータを渡し、デバイスのための唯一の主要検索サービス料金7 UUID、デバイスは、Bluetoothスマートライトのマイクロチャネルのハードウェア・プラットフォームであります
操作⚠️消費するシステムリソースを停止する検索コールwx.stopBluetoothDevicesDiscovery方法の後、ために探索する必要があるとコネクト装置
startBluetoothDevicesDiscovery() {
wx.startBluetoothDevicesDiscovery({
success: res => {
console.log('startBluetoothDevicesDiscovery开始搜索外围设备成功:', res)
this.getBluetoothDevices() // (3) 获取蓝牙列表
},
fail: err => {
console.log('startBluetoothDevicesDiscovery搜索外围设备失败:', err)
}
})
}
(3)Bluetoothデバイスを取得します
Bluetoothデバイスを含む、検出されたすべてのBluetoothデバイス、中に取得した力へのブルートゥースモジュールが正常--- wx.getBluetoothDevicesに接続されていました
getBluetoothDevices() {
wx.getBluetoothDevices({
success: res => {
console.log('getBluetoothDevices获取蓝牙设备成功:', res)
this.setData({
devices: res. devices || [] // uuid 对应的的已连接设备列表
})
this.createBLEConnection(); // (4) 与目标设备建立连接
},
fail: err => {
console.log('getBluetoothDevices获取蓝牙设备失败:', err)
}
})
}
(4)接続を確立します
Bluetoothデバイスとの接続を確立し、あなたは低消費電力を必要とするBluetoothデバイス--- wx.createBLEConnection
⚠️小さなマイクロチャネルプログラムは、Bluetoothデバイスを検索し、接続を正常に確立していた場合、再検索せずに、渡す前にデバイスを直接接続するために、直接取得DEVICEIDトライを検索
createBLEConnection() {
// 如果是第一次建立连接,可以通过名称匹配,获取相应设备的 deviceId
let devices = this.data.devices;
devices.forEach(item => {
if(item.name == 'kunHong') {
this.setData({
deviceId: item.deviceId
})
}
})
// 建立连接
wx.createBLEConnection({
deviceId: this.data.deviceId,
success: res => {
console.log('createBLEConnection与目标蓝牙连接成功:', res)
this.getBLEDeviceServices() // (5)获取服务
},
fail: err => {
console.log('createBLEConnection与目标蓝牙连接失败:', err)
}
})
}
(5)Bluetoothデバイスのサービスを得るために
Bluetoothデバイスはwx.getBLEDeviceServicesを主なサービスがすべてのuuidを取得します---
- DEVICEIDでwx.getBluetoothDevicesのために取得した基準ターゲットBluetoothデバイスにDEVICEID
リストのすべて(サービスIDを通じて、公式文書を⚠️開発プロセス、メインサービスをServiceIDとサービスの価値特性の主な特徴の実際の動作は、ターゲットの類似したBluetoothデバイスIDを与えるように選択されているが、デモは、アプレットおよび特性)、区別する必要があります
getBLEDeviceServices() {
wx.getBLEDeviceServices({
deviceId: this.data.deviceId,
success: res => {
console.log('getBLEDeviceServices获取蓝牙设备服务', res)
// getBluetoothDevices 获取的有 deviceId 和 advertisServiceUUIDs,可以在这里获取的服务列表中选择一个一样的作为后续 API 请求的服务id,这个 id 需要满足是否可读
this.setData({
services: res.services,
serviceId: res.services[0].uuid // 假设是第一个
})
this.getBLEDeviceCharacteristics() // (6) 获取特征值
// 官方 demo
for(var i = 0; i < res.services.length; i++) {
// 该服务是否为主服务
if(res.services[i].isPrimary) {
this.getBLEDeviceCharacteristics(res.services[i].uuid)
}
}
},
fail: err => {
console.log('getBLEDeviceServices获取蓝牙设备服务失败:', err)
}
})
}
(6)は、特徴量を取得します
サービスを取得するためにBluetoothデバイスのすべての固有値--- wx.getBLEDeviceCharacteristics
- DEVICEIDでwx.getBluetoothDevicesのために取得した基準ターゲットBluetoothデバイスにDEVICEID
- wx.getBLEDeviceServicesによって得られた基準をServiceID BluetoothサービスのUUIDへ
getBLEDeviceCharacteristics(serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
success: res => {
console.log('getBLEDeviceCharacteristics获取蓝牙服务特征值成功:', res)
this.setData({
characteristics: res. characteristics,
characteristics: res. characteristics[0].uuid
})
this.notifyBLECharacteristicValueChange(); // (7)启用 notify 功能
// 官方 demo
for(var i = 0; i < res.characteristics.length; i++) {
// 是否可读
if(res.characteristics[i].read) {
// 读取数据
wx.readBLECharacteristicValue({
deviceId: this.data.deviceId,
serviceId: serviceid,
characteristicId: res.characteristicId[i].uuid
})
},
if(res.characteristics[i].properties.notify || res.characteristics[i].properties.indicate) {
// 启用功能
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
})
}
}
},
fail: err => {
console.log('getBLEDeviceCharacteristics获取蓝牙服务特征值失败:', err)
}
})
this.onBLECharacteristicValueChange() // (8)监听特征值变化
this.readBLECharacteristicValue(); // (9)读取数据
}
(7)機能が有効に通知します
、低消費電力のBluetooth機能値の変更中に機能を通知サブスクリプション機能の値を有効にします
⚠️デバイスの特性を通知したり、彼らが正常に有効化する前に、サポートの価値を示さなければなりません
notifyBLECharacteristicValueChange() {
wx.notifyBLECharacteristicValueChange({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
characteristicId: this.data. characteristicId,
state: true // 是否启用 notify (四个字段全部必填)
})
}
(8)特性値の変化をリスニング
--- wx.onBLECharacteristicValueChangeイベントリスナ機能、低消費電力のBluetoothデバイスの値に変更します
⚠️最初のデバイス(通知)にプッシュ通知notifyBLECharacteristicValueChangeを受信するためのインタフェースを有効にする必要があります
// 先监听一下,保证第一时间获取数据
onBLECharacteristicValueChange() {
wx.onBLECharacteristicValueChange( characteristic => {
console.log('onBLECharacteristicValueChange从目标蓝牙设备监听到的数据值:', characteristic)
this.setData({
value: this.ab2hex(abcharacteristic.value) // (10) 转为 16 进制
})
})
}
(9)読み出しデータ
バイナリデータ値は、Bluetooth低エネルギーデバイスの固有値--- wx.readBLECharacteristicValueを読みます
⚠️ターゲットBluetoothデバイスが特性値のみが呼び出され、単独でreadBLECharacteristicValueを使用することができます読んで実際の特性値を取得していないサポートしなければならない、唯一の状態の特性値を取得するために返すことができる、の値が正常に取得し、実際の値はWXの使用を必要とするかどうか、です読んで.onBLECharacteristicValueChange()コールバックは、このAPIは)(wx.onBLECharacteristicValueChangeで得られる固有値
readBLECharacteristicValue() {
wx.readBLECharacteristicValue({
deviceId: this.data.deviceId,
serviceId: this.data.serviceId,
characteristicId: this.data.charecteristicId,
success: res => {
console.log('readBLECharacteristicValue读取特征值成功:', res)
},
fail: err => {
console.log('readBLECharacteristicValue读取特征值失败:', err)
}
})
}
(10)進数に
公式文書は、16進数にする方法を説明しArrayBuffer
// ArrayBuffer转16进制字符串示例
ab2hex(buffer) {
let hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}