提交 09db687b 编写于 作者: Q qiang

feat(App): bluetooth-BLE

上级 d9bd4cd2
......@@ -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'
......
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<API_TYPE_START_BLUETOOTH_DEVICES_DISCOVERY> =
{
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<API_TYPE_GET_CONNECTED_BLUETOOTH_DEVICES> =
{
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<API_TYPE_CREATE_BLE_CONNECTION> =
{
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<API_TYPE_CLOSE_BLE_CONNECTION> =
{
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<API_TYPE_GET_BLE_DEVICE_SERVICES> =
{
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<API_TYPE_GET_BLE_DEVICE_CHARACTERISTICS> =
{
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<API_TYPE_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE> =
{
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<API_TYPE_READ_BLE_CHARACTERISTIC_VALUE> =
{
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<API_TYPE_WRITE_BLE_CHARACTERISTIC_VALUE> =
{
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<API_TYPE_SET_BLE_MTU> = {
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<API_TYPE_GET_BLE_DEVICE_RSSI> =
{
deviceId: {
type: String,
required: true,
},
}
......@@ -5,9 +5,11 @@ interface PlusResult extends Record<string, any> {
}
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<string, any>) {
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),
})
)
}
}
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_TYPE_ON_BLUETOOTH_DEVICE_FOUND>(
API_ON_BLUETOOTH_DEVICE_FOUND,
warpPlusEvent(
() => plus.bluetooth.onBluetoothDeviceFound,
API_ON_BLUETOOTH_DEVICE_FOUND
)
)
export const onBluetoothAdapterStateChange =
defineOnApi<API_TYPE_ON_BLUETOOTH_ADAPTER_STATE_CHANGE>(
API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE,
warpPlusEvent(
() => plus.bluetooth.onBluetoothAdapterStateChange,
API_ON_BLUETOOTH_ADAPTER_STATE_CHANGE
)
)
export const onBLEConnectionStateChange =
defineOnApi<API_TYPE_ON_BLE_CONNECTION_STATE_CHANGE>(
API_ON_BLE_CONNECTION_STATE_CHANGE,
warpPlusEvent(
() => plus.bluetooth.onBLEConnectionStateChange,
API_ON_BLE_CONNECTION_STATE_CHANGE
)
)
export const onBLECharacteristicValueChange =
defineOnApi<API_TYPE_ON_BLE_CHARACTERISTIC_VALUE_CHANGE>(
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_TYPE_START_BLUETOOTH_DEVICES_DISCOVERY>(
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_TYPE_GET_CONNECTED_BLUETOOTH_DEVICES>(
API_GET_CONNECTED_BLUETOOTH_DEVICES,
warpPlusMethod(() => plus.bluetooth.getConnectedBluetoothDevices),
GetConnectedBluetoothDevicesProtocol
)
export const createBLEConnection =
defineAsyncApi<API_TYPE_CREATE_BLE_CONNECTION>(
API_CREATE_BLE_CONNECTION,
warpPlusMethod(() => plus.bluetooth.createBLEConnection),
CreateBLEConnectionProtocol
)
export const closeBLEConnection = defineAsyncApi<API_TYPE_CLOSE_BLE_CONNECTION>(
API_CLOSE_BLE_CONNECTION,
warpPlusMethod(() => plus.bluetooth.closeBLEConnection),
CloseBLEConnectionProtocol
)
export const getBLEDeviceServices =
defineAsyncApi<API_TYPE_GET_BLE_DEVICE_SERVICES>(
API_GET_BLE_DEVICE_SERVICES,
warpPlusMethod(() => plus.bluetooth.getBLEDeviceServices),
GetBLEDeviceServicesProtocol
)
export const getBLEDeviceCharacteristics =
defineAsyncApi<API_TYPE_GET_BLE_DEVICE_CHARACTERISTICS>(
API_GET_BLE_DEVICE_CHARACTERISTICS,
warpPlusMethod(() => plus.bluetooth.getBLEDeviceCharacteristics),
GetBLEDeviceCharacteristicsProtocol
)
export const notifyBLECharacteristicValueChange =
defineAsyncApi<API_TYPE_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE>(
API_NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE,
warpPlusMethod(() => plus.bluetooth.notifyBLECharacteristicValueChange),
NotifyBLECharacteristicValueChangeProtocol
)
export const readBLECharacteristicValue =
defineAsyncApi<API_TYPE_READ_BLE_CHARACTERISTIC_VALUE>(
API_READ_BLE_CHARACTERISTIC_VALUE,
warpPlusMethod(() => plus.bluetooth.readBLECharacteristicValue),
ReadBLECharacteristicValueProtocol
)
export const writeBLECharacteristicValue =
defineAsyncApi<API_TYPE_WRITE_BLE_CHARACTERISTIC_VALUE>(
API_WRITE_BLE_CHARACTERISTIC_VALUE,
warpPlusMethod(() => plus.bluetooth.writeBLECharacteristicValue),
WriteBLECharacteristicValueProtocol
)
export const setBLEMTU = defineAsyncApi<API_TYPE_SET_BLE_MTU>(
API_SET_BLE_MTU,
warpPlusMethod(() => plus.bluetooth.setBLEMTU),
SetBLEMTUProtocol
)
export const getBLEDeviceRSSI = defineAsyncApi<API_TYPE_GET_BLE_DEVICE_RSSI>(
API_GET_BLE_DEVICE_RSSI,
warpPlusMethod(() => plus.bluetooth.getBLEDeviceRSSI),
GetBLEDeviceRSSIProtocol
)
......@@ -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'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册