102 lines
3.3 KiB
Plaintext
102 lines
3.3 KiB
Plaintext
import Build from 'android.os.Build'
|
|
import { BluetoothNative } from 'uts.sdk.modules.sp2Bluetooth'
|
|
import { type BluetoothDeviceItem, type BluetoothDeviceFoundCallback, type BluetoothErrorCallback, type BluetoothSuccessCallback, type BluetoothDataCallback, type BluetoothDebugCallback } from '../interface.uts'
|
|
|
|
function getBluetoothPermissions(): string[] {
|
|
const permissions: string[] = []
|
|
if (Build.VERSION.SDK_INT >= 31) {
|
|
permissions.push('android.permission.BLUETOOTH_SCAN')
|
|
permissions.push('android.permission.BLUETOOTH_CONNECT')
|
|
} else if (Build.VERSION.SDK_INT >= 23) {
|
|
permissions.push('android.permission.ACCESS_FINE_LOCATION')
|
|
}
|
|
return permissions
|
|
}
|
|
|
|
function ensureBluetoothPermissions(onSuccess: BluetoothSuccessCallback, onError: BluetoothErrorCallback): void {
|
|
const activity = UTSAndroid.getUniActivity()
|
|
if (activity == null) {
|
|
onError('无法获取当前 Activity')
|
|
return
|
|
}
|
|
|
|
const permissions = getBluetoothPermissions()
|
|
if (permissions.length === 0) {
|
|
onSuccess()
|
|
return
|
|
}
|
|
|
|
UTSAndroid.requestSystemPermission(activity, permissions, (allRight: boolean, grantedList: string[]) => {
|
|
if (allRight || grantedList.length === permissions.length) {
|
|
onSuccess()
|
|
return
|
|
}
|
|
onError('蓝牙权限未完全授予')
|
|
}, (doNotAskAgain: boolean, grantedList: string[]) => {
|
|
if (doNotAskAgain) {
|
|
onError('蓝牙权限被永久拒绝,请到系统设置中开启')
|
|
return
|
|
}
|
|
if (grantedList.length > 0) {
|
|
onError('蓝牙权限未完全授予')
|
|
return
|
|
}
|
|
onError('蓝牙权限申请失败')
|
|
})
|
|
}
|
|
|
|
export function startBluetoothScan(onDeviceFound: BluetoothDeviceFoundCallback, onError: BluetoothErrorCallback): void {
|
|
ensureBluetoothPermissions(() => {
|
|
BluetoothNative.startBluetoothScan((deviceId: string, name: string) => {
|
|
const device: BluetoothDeviceItem = {
|
|
deviceId: deviceId,
|
|
name: name
|
|
}
|
|
onDeviceFound(device)
|
|
}, onError)
|
|
}, onError)
|
|
}
|
|
|
|
export function stopBluetoothScan(): void {
|
|
BluetoothNative.stopBluetoothScan()
|
|
}
|
|
|
|
export function connectBluetoothDevice(deviceId: string, onSuccess: BluetoothSuccessCallback, onError: BluetoothErrorCallback): void {
|
|
stopBluetoothScan()
|
|
disconnectBluetoothDevice()
|
|
ensureBluetoothPermissions(() => {
|
|
// 对齐 ecBLE.js 的“先清理旧连接,再建立新连接”思路,避免复用脏的 GATT 状态。
|
|
setTimeout(() => {
|
|
BluetoothNative.connectBluetoothDevice(deviceId, onSuccess, onError)
|
|
}, 300)
|
|
}, onError)
|
|
}
|
|
|
|
export function disconnectBluetoothDevice(): void {
|
|
BluetoothNative.disconnectBluetoothDevice()
|
|
}
|
|
|
|
export function onBluetoothDataReceived(callback: BluetoothDataCallback): void {
|
|
BluetoothNative.setOnDataReceivedCallback(callback)
|
|
}
|
|
|
|
export function onBluetoothDebugMessage(callback: BluetoothDebugCallback): void {
|
|
BluetoothNative.setOnDebugMessageCallback(callback)
|
|
}
|
|
|
|
export function getBluetoothDebugSnapshot(): string {
|
|
return BluetoothNative.getDebugSnapshot()
|
|
}
|
|
|
|
export function readBluetoothDataOnce(): void {
|
|
BluetoothNative.readBluetoothDataOnce()
|
|
}
|
|
|
|
export function refreshBluetoothNotification(): void {
|
|
BluetoothNative.refreshBluetoothNotification()
|
|
}
|
|
|
|
export function writeBluetoothData(data: string): void {
|
|
BluetoothNative.writeBluetoothData(data)
|
|
}
|