This commit is contained in:
Xu Chang 2022-07-21 07:07:10 +08:00
parent 82e878ef18
commit 7f5afa22bb
6 changed files with 225 additions and 0 deletions

3
lib/types/Wx.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
/// <reference types="wechat-miniprogram" />
export declare type PromisefyOption<T extends Record<string, any>> = Omit<T, 'success' | 'fail' | 'complete'>;
export declare type PromisefyResult<T extends WechatMiniprogram.AsyncMethodOptionLike> = Promise<Parameters<Exclude<T['success'], undefined>>[0]>;

2
lib/types/Wx.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

1
lib/utils/bluetooth.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from './bluetooth.mp';

18
lib/utils/bluetooth.js Normal file
View File

@ -0,0 +1,18 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./bluetooth.mp"), exports);
console.log('不应该走到这里');

48
lib/utils/bluetooth.mp.d.ts vendored Normal file
View File

@ -0,0 +1,48 @@
/// <reference types="wechat-miniprogram" />
import { PromisefyOption } from '../types/Wx';
/**
* Bluetooth不用action重渲染
*/
export declare class Bluetooth {
private serverDict;
constructor();
startScanDevice(option: PromisefyOption<WechatMiniprogram.StartBluetoothDevicesDiscoveryOption>): Promise<WechatMiniprogram.BluetoothError>;
stopScanDevice(): Promise<WechatMiniprogram.BluetoothError>;
openAdapter(option: PromisefyOption<WechatMiniprogram.OpenBluetoothAdapterOption>): Promise<WechatMiniprogram.BluetoothError>;
closeAdapter(): Promise<WechatMiniprogram.BluetoothError>;
onDeviceFound(callback: WechatMiniprogram.OnBluetoothDeviceFoundCallback): void;
onAdapterStateChanged(callback: WechatMiniprogram.OnBluetoothAdapterStateChangeCallback): void;
offDeviceFound(callback: WechatMiniprogram.OnBluetoothDeviceFoundCallback): void;
offAdapterStateChaned(callback: WechatMiniprogram.OnBluetoothAdapterStateChangeCallback): void;
getConnectedDevices(option: PromisefyOption<WechatMiniprogram.GetConnectedBluetoothDevicesOption>): Promise<WechatMiniprogram.GetConnectedBluetoothDevicesSuccessCallbackResult>;
getDevices(): Promise<WechatMiniprogram.GetBluetoothDevicesSuccessCallbackResult>;
getAdapterState(): Promise<WechatMiniprogram.GetBluetoothAdapterStateSuccessCallbackResult>;
writeBLECharacteristicValue(option: PromisefyOption<WechatMiniprogram.WriteBLECharacteristicValueOption>): Promise<WechatMiniprogram.BluetoothError>;
readBLECharacteristicValue(option: PromisefyOption<WechatMiniprogram.ReadBLECharacteristicValueOption>): Promise<WechatMiniprogram.BluetoothError>;
onBLEConnectionStateChange(callback: WechatMiniprogram.OnBLEConnectionStateChangeCallback): void;
onBLECharacteristicValueChange(callback: WechatMiniprogram.OnBLECharacteristicValueChangeCallback): void;
offBLEConnectionStateChange(callback: WechatMiniprogram.OnBLEConnectionStateChangeCallback): void;
offBLECharacteristicValueChange(callback: WechatMiniprogram.OnBLECharacteristicValueChangeCallback): void;
notifyBLECharacteristicValueChange(option: PromisefyOption<WechatMiniprogram.NotifyBLECharacteristicValueChangeOption>): void;
getBLEDeviceServices(option: PromisefyOption<WechatMiniprogram.GetBLEDeviceServicesOption>): Promise<WechatMiniprogram.GetBLEDeviceServicesSuccessCallbackResult>;
getBLEDeviceCharacteristics(option: PromisefyOption<WechatMiniprogram.GetBLEDeviceCharacteristicsOption>): Promise<WechatMiniprogram.GetBLEDeviceCharacteristicsSuccessCallbackResult>;
createBLEConnection(option: PromisefyOption<WechatMiniprogram.CreateBLEConnectionOption>): Promise<WechatMiniprogram.BluetoothError>;
closeBLEConnection(option: PromisefyOption<WechatMiniprogram.CloseBLEConnectionOption>): Promise<WechatMiniprogram.BluetoothError>;
onPeripheralConnectionStateChanged(callback: WechatMiniprogram.OnBLEPeripheralConnectionStateChangedCallback): void;
offPeripheralConnectionStateChanged(callback: WechatMiniprogram.OnBLEPeripheralConnectionStateChangedCallback): void;
createPeripheralServer(): Promise<string>;
closePeripheralServer(id: string): void;
addPeripheralService(id: string, option: PromisefyOption<WechatMiniprogram.AddServiceOption>): void;
removePeripheralService(id: string, option: PromisefyOption<WechatMiniprogram.RemoveServiceOption>): void;
startPeripheralAdvertising(id: string, option: PromisefyOption<WechatMiniprogram.StartAdvertisingObject>): void;
stopPeripheralAdvertising(id: string): void;
writePeripheralCharacteristicValue(id: string, option: PromisefyOption<WechatMiniprogram.WriteCharacteristicValueObject>): void;
onPeripheralCharacteristicReadRequest(id: string, callback: WechatMiniprogram.OnCharacteristicReadRequestCallback): void;
offPeripheralCharacteristicReadRequest(id: string, callback: WechatMiniprogram.OnCharacteristicReadRequestCallback): void;
onPeripheralCharacteristicWriteRequest(id: string, callback: WechatMiniprogram.OnCharacteristicWriteRequestCallback): void;
offPeripheralCharacteristicWriteRequest(id: string, callback: WechatMiniprogram.OnCharacteristicWriteRequestCallback): void;
onPeripheralCharacteristicSubscribed(id: string, callback: WechatMiniprogram.OnCharacteristicSubscribedCallback): void;
offPeripheralCharacteristicSubscribed(id: string, callback: WechatMiniprogram.OnCharacteristicSubscribedCallback): void;
onPeripheralCharacteristicUnsubscribed(id: string, callback: WechatMiniprogram.OnCharacteristicUnsubscribedCallback): void;
offPeripheralCharacteristicUnsubscribed(id: string, callback: WechatMiniprogram.OnCharacteristicUnsubscribedCallback): void;
}

153
lib/utils/bluetooth.mp.js Normal file
View File

@ -0,0 +1,153 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bluetooth = void 0;
const lodash_1 = require("lodash");
/**
* Bluetooth不用action重渲染用监听回调的方式处理数据变化吧
*/
class Bluetooth {
serverDict;
constructor() {
this.serverDict = {};
}
startScanDevice(option) {
return wx.startBluetoothDevicesDiscovery(option);
}
stopScanDevice() {
return wx.stopBluetoothDevicesDiscovery({});
}
openAdapter(option) {
return wx.openBluetoothAdapter(option);
}
closeAdapter() {
return wx.closeBluetoothAdapter({});
}
onDeviceFound(callback) {
wx.onBluetoothDeviceFound(callback);
}
onAdapterStateChanged(callback) {
wx.onBluetoothAdapterStateChange(callback);
}
offDeviceFound(callback) {
wx.offBluetoothDeviceFound(callback);
}
offAdapterStateChaned(callback) {
wx.offBluetoothAdapterStateChange(callback);
}
getConnectedDevices(option) {
return wx.getConnectedBluetoothDevices(option);
}
getDevices() {
return wx.getBluetoothDevices({});
}
getAdapterState() {
return wx.getBluetoothAdapterState({});
}
// ble
writeBLECharacteristicValue(option) {
return wx.writeBLECharacteristicValue(option);
}
readBLECharacteristicValue(option) {
return wx.readBLECharacteristicValue(option);
}
onBLEConnectionStateChange(callback) {
wx.onBLEConnectionStateChange(callback);
}
onBLECharacteristicValueChange(callback) {
wx.onBLECharacteristicValueChange(callback);
}
offBLEConnectionStateChange(callback) {
wx.offBLEConnectionStateChange(callback);
}
offBLECharacteristicValueChange(callback) {
wx.offBLECharacteristicValueChange(callback);
}
notifyBLECharacteristicValueChange(option) {
wx.notifyBLECharacteristicValueChange(option);
}
getBLEDeviceServices(option) {
return wx.getBLEDeviceServices(option);
}
getBLEDeviceCharacteristics(option) {
return wx.getBLEDeviceCharacteristics(option);
}
createBLEConnection(option) {
return wx.createBLEConnection(option);
}
closeBLEConnection(option) {
return wx.closeBLEConnection(option);
}
// peripheral
onPeripheralConnectionStateChanged(callback) {
wx.onBLEPeripheralConnectionStateChanged(callback);
}
offPeripheralConnectionStateChanged(callback) {
wx.offBLEPeripheralConnectionStateChanged(callback);
}
async createPeripheralServer() {
const server = await wx.createBLEPeripheralServer({});
const id = await generateNewId();
(0, lodash_1.assign)(this.serverDict, {
[id]: server,
});
return id;
}
closePeripheralServer(id) {
const server = this.serverDict[id];
server.close();
(0, lodash_1.unset)(this.serverDict, id);
}
addPeripheralService(id, option) {
const server = this.serverDict[id];
server.addService(option);
}
removePeripheralService(id, option) {
const server = this.serverDict[id];
return server.removeService(option);
}
startPeripheralAdvertising(id, option) {
const server = this.serverDict[id];
return server.startAdvertising(option);
}
stopPeripheralAdvertising(id) {
const server = this.serverDict[id];
return server.stopAdvertising();
}
writePeripheralCharacteristicValue(id, option) {
const server = this.serverDict[id];
return server.writeCharacteristicValue(option);
}
onPeripheralCharacteristicReadRequest(id, callback) {
const server = this.serverDict[id];
server.onCharacteristicReadRequest(callback);
}
offPeripheralCharacteristicReadRequest(id, callback) {
const server = this.serverDict[id];
server.offCharacteristicReadRequest(callback);
}
onPeripheralCharacteristicWriteRequest(id, callback) {
const server = this.serverDict[id];
server.onCharacteristicWriteRequest(callback);
}
offPeripheralCharacteristicWriteRequest(id, callback) {
const server = this.serverDict[id];
server.offCharacteristicWriteRequest(callback);
}
onPeripheralCharacteristicSubscribed(id, callback) {
const server = this.serverDict[id];
server.onCharacteristicSubscribed(callback);
}
offPeripheralCharacteristicSubscribed(id, callback) {
const server = this.serverDict[id];
server.offCharacteristicSubscribed(callback);
}
onPeripheralCharacteristicUnsubscribed(id, callback) {
const server = this.serverDict[id];
server.onCharacteristicUnsubscribed(callback);
}
offPeripheralCharacteristicUnsubscribed(id, callback) {
const server = this.serverDict[id];
server.offCharacteristicUnsubscribed(callback);
}
}
exports.Bluetooth = Bluetooth;