From 09db687ba5e0489e7aeda5ffd822eead9100aebc Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 23 Jun 2021 20:56:25 +0800 Subject: [PATCH] feat(App): bluetooth-BLE --- packages/uni-api/src/index.ts | 1 + .../uni-api/src/protocols/device/bluetooth.ts | 172 +++++++++++++++++ packages/uni-app-plus/src/helpers/plus.ts | 41 ++++- .../src/service/api/device/bluetooth.ts | 173 ++++++++++++++++++ .../uni-app-plus/src/service/api/index.ts | 1 + 5 files changed, 383 insertions(+), 5 deletions(-) create mode 100644 packages/uni-api/src/protocols/device/bluetooth.ts create mode 100644 packages/uni-app-plus/src/service/api/device/bluetooth.ts diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 31f8f06b7..c90424660 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -27,6 +27,7 @@ export * from './protocols/device/setClipboardData' export * from './protocols/device/accelerometer' export * from './protocols/device/compass' export * from './protocols/device/vibrate' +export * from './protocols/device/bluetooth' export * from './protocols/storage/storage' diff --git a/packages/uni-api/src/protocols/device/bluetooth.ts b/packages/uni-api/src/protocols/device/bluetooth.ts new file mode 100644 index 000000000..74556d84e --- /dev/null +++ b/packages/uni-api/src/protocols/device/bluetooth.ts @@ -0,0 +1,172 @@ +export const API_ON_BLUETOOTH_DEVICE_FOUND = 'onBluetoothDeviceFound' +export type API_TYPE_ON_BLUETOOTH_DEVICE_FOUND = + typeof uni.onBluetoothDeviceFound + +export const API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE = + 'onBluetoothAdapterStateChange' +export type API_TYPE_ON_BLUETOOTH_ADAPTER_STATE_CHANGE = + typeof uni.onBluetoothAdapterStateChange +export const API_ON_BLE_CONNECTION_STATE_CHANGE = 'onBLEConnectionStateChange' +export type API_TYPE_ON_BLE_CONNECTION_STATE_CHANGE = + typeof uni.onBLEConnectionStateChange +export const API_ON_BLE_CHARACTERISTIC_VALUE_CHANGE = + 'onBLECharacteristicValueChange' +export type API_TYPE_ON_BLE_CHARACTERISTIC_VALUE_CHANGE = + typeof uni.onBLECharacteristicValueChange + +export const API_START_BLUETOOTH_DEVICES_DISCOVERY = + 'startBluetoothDevicesDiscovery' +export type API_TYPE_START_BLUETOOTH_DEVICES_DISCOVERY = + typeof uni.startBluetoothDevicesDiscovery +export const StartBluetoothDevicesDiscoveryProtocol: ApiProtocol = + { + services: Array, + allowDuplicatesKey: Boolean, + interval: Number, + } + +export const API_GET_CONNECTED_BLUETOOTH_DEVICES = + 'getConnectedBluetoothDevices' +export type API_TYPE_GET_CONNECTED_BLUETOOTH_DEVICES = + typeof uni.getConnectedBluetoothDevices +export const GetConnectedBluetoothDevicesProtocol: ApiProtocol = + { + services: { + type: Array, + required: true, + }, + } + +export const API_CREATE_BLE_CONNECTION = 'createBLEConnection' +export type API_TYPE_CREATE_BLE_CONNECTION = typeof uni.createBLEConnection +export const CreateBLEConnectionProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + } + +export const API_CLOSE_BLE_CONNECTION = 'closeBLEConnection' +export type API_TYPE_CLOSE_BLE_CONNECTION = typeof uni.closeBLEConnection +export const CloseBLEConnectionProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + } + +export const API_GET_BLE_DEVICE_SERVICES = 'getBLEDeviceServices' +export type API_TYPE_GET_BLE_DEVICE_SERVICES = typeof uni.getBLEDeviceServices +export const GetBLEDeviceServicesProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + } + +export const API_GET_BLE_DEVICE_CHARACTERISTICS = 'getBLEDeviceCharacteristics' +export type API_TYPE_GET_BLE_DEVICE_CHARACTERISTICS = + typeof uni.getBLEDeviceCharacteristics +export const GetBLEDeviceCharacteristicsProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + serviceId: { + type: String, + required: true, + }, + } + +export const API_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE = + 'notifyBLECharacteristicValueChange' +export type API_TYPE_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE = + typeof uni.notifyBLECharacteristicValueChange +export const NotifyBLECharacteristicValueChangeProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + serviceId: { + type: String, + required: true, + }, + characteristicId: { + type: String, + required: true, + }, + state: { + type: Boolean, + required: true, + }, + } + +export const API_READ_BLE_CHARACTERISTIC_VALUE = 'readBLECharacteristicValue' +export type API_TYPE_READ_BLE_CHARACTERISTIC_VALUE = + typeof uni.readBLECharacteristicValue +export const ReadBLECharacteristicValueProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + serviceId: { + type: String, + required: true, + }, + characteristicId: { + type: String, + required: true, + }, + } + +export const API_WRITE_BLE_CHARACTERISTIC_VALUE = 'writeBLECharacteristicValue' +export type API_TYPE_WRITE_BLE_CHARACTERISTIC_VALUE = + typeof uni.writeBLECharacteristicValue +export const WriteBLECharacteristicValueProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + serviceId: { + type: String, + required: true, + }, + characteristicId: { + type: String, + required: true, + }, + value: { + type: Array, + required: true, + }, + } + +export const API_SET_BLE_MTU = 'setBLEMTU' +export type API_TYPE_SET_BLE_MTU = typeof uni.setBLEMTU +export const SetBLEMTUProtocol: ApiProtocol = { + deviceId: { + type: String, + required: true, + }, + mtu: { + type: Number, + required: true, + }, +} + +export const API_GET_BLE_DEVICE_RSSI = 'getBLEDeviceRSSI' +export type API_TYPE_GET_BLE_DEVICE_RSSI = typeof uni.getBLEDeviceRSSI +export const GetBLEDeviceRSSIProtocol: ApiProtocol = + { + deviceId: { + type: String, + required: true, + }, + } diff --git a/packages/uni-app-plus/src/helpers/plus.ts b/packages/uni-app-plus/src/helpers/plus.ts index 4b8ea2044..f1db1750a 100644 --- a/packages/uni-app-plus/src/helpers/plus.ts +++ b/packages/uni-app-plus/src/helpers/plus.ts @@ -5,9 +5,11 @@ interface PlusResult extends Record { } type PlusError = PlusResult +type Resolve = (res: any) => void +type Reject = (errMsg: string, errRes?: any) => void export function warpPlusSuccessCallback( - resolve: (res: any) => void, + resolve: Resolve, after?: (res: any) => any ) { return function successCallback(data: PlusResult) { @@ -20,10 +22,7 @@ export function warpPlusSuccessCallback( } } -export function warpPlusErrorCallback( - reject: (errMsg: string, errRes?: any) => void, - errMsg?: string -) { +export function warpPlusErrorCallback(reject: Reject, errMsg?: string) { return function errorCallback(error?: PlusError) { error = error || {} // 一键登录errorCallback新增 appid、metadata、uid 参数返回 @@ -32,3 +31,35 @@ export function warpPlusErrorCallback( reject(errMsg, extend({ code: 0 }, error)) } } + +export function warpPlusEvent(plusObject: () => any, event: string) { + return function () { + const object = plusObject() + object(function (data?: Record) { + if (data) { + delete data.code + delete data.message + } + UniServiceJSBridge.invokeOnCallback(event, data) + }) + } +} + +export function warpPlusMethod( + plusObject: () => any, + before?: (options: any) => any, + after?: (res: any) => any +) { + return function ( + options: any, + { resolve, reject }: { resolve: Resolve; reject: Reject } + ) { + const object = plusObject() + object( + extend({}, typeof before === 'function' ? before(options) : options, { + success: warpPlusSuccessCallback(resolve, after), + fail: warpPlusErrorCallback(reject), + }) + ) + } +} diff --git a/packages/uni-app-plus/src/service/api/device/bluetooth.ts b/packages/uni-app-plus/src/service/api/device/bluetooth.ts new file mode 100644 index 000000000..0566b8446 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/device/bluetooth.ts @@ -0,0 +1,173 @@ +import { + defineOnApi, + API_ON_BLUETOOTH_DEVICE_FOUND, + API_TYPE_ON_BLUETOOTH_DEVICE_FOUND, + API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE, + API_TYPE_ON_BLUETOOTH_ADAPTER_STATE_CHANGE, + API_ON_BLE_CONNECTION_STATE_CHANGE, + API_TYPE_ON_BLE_CONNECTION_STATE_CHANGE, + API_ON_BLE_CHARACTERISTIC_VALUE_CHANGE, + API_TYPE_ON_BLE_CHARACTERISTIC_VALUE_CHANGE, + defineAsyncApi, + API_START_BLUETOOTH_DEVICES_DISCOVERY, + API_TYPE_START_BLUETOOTH_DEVICES_DISCOVERY, + StartBluetoothDevicesDiscoveryProtocol, + API_GET_CONNECTED_BLUETOOTH_DEVICES, + API_TYPE_GET_CONNECTED_BLUETOOTH_DEVICES, + GetConnectedBluetoothDevicesProtocol, + API_CREATE_BLE_CONNECTION, + API_TYPE_CREATE_BLE_CONNECTION, + CreateBLEConnectionProtocol, + API_CLOSE_BLE_CONNECTION, + API_TYPE_CLOSE_BLE_CONNECTION, + CloseBLEConnectionProtocol, + API_GET_BLE_DEVICE_SERVICES, + API_TYPE_GET_BLE_DEVICE_SERVICES, + GetBLEDeviceServicesProtocol, + API_GET_BLE_DEVICE_CHARACTERISTICS, + API_TYPE_GET_BLE_DEVICE_CHARACTERISTICS, + GetBLEDeviceCharacteristicsProtocol, + API_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE, + API_TYPE_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE, + NotifyBLECharacteristicValueChangeProtocol, + API_READ_BLE_CHARACTERISTIC_VALUE, + API_TYPE_READ_BLE_CHARACTERISTIC_VALUE, + ReadBLECharacteristicValueProtocol, + API_WRITE_BLE_CHARACTERISTIC_VALUE, + API_TYPE_WRITE_BLE_CHARACTERISTIC_VALUE, + WriteBLECharacteristicValueProtocol, + API_SET_BLE_MTU, + API_TYPE_SET_BLE_MTU, + SetBLEMTUProtocol, + API_GET_BLE_DEVICE_RSSI, + API_TYPE_GET_BLE_DEVICE_RSSI, + GetBLEDeviceRSSIProtocol, +} from '@dcloudio/uni-api' +import { warpPlusEvent, warpPlusMethod } from '../../../helpers/plus' + +export const onBluetoothDeviceFound = + defineOnApi( + API_ON_BLUETOOTH_DEVICE_FOUND, + warpPlusEvent( + () => plus.bluetooth.onBluetoothDeviceFound, + API_ON_BLUETOOTH_DEVICE_FOUND + ) + ) +export const onBluetoothAdapterStateChange = + defineOnApi( + API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE, + warpPlusEvent( + () => plus.bluetooth.onBluetoothAdapterStateChange, + API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE + ) + ) +export const onBLEConnectionStateChange = + defineOnApi( + API_ON_BLE_CONNECTION_STATE_CHANGE, + warpPlusEvent( + () => plus.bluetooth.onBLEConnectionStateChange, + API_ON_BLE_CONNECTION_STATE_CHANGE + ) + ) +export const onBLECharacteristicValueChange = + defineOnApi( + API_ON_BLE_CHARACTERISTIC_VALUE_CHANGE, + warpPlusEvent( + () => plus.bluetooth.onBLECharacteristicValueChange, + API_ON_BLE_CHARACTERISTIC_VALUE_CHANGE + ) + ) + +export const openBluetoothAdapter = defineAsyncApi< + typeof uni.openBluetoothAdapter +>( + 'openBluetoothAdapter', + warpPlusMethod(() => plus.bluetooth.openBluetoothAdapter) +) +export const closeBluetoothAdapter = defineAsyncApi< + typeof uni.closeBluetoothAdapter +>( + 'closeBluetoothAdapter', + warpPlusMethod(() => plus.bluetooth.closeBluetoothAdapter) +) +export const getBluetoothAdapterState = defineAsyncApi< + typeof uni.getBluetoothAdapterState +>( + 'getBluetoothAdapterState', + warpPlusMethod(() => plus.bluetooth.getBluetoothAdapterState) +) +export const startBluetoothDevicesDiscovery = + defineAsyncApi( + API_START_BLUETOOTH_DEVICES_DISCOVERY, + warpPlusMethod(() => plus.bluetooth.startBluetoothDevicesDiscovery), + StartBluetoothDevicesDiscoveryProtocol + ) +export const stopBluetoothDevicesDiscovery = defineAsyncApi< + typeof uni.stopBluetoothDevicesDiscovery +>( + 'stopBluetoothDevicesDiscovery', + warpPlusMethod(() => plus.bluetooth.stopBluetoothDevicesDiscovery) +) +export const getBluetoothDevices = defineAsyncApi< + typeof uni.getBluetoothDevices +>( + 'getBluetoothDevices', + warpPlusMethod(() => plus.bluetooth.getBluetoothDevices) +) +export const getConnectedBluetoothDevices = + defineAsyncApi( + API_GET_CONNECTED_BLUETOOTH_DEVICES, + warpPlusMethod(() => plus.bluetooth.getConnectedBluetoothDevices), + GetConnectedBluetoothDevicesProtocol + ) +export const createBLEConnection = + defineAsyncApi( + API_CREATE_BLE_CONNECTION, + warpPlusMethod(() => plus.bluetooth.createBLEConnection), + CreateBLEConnectionProtocol + ) +export const closeBLEConnection = defineAsyncApi( + API_CLOSE_BLE_CONNECTION, + warpPlusMethod(() => plus.bluetooth.closeBLEConnection), + CloseBLEConnectionProtocol +) +export const getBLEDeviceServices = + defineAsyncApi( + API_GET_BLE_DEVICE_SERVICES, + warpPlusMethod(() => plus.bluetooth.getBLEDeviceServices), + GetBLEDeviceServicesProtocol + ) +export const getBLEDeviceCharacteristics = + defineAsyncApi( + API_GET_BLE_DEVICE_CHARACTERISTICS, + warpPlusMethod(() => plus.bluetooth.getBLEDeviceCharacteristics), + GetBLEDeviceCharacteristicsProtocol + ) +export const notifyBLECharacteristicValueChange = + defineAsyncApi( + API_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE, + warpPlusMethod(() => plus.bluetooth.notifyBLECharacteristicValueChange), + NotifyBLECharacteristicValueChangeProtocol + ) +export const readBLECharacteristicValue = + defineAsyncApi( + API_READ_BLE_CHARACTERISTIC_VALUE, + warpPlusMethod(() => plus.bluetooth.readBLECharacteristicValue), + ReadBLECharacteristicValueProtocol + ) +export const writeBLECharacteristicValue = + defineAsyncApi( + API_WRITE_BLE_CHARACTERISTIC_VALUE, + warpPlusMethod(() => plus.bluetooth.writeBLECharacteristicValue), + WriteBLECharacteristicValueProtocol + ) +export const setBLEMTU = defineAsyncApi( + API_SET_BLE_MTU, + warpPlusMethod(() => plus.bluetooth.setBLEMTU), + SetBLEMTUProtocol +) +export const getBLEDeviceRSSI = defineAsyncApi( + API_GET_BLE_DEVICE_RSSI, + warpPlusMethod(() => plus.bluetooth.getBLEDeviceRSSI), + GetBLEDeviceRSSIProtocol +) diff --git a/packages/uni-app-plus/src/service/api/index.ts b/packages/uni-app-plus/src/service/api/index.ts index cf229b441..3e3162dbf 100644 --- a/packages/uni-app-plus/src/service/api/index.ts +++ b/packages/uni-app-plus/src/service/api/index.ts @@ -4,6 +4,7 @@ export * from './file/getFileInfo' export * from './device/compass' export * from './device/vibrate' export * from './device/accelerometer' +export * from './device/bluetooth' export * from './media/getImageInfo' export * from './media/getVideoInfo' -- GitLab