BLE Peripheral设备的名字显示错误
2015-12-30 14:30
666 查看
在IOS BLE开发中碰到一个搜索到的设备名称(pheripheral.name)和设备实际名称不相符的问题。
最后发现这个问题的原因是,在iPhone中,被连接过的BLE设备名称都会被缓存。如果你在连接过一次某设备后,更改了该设备的名字。那么在你的iphone BLE cache数据清除之前,你无论多少次搜索这个设备,都会是旧的名字。比如你有个设备的初始名字是F5000,你用iphone连接过一次后,如果把设备名字再改成FA01Y,那么当你再次用iphone搜索设备时,你永远搜到的都是F5000而不是该设备的新名称FA01Y。
目前有两个解决办法:
1. 清除你iphone的BLE cache。
2. 另一个更简单的办法是,通过任何一款BLE测试软件连接该设备。或者通过设备的旧名称来连接一次设备。当设备被你再次连接过后,本地cache中该设备的BLE名字会更新。之后你搜到该设备就会显示它的新名字了。
以下是我碰到的问题实例。
先看代码:
@property (strong, nonatomic) CBCentralManager *manager;
@property NSString *deviceName;
self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.deviceName = @"FA01Y";
[self.manager scanForPeripheralsWithServices:nil options:nil];
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id>
*)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"Found Device: %@", peripheral.name);
if ([peripheral.name isEqualToString:self.deviceName])
{
[self.manager stopScan];
self.selectedDevice = peripheral;
self.selectedDevice.delegate = self;
[self.manager connectPeripheral:peripheral options:nil];
}
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"Connected device: %@", peripheral.name);
[self.selectedDevice discoverServices:nil];
}
我有一个BLE设备作为Peripheral,它的初始名字是:F5000。之后我把名字设置为了:FA01Y。我想要通过以上代码,来通过这个FA01Y新名字来连接设备,但是失败了。
原因是:程序没有搜索到FA01Y设备,而是只搜索到一个名字叫做“F5000”的设备:
2015-12-30 14:28:18.345 PainKARE1.0[10017:1824607] Found Device: F5000
2015-12-30 14:28:18.425 PainKARE1.0[10017:1824607] Found device: F5000
而F5000恰巧是这个Peripheral设备的初始名字。(也就是更改为FA01Y之前的名字)。
最后发现这个问题的原因是,在iPhone中,被连接过的BLE设备名称都会被缓存。如果你在连接过一次某设备后,更改了该设备的名字。那么在你的iphone BLE cache数据清除之前,你无论多少次搜索这个设备,都会是旧的名字。比如你有个设备的初始名字是F5000,你用iphone连接过一次后,如果把设备名字再改成FA01Y,那么当你再次用iphone搜索设备时,你永远搜到的都是F5000而不是该设备的新名称FA01Y。
目前有两个解决办法:
1. 清除你iphone的BLE cache。
2. 另一个更简单的办法是,通过任何一款BLE测试软件连接该设备。或者通过设备的旧名称来连接一次设备。当设备被你再次连接过后,本地cache中该设备的BLE名字会更新。之后你搜到该设备就会显示它的新名字了。
以下是我碰到的问题实例。
先看代码:
@property (strong, nonatomic) CBCentralManager *manager;
@property NSString *deviceName;
self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.deviceName = @"FA01Y";
[self.manager scanForPeripheralsWithServices:nil options:nil];
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id>
*)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"Found Device: %@", peripheral.name);
if ([peripheral.name isEqualToString:self.deviceName])
{
[self.manager stopScan];
self.selectedDevice = peripheral;
self.selectedDevice.delegate = self;
[self.manager connectPeripheral:peripheral options:nil];
}
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"Connected device: %@", peripheral.name);
[self.selectedDevice discoverServices:nil];
}
我有一个BLE设备作为Peripheral,它的初始名字是:F5000。之后我把名字设置为了:FA01Y。我想要通过以上代码,来通过这个FA01Y新名字来连接设备,但是失败了。
原因是:程序没有搜索到FA01Y设备,而是只搜索到一个名字叫做“F5000”的设备:
2015-12-30 14:28:18.345 PainKARE1.0[10017:1824607] Found Device: F5000
2015-12-30 14:28:18.425 PainKARE1.0[10017:1824607] Found device: F5000
而F5000恰巧是这个Peripheral设备的初始名字。(也就是更改为FA01Y之前的名字)。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- IIS 错误 Server Application Error 详细解决方法
- Lua教程(六):编译执行与错误
- 完美解决Persits.Jpeg.1 错误 ''800a0001''
- "在试着设置任务帐户信息时出现错误,指定的错误是:0x80070005:拒绝访问
- Mysql 本地计算机无法启动 mysql 服务 错误 1067:进程意外终止。
- 修复ShopNC使用QQ 互联时提示100010 错误
- mysql错误处理之ERROR 1665 (HY000)
- 使用Raygun对Node.js应用进行错误处理的方法
- C#新手常犯的错误汇总
- php自定义错误处理用法实例
- Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)
- 使用PHP的错误处理
- msxml3.dll 错误 '800c0005'解决方案