更新记录
1.0.3(2024-07-17)
优化
1.0.2(2024-06-28)
优化
1.0.0(2024-05-14)
初版
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:支持,iOS:不确定,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
jk-ble
开发文档
<template>
<view class="content">
<button @click="onGrant">onGrant</button>
<button @click="onInit">onInit</button>
<view style="display: flex;flex-direction: row;">
<button @click="onScan">Scan</button>
<button @click="onStopScan">StopScan</button>
</view>
<view style="display: flex;flex-direction: row;">
<button @click="connect">connect</button>
<button @click="disconnect">disconnect</button>
<button @click="connState">connState</button>
</view>
<button @click="notify">notify</button>
<button @click="write">write</button>
</view>
</template>
<script>
import * as JkBLE from "@/uni_modules/jk-ble";
export default {
data() {
return {
characteristic: null,
deviceId: '----'
}
},
onLoad() {
JkBLE.onBluetoothDeviceFound((device : BluetoothDeviceFoundResult) => {
});
JkBLE.onBLEConnectionStateChange((result : BLEConnectionStateChange) => {
})
JkBLE.onBLECharacteristicValueChange((result : BLECharacteristicValueChange) => {
let datas = result?.value
if (datas != null) {
let list : string[] = []
datas.forEach(item => {
list.push(number2Int(item).toString(16))
})
console.log("recv:" + list)
}
})
},
methods: {
onGrant() {
let permission = JkBLE.blueToothPermissions()
if (permission != null) {
UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permission, (_ : boolean, p : string[]) => {
}, (_ : boolean, p : string[]) => {
})
}
},
onInit() {
JkBLE.debugEnable()
JkBLE.initBLE()
JkBLE.openBluetoothAdapter()
},
onScan() {
JkBLE.startBluetoothDevicesDiscovery({
success: () => {
},
} as BluetoothDevicesDiscoveryOptions)
},
onStopScan() {
JkBLE.stopBluetoothDevicesDiscovery()
},
connect() {
let deviceId = this.deviceId
JkBLE.createBLEConnection(deviceId)
},
disconnect() {
let deviceId = this.deviceId
JkBLE.closeBLEConnection(deviceId)
},
connState() {
let deviceId = this.deviceId
console.log(("connected:" + JkBLE.isBLEConnected(deviceId)))
},
notify() {
let deviceId = this.deviceId
let options : NotifyBLECharacteristicValue = {
deviceId,
autoFind: true,
success(characteristic) {
}
}
JkBLE.notifyBLECharacteristicValueChange(options)
},
write() {
let deviceId = this.deviceId
let datas = [1,2,3]
if (this.characteristic != null) {
JkBLE.writeBLECharacteristicValue(deviceId, this.characteristic, datas)
}
}
}
}
</script>
<style>
.content {
display: flex;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-bottom: 50rpx;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>