UNPKG

17 kBTypeScriptView Raw
1import Taro from '../../index'
2
3declare module '../../index' {
4 namespace onBLEPeripheralConnectionStateChanged {
5 /** 当前外围设备被连接或断开连接事件的回调函数 */
6 type Callback = (
7 result: CallbackResult,
8 ) => void
9
10 interface CallbackResult {
11 /** 蓝牙设备 id */
12 deviceId: string
13 /** server 的 UUID */
14 serverId: string
15 /** 连接目前状态 */
16 connected: string
17 }
18 }
19
20 namespace createBLEPeripheralServer {
21 interface Option {
22 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
23 complete?: (res: TaroGeneral.BluetoothError) => void
24 /** 接口调用失败的回调函数 */
25 fail?: (res: TaroGeneral.BluetoothError) => void
26 /** 接口调用成功的回调函数 */
27 success?: (res: SuccessCallbackResult) => void
28 }
29 interface SuccessCallbackResult extends TaroGeneral.BluetoothError {
30 /** 外围设备的服务端 */
31 server: BLEPeripheralServer
32 }
33 }
34
35 /** 外围设备的服务端
36 * @supported weapp
37 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.html
38 */
39 interface BLEPeripheralServer {
40 /** 添加服务
41 * @supported weapp
42 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.addService.html
43 */
44 addService(option: BLEPeripheralServer.addService.Option): Promise<TaroGeneral.BluetoothError>
45 /** 关闭当前服务端
46 * @supported weapp
47 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.close.html
48 */
49 close(option: BLEPeripheralServer.close.Option): Promise<TaroGeneral.BluetoothError>
50 /** 取消监听已连接的设备请求读当前外围设备的特征值事件
51 * @supported weapp
52 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.offCharacteristicReadRequest.html
53 */
54 offCharacteristicReadRequest(
55 /** 已连接的设备请求读当前外围设备的特征值事件的回调函数 */
56 callback: BLEPeripheralServer.onCharacteristicReadRequest.Callback,
57 ): void
58 /** 取消监听特征订阅事件
59 * @supported weapp
60 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.offCharacteristicSubscribed.html
61 */
62 offCharacteristicSubscribed(
63 /** 特征订阅事件的回调函数 */
64 callback: BLEPeripheralServer.onCharacteristicSubscribed.Callback,
65 ): void
66 /** 取消监听取消特征订阅事件
67 * @supported weapp
68 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.offCharacteristicUnsubscribed.html
69 */
70 offCharacteristicUnsubscribed(
71 /** 取消特征订阅事件的回调函数 */
72 callback: BLEPeripheralServer.onCharacteristicUnsubscribed.Callback,
73 ): void
74 /** 取消监听已连接的设备请求写当前外围设备的特征值事件
75 * @supported weapp
76 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.offCharacteristicWriteRequest.html
77 */
78 offCharacteristicWriteRequest(
79 /** 已连接的设备请求写当前外围设备的特征值事件的回调函数 */
80 callback: BLEPeripheralServer.onCharacteristicWriteRequest.Callback,
81 ): void
82 /** 监听已连接的设备请求读当前外围设备的特征值事件
83 *
84 * 收到该消息后需要立刻调用 [writeCharacteristicValue](/docs/apis/device/bluetooth-peripheral/BLEPeripheralServer#writecharacteristicvalue) 写回数据,否则主机不会收到响应。
85 * @supported weapp
86 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.onCharacteristicReadRequest.html
87 */
88 onCharacteristicReadRequest(
89 /** 已连接的设备请求读当前外围设备的特征值事件的回调函数 */
90 callback: BLEPeripheralServer.onCharacteristicReadRequest.Callback,
91 ): void
92 /** 监听特征订阅事件,仅 iOS 支持
93 * @supported weapp
94 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.onCharacteristicSubscribed.html
95 */
96 onCharacteristicSubscribed(
97 /** 特征订阅事件的回调函数 */
98 callback: BLEPeripheralServer.onCharacteristicSubscribed.Callback,
99 ): void
100 /** 监听取消特征订阅事件,仅 iOS 支持
101 * @supported weapp
102 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.onCharacteristicUnsubscribed.html
103 */
104 onCharacteristicUnsubscribed(
105 /** 取消特征订阅事件的回调函数 */
106 callback: BLEPeripheralServer.onCharacteristicUnsubscribed.Callback,
107 ): void
108 /** 监听已连接的设备请求写当前外围设备的特征值事件
109 * @supported weapp
110 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.onCharacteristicWriteRequest.html
111 */
112 onCharacteristicWriteRequest(
113 /** 已连接的设备请求写当前外围设备的特征值事件的回调函数 */
114 callback: BLEPeripheralServer.onCharacteristicWriteRequest.Callback,
115 ): void
116 /** 移除服务
117 * @supported weapp
118 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.removeService.html
119 */
120 removeService(option: BLEPeripheralServer.removeService.Option): Promise<TaroGeneral.BluetoothError>
121 /** 开始广播本地创建的外围设备
122 * @supported weapp
123 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.startAdvertising.html
124 */
125 startAdvertising(option: BLEPeripheralServer.startAdvertising.Option): Promise<TaroGeneral.BluetoothError>
126 /** 停止广播
127 * @supported weapp
128 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.stopAdvertising.html
129 */
130 stopAdvertising(option: BLEPeripheralServer.stopAdvertising.Option): Promise<TaroGeneral.BluetoothError>
131 /** 往指定特征写入二进制数据值,并通知已连接的主机,从机的特征值已发生变化,该接口会处理是走回包还是走订阅
132 * @supported weapp
133 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/BLEPeripheralServer.writeCharacteristicValue.html
134 */
135 writeCharacteristicValue(option: BLEPeripheralServer.writeCharacteristicValue.Option): Promise<TaroGeneral.BluetoothError>
136 }
137
138 namespace BLEPeripheralServer {
139 namespace addService {
140 interface Option {
141 /** 描述 service 的 Object */
142 service: service
143 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
144 complete?: (res: TaroGeneral.BluetoothError) => void
145 /** 接口调用失败的回调函数 */
146 fail?: (res: TaroGeneral.BluetoothError) => void
147 /** 接口调用成功的回调函数 */
148 success?: (res: TaroGeneral.BluetoothError) => void
149 }
150 interface service {
151 /** 蓝牙服务的 UUID */
152 uuid: string
153 /** characteristics 列表 */
154 characteristics: characteristic[]
155 }
156 interface characteristic {
157 /** characteristic 的 UUID */
158 uuid: string
159 /** 特征支持的操作 */
160 properties: properties
161 /** 特征权限 */
162 permission: characteristicPermission
163 /** 特征对应的二进制值 */
164 value: ArrayBuffer
165 /** 描述符数据 */
166 descriptors: descriptor[]
167 }
168 /** 特征支持的操作 */
169 interface properties {
170 /** 写
171 * @default false
172 */
173 write?: boolean
174 /** 无回复写
175 * @default false
176 */
177 writeNoResponse?: boolean
178 /** 读
179 * @default false
180 */
181 read?: boolean
182 /** 订阅
183 * @default false
184 */
185 notify?: boolean
186 /** 回包
187 * @default false
188 */
189 indicate?: boolean
190 }
191 /** 特征权限 */
192 interface characteristicPermission {
193 /** 可读
194 * @default false
195 */
196 readable?: boolean
197 /** 可写
198 * @default false
199 */
200 writeable?: boolean
201 /** 加密读请求
202 * @default false
203 */
204 readEncryptionRequired?: boolean
205 /** 加密写请求
206 * @default false
207 */
208 writeEncryptionRequired?: boolean
209 }
210 /** 描述符数据 */
211 interface descriptor {
212 /** Descriptor 的 UUID */
213 uuid: string
214 /** 描述符的权限 */
215 permission: descriptorPermission
216 /** 描述符数据 */
217 value: ArrayBuffer
218 }
219 /** 描述符的权限 */
220 interface descriptorPermission {
221 /** 写
222 * @default false
223 */
224 write?: boolean
225 /** 读
226 * @default false
227 */
228 read?: boolean
229 }
230 }
231 namespace close {
232 interface Option {
233 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
234 complete?: (res: TaroGeneral.BluetoothError) => void
235 /** 接口调用失败的回调函数 */
236 fail?: (res: TaroGeneral.BluetoothError) => void
237 /** 接口调用成功的回调函数 */
238 success?: (res: TaroGeneral.BluetoothError) => void
239 }
240 }
241 namespace onCharacteristicReadRequest {
242 /** 已连接的设备请求读当前外围设备的特征值事件的回调函数 */
243 type Callback = (
244 result: CallbackResult,
245 ) => void
246
247 interface CallbackResult {
248 /** 蓝牙特征对应服务的 UUID */
249 serviceId: string
250 /** 蓝牙特征的 UUID */
251 characteristicId: string
252 /** 唯一标识码,调用 [writeCharacteristicValue](/docs/apis/device/bluetooth-peripheral/BLEPeripheralServer#writecharacteristicvalue) 时使用 */
253 callbackId: number
254 }
255 }
256 namespace onCharacteristicSubscribed {
257 /** 特征订阅事件的回调函数 */
258 type Callback = (
259 result: CallbackResult,
260 ) => void
261
262 interface CallbackResult {
263 /** 蓝牙特征对应服务的 UUID */
264 serviceId: string
265 /** 蓝牙特征的 UUID */
266 characteristicId: string
267 }
268 }
269 namespace onCharacteristicUnsubscribed {
270 /** 取消特征订阅事件的回调函数 */
271 type Callback = (
272 result: CallbackResult,
273 ) => void
274
275 interface CallbackResult {
276 /** 蓝牙特征对应服务的 UUID */
277 serviceId: string
278 /** 蓝牙特征的 UUID */
279 characteristicId: string
280 }
281 }
282 namespace onCharacteristicWriteRequest {
283 /** 已连接的设备请求写当前外围设备的特征值事件的回调函数 */
284 type Callback = (
285 result: CallbackResult,
286 ) => void
287
288 interface CallbackResult {
289 /** 蓝牙特征对应服务的 UUID */
290 serviceId: string
291 /** 蓝牙特征的 UUID */
292 characteristicId: string
293 /** 唯一标识码,调用 [writeCharacteristicValue](/docs/apis/device/bluetooth-peripheral/BLEPeripheralServer#writecharacteristicvalue) 时使用 */
294 callbackId: number
295 /** 请求写入特征的二进制数据值 */
296 value: ArrayBuffer
297 }
298 }
299 namespace removeService {
300 interface Option {
301 /** service 的 UUID */
302 serviceId: string
303 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
304 complete?: (res: TaroGeneral.BluetoothError) => void
305 /** 接口调用失败的回调函数 */
306 fail?: (res: TaroGeneral.BluetoothError) => void
307 /** 接口调用成功的回调函数 */
308 success?: (res: TaroGeneral.BluetoothError) => void
309 }
310 }
311 namespace startAdvertising {
312 interface Option {
313 /** 广播自定义参数 */
314 advertiseRequest: advertiseRequest
315 /** 广播功率
316 * @default "medium"
317 */
318 powerLevel?: keyof PowerLevel
319 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
320 complete?: (res: TaroGeneral.BluetoothError) => void
321 /** 接口调用失败的回调函数 */
322 fail?: (res: TaroGeneral.BluetoothError) => void
323 /** 接口调用成功的回调函数 */
324 success?: (res: TaroGeneral.BluetoothError) => void
325 }
326 /** 广播自定义参数 */
327 interface advertiseRequest {
328 /** 当前设备是否可连接
329 * @default true
330 */
331 connectable?: boolean
332 /** 广播中 deviceName 字段,默认为空
333 * @default ""
334 */
335 deviceName?: string
336 /** 要广播的服务 UUID 列表。使用 16/32 位 UUID 时请参考注意事项。 */
337 serviceUuids?: string[]
338 /** 广播的制造商信息。仅安卓支持,iOS 因系统限制无法定制。 */
339 manufacturerData?: manufacturerData[]
340 /** 以 beacon 设备形式广播的参数。 */
341 beacon?: beacon
342 }
343 /** 广播的制造商信息。仅安卓支持,iOS 因系统限制无法定制。 */
344 interface manufacturerData {
345 /** 制造商ID,0x 开头的十六进制 */
346 manufacturerId: string
347 /** 制造商信息 */
348 manufacturerSpecificData?: ArrayBuffer
349 }
350 /** 以 beacon 设备形式广播的参数。 */
351 interface beacon {
352 /** Beacon 设备广播的 UUID */
353 uuid: string
354 /** Beacon 设备的主 ID */
355 major: number
356 /** Beacon 设备的次 ID */
357 minor: number
358 /** 用于判断距离设备 1 米时 RSSI 大小的参考值 */
359 measuredPower?: number
360 }
361 /** 广播功率合法值 */
362 interface PowerLevel {
363 /** 功率低 */
364 low
365 /** 功率适中 */
366 medium
367 /** 功率高 */
368 high
369 }
370 }
371 namespace stopAdvertising {
372 interface Option {
373 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
374 complete?: (res: TaroGeneral.BluetoothError) => void
375 /** 接口调用失败的回调函数 */
376 fail?: (res: TaroGeneral.BluetoothError) => void
377 /** 接口调用成功的回调函数 */
378 success?: (res: TaroGeneral.BluetoothError) => void
379 }
380 }
381 namespace writeCharacteristicValue {
382 interface Option {
383 /** 蓝牙特征对应服务的 UUID */
384 serviceId: string
385 /** 蓝牙特征的 UUID */
386 characteristicId: string
387 /** characteristic 对应的二进制值 */
388 value: ArrayBuffer
389 /** 是否需要通知主机 value 已更新 */
390 needNotify: boolean
391 /** 可选,处理回包时使用 */
392 callbackId?: number
393 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
394 complete?: (res: TaroGeneral.BluetoothError) => void
395 /** 接口调用失败的回调函数 */
396 fail?: (res: TaroGeneral.BluetoothError) => void
397 /** 接口调用成功的回调函数 */
398 success?: (res: TaroGeneral.BluetoothError) => void
399 }
400 }
401 }
402
403 interface TaroStatic {
404 /** 监听当前外围设备被连接或断开连接事件
405 * @supported weapp
406 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/wx.onBLEPeripheralConnectionStateChanged.html
407 */
408 onBLEPeripheralConnectionStateChanged(
409 /** 监听当前外围设备被连接或断开连接事件 */
410 callback: onBLEPeripheralConnectionStateChanged.Callback,
411 ): void
412
413 /** 取消监听当前外围设备被连接或断开连接事件
414 * @supported weapp
415 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/wx.offBLEPeripheralConnectionStateChanged.html
416 */
417 offBLEPeripheralConnectionStateChanged(
418 /** 当前外围设备被连接或断开连接事件的回调函数 */
419 callback: onBLEPeripheralConnectionStateChanged.Callback,
420 ): void
421
422 /** 建立本地作为蓝牙低功耗外围设备的服务端,可创建多个
423 * @supported weapp
424 * @see https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-peripheral/wx.createBLEPeripheralServer.html
425 */
426 createBLEPeripheralServer(
427 option: createBLEPeripheralServer.Option,
428 ): Promise<createBLEPeripheralServer.SuccessCallbackResult>
429 }
430}