IOS 蓝牙连接

最近做一个硬件设备,写IOS相应的数据连接/分析代码时;发现一个问题,如果是开机,每次都能连接上。连接断开后,发现再也扫描不到了。通过第三方工具LightBlue,发现信号是-127。

此时进入设置查看蓝牙设备,发现该设备处于连接状态。所以发现不了该设备。

此问题解决方案:搜索前,先通过

retrieveConnectedPeripheralsWithServices

搜索已连接设备,是否存在当前应用需要的设备。存在时,不进行扫描,直接连接设备。

不存在时,进行扫描。

搜索示例代码:

复制代码
-(int) Search_RetrieveConnPeri
{
    NSArray *uuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:DEVICE_INFO_SERVICE],[CBUUID UUIDWithString:DEVICE_BATTER_SERVICE], [CBUUID UUIDWithString:kConnectedServiceUUID1], [CBUUID UUIDWithString:OAT695_SERVICE],nil];
    
    NSArray* allConnected=[centralManager retrieveConnectedPeripheralsWithServices:uuidArray];
    if(allConnected.count>0)
    {
        for(int i=0;i<allConnected.count;i++)
        {
            CBPeripheral *per = allConnected[i];
            NSLog(@"retrie conn names: %@",per.name);
            if([per.name hasPrefix:KNAME_DEVICE]){
                [self Connect_Peri:per];
                return 1;
            }
        }
        
        //CBPeripheral *per = allConnected[0];
        
    }
    return 0;
}
相关推荐
归辞...1 小时前
「iOS」————设计架构
ios·架构
i紸定i4 小时前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
YungFan18 小时前
iOS26适配指南之UIButton
ios·swift
共享家95271 天前
linux-数据链路层
linux·网络·macos
红橙Darren1 天前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.1 天前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
CZIDC1 天前
MacOS字体看起来比在 Windows 上更好?
macos
Cosmoshhhyyy2 天前
linux远程部署dify和mac本地部署dify
linux·运维·macos
行星0082 天前
mac 通过homebrew 安装和使用nvm
macos·npm·node.js
麦兜*2 天前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift