@@ -240,6 +240,75 @@ export default class PeripheralData {
240
240
} ) ;
241
241
}
242
242
243
+ async write ( serviceUUID : string , characteristicUUID : string , data :Uint8Array ,
244
+ maxByteSize : number , writeType :number ) : Promise < void > {
245
+ if ( ! this . isConnected || this . device === null ) {
246
+ return Promise . reject ( 'Device is not connected' )
247
+ }
248
+ let result :Array < ble . GattService > = await this . device . getServices ( ) ;
249
+ let gattService :ble . GattService = result . find ( ( gattService ) => gattService . serviceUuid === serviceUUID )
250
+ if ( gattService === null || gattService === undefined ) {
251
+ return Promise . reject ( `serviceUUID + ${ serviceUUID } + not found.` )
252
+ }
253
+ let characteristic : ble . BLECharacteristic = this . findWritableCharacteristic ( gattService , characteristicUUID , writeType ) ;
254
+ if ( characteristic === null || characteristic === undefined ) {
255
+ return Promise . reject ( `Characteristic + ${ characteristicUUID } + not found.` )
256
+ }
257
+ if ( data . length <= maxByteSize ) {
258
+ if ( ! this . doWrite ( characteristic , new Uint8Array ( data ) ) ) {
259
+ return Promise . reject ( "Write failed" )
260
+ } else {
261
+ return Promise . resolve ( ) ;
262
+ }
263
+ } else {
264
+ let dataLength = data . length ;
265
+ let count = 0 ;
266
+ let firstMessage :Uint8Array = null ;
267
+ const splittedMessage = [ ] ;
268
+ while ( count < dataLength && ( dataLength - count ) > maxByteSize ) {
269
+ if ( count == 0 ) {
270
+ //处理第一个信息
271
+ firstMessage = new Uint8Array ( data ) . subarray ( count , count + maxByteSize ) ;
272
+ } else {
273
+ //将其分割数据添加到列表
274
+ splittedMessage . push ( new Uint8Array ( data ) . subarray ( count , count + maxByteSize ) )
275
+ }
276
+ count += maxByteSize ;
277
+ }
278
+ if ( count < dataLength ) {
279
+ splittedMessage . push ( new Uint8Array ( data ) . subarray ( count , data . length ) )
280
+ }
281
+ //harmonyOS 默认writeType == ble.GattWriteType.WRITE
282
+ let writeError :boolean = false ;
283
+ if ( ! this . doWrite ( characteristic , firstMessage ) ) {
284
+ writeError = true ;
285
+ return Promise . reject ( 'Write failed' )
286
+ }
287
+ if ( ! writeError ) {
288
+ for ( let i = 0 ; i < splittedMessage . length ; i ++ ) {
289
+ let message = splittedMessage [ i ] ;
290
+ if ( ! this . doWrite ( characteristic , message ) ) {
291
+ writeError = true ;
292
+ return Promise . reject ( 'Write failed' )
293
+ }
294
+ }
295
+ }
296
+ }
297
+ return Promise . resolve ( ) ;
298
+ }
299
+
300
+ doWrite ( characteristic : ble . BLECharacteristic , data :Uint8Array ) :boolean {
301
+ try {
302
+ characteristic . characteristicValue = data . buffer ;
303
+ this . device . writeCharacteristicValue ( characteristic , ble . GattWriteType . WRITE )
304
+ return true ;
305
+ } catch ( error ) {
306
+ console . info ( 'Write failed :' + JSON . stringify ( error ) )
307
+ return false ;
308
+ }
309
+ }
310
+
311
+
243
312
findWritableCharacteristic ( gattService :ble . GattService , characteristicUUID : string , writeType :number ) :ble . BLECharacteristic {
244
313
let writeProperty = ble . GattWriteType . WRITE ;
245
314
if ( writeType == ble . GattWriteType . WRITE_NO_RESPONSE ) {
0 commit comments