更新记录
1.0.3(2024-07-17)
优化
1.0.2(2024-06-28)
优化
1.0.0(2024-05-14)
初版
查看更多平台兼容性
uni-app
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | - | - | - | - | 4.4 | - | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - | - | - |
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.0 | - | - | - |
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>