蓝牙连接报错该如何处理?

使用如下demo:

let clientNumber = -1; 
let clientSocket = (code: BusinessError, number: number) => { 
  if (code) { 
    console.error('sppListen error, code is ' + code.message); 
    return; 
  } else { 
    console.log('bluetooth serverSocket Number: ' + number); 
    // 获取的clientNumber用作客户端后续读/写操作socket的id。 
    clientNumber = number; 
  } 
} 
let sppOption:socket.SppOptions = {uuid: '00001101-0000-1000-8000-00805F9Bxxxx', secure: false, type: 0}; 
try { 
  socket.sppConnect('xx:xx:xx:xx:xx:xx', sppOption, clientSocket); 
} catch (err) { 
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 
}

报错2900099

HarmonyOS
2024-09-09 09:45:48
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

socket.sppConnect(‘xx:xx:xx:xx:xx:xx’, sppOption, clientSocket);这个接口里的MAC地址不可以直接使用设备真实的物理地址,需要先使用connection.getPairedDevices()接口获取当前蓝牙设备生成的随机地址,然后使用这个地址进行连接,主要需要先向用户申请蓝牙权限

参考demo:

import { BusinessError } from '@kit.BasicServicesKit' 
import socket from '@ohos.bluetooth.socket'; 
import connection from '@ohos.bluetooth.connection'; 
import { abilityAccessCtrl, common, PermissionRequestResult, Permissions } from '@kit.AbilityKit'; 
 
@Component 
@Entry 
export struct DSRefresh { 
 
  permissions: Array<Permissions> = ['ohos.permission.ACCESS_BLUETOOTH']; 
 
  reqPermissionsFromUser(permissions: Array<Permissions>): void { 
    let context: Context = getContext(this) as common.UIAbilityContext; 
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 
    // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗 
    atManager.requestPermissionsFromUser(context, permissions).then((data: PermissionRequestResult) => { 
      let grantStatus: Array<number> = data.authResults; 
      let length: number = grantStatus.length; 
      for (let i = 0; i < length; i++) { 
        if (grantStatus[i] === 0) { 
          // 用户授权,可以继续访问目标操作 
        } else { 
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限 
          return; 
        } 
      } 
      // 授权成功 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); 
    }) 
  } 
 
  aboutToAppear(): void { 
    this.reqPermissionsFromUser(this.permissions) 
  } 
  build() { 
    Column() { 
      Button() 
        .onClick(() => { 
          let devices: Array<string> = [] 
          try { 
            devices = connection.getPairedDevices(); 
          } catch (err) { 
            console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 
          } 
          if(devices.length ==0){ 
            return 
          } 
          socket.sppConnect(devices[0], { 
            uuid: '00001101-0000-1000-8000-008xxxB34FB', 
            secure: true, 
            type: 0 
          }, (code, socketID) => { 
            if (code != null) { 
              console.error('sppConnect error, code = ' + (code as BusinessError).code); 
              return; 
            } 
            console.info('sppConnect success, socketId = ' + socketID); 
          }) 
        }) 
        .height('100%') 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-09-09 15:42:22
相关问题
ArkTS语法校验报错如何处理啊?
931浏览 • 1回复 待解决
Socket连接报错,错误码88
1801浏览 • 1回复 待解决
注册监听关机事件,如何处理
792浏览 • 1回复 待解决
监听音频打断事件,如何处理
1001浏览 • 1回复 待解决
权限的申请与校验,如何处理啊?
972浏览 • 1回复 待解决
编译报错,信息不足如何处理啊?
327浏览 • 1回复 待解决
润和轻量系统mqtt连接报错误码2
1959浏览 • 0回复 待解决
项目启动报错怎么处理啊?
323浏览 • 1回复 待解决
怎么解决mysql远程连接报10038的错误?
2209浏览 • 1回复 待解决
HarmonyOS 大整数如何处理
492浏览 • 1回复 待解决
HarmonyOS蓝牙连接后获取GATT服务失败
735浏览 • 1回复 待解决