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;
}
相关推荐
songgeb11 小时前
Concurrency in Swift学习笔记-初识
ios·swift
DY009J12 小时前
C++基础学习——文件操作详解
c++·学习·cocoa·visual studio code
Everbrilliant8913 小时前
macOs上交叉编译ffmpeg及安装ffmpeg工具
macos·ffmpeg·ffmpeg交叉编译·ffmpeg工具安装
mobsmobs16 小时前
Flutter开发环境搭建与工具链
android·flutter·ios·android studio·xcode
SY.ZHOU19 小时前
iOS上使用WebRTC推拉流的案例
ios·音视频·cocoa·webrtc
杂雾无尘20 小时前
2025 年了,是否该全面拥抱 Swift 6?
ios·swift·客户端
sunshine__sun1 天前
快速启用 JMeter(macOS Automator 创建 JMeter 脚本)
jmeter·macos
liliangcsdn1 天前
mac llama_index agent算术式子计算示例
人工智能·python·macos·llama
WIN赢1 天前
【MAC电脑系统变量管理】
macos
Digitally1 天前
设置后轻松将 iPhone 转移到 iPhone
ios·iphone