iOS BLE Scan問題!!

fuminoaya 2017-12-11 10:56:00
有關iOS BLE Scan問題想請教:

1. 請參考附檔(iOS_Foreground_Scan_time.png, iOS_Background_scan_time.png), BLE裝置會改變廣播字串 F_H06LOCKERFFFF ---> F_H127CEC79FEA805LLLL(持續6秒) ---> F_H06LOCKERFFFF, 手機App必須在6秒時間內掃描到BLE裝置, App在前景時沒有問題, 連續掃描間隔約1-2秒, 但App在背景時, 掃描間隔不固定, 有時掃描間隔甚至大於15秒以上, 而錯過廣播字串(F_H127CEC79FEA805LLLL), 以至於該動作而不動作, 請問這是iOS BLE在背景下運作的限制嗎? 有沒有方法可以改善(和前景掃描一樣間隔)?
<<註>>: 在AppDelegate使用Timer(1秒或2秒)定時掃描, 得到和(iOS_Background_scan_time.png)一樣的結果

2. 請參考附檔(iOS_BLE_Scan_1.jpg, iOS_BLE_Scan_2.jpg, iOS_BLE_Scan_3.jpg), 當藍牙裝置改變廣播字串時(F_H06LOCKERFFFF ---> F_H127CEC79FEA805LLLL), Android手機可以掃瞄到改變的字串(F_H127CEC79FEA805LLLL), 但是iOS仍然掃瞄到舊字串(F_H06LOCKERFFFF), 我要怎麼處理才能讓iOS掃瞄到新字串?

謝謝您的回覆!!


// ---------------------------------------------------------------------------------------------------------------
AppDelegate.m
//
// AppDelegate.m
// Locker_BLE_F1
//

#import "AppDelegate.h"

@interface AppDelegate ()
{
MainViewController *MainVC;
}
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog (@"AppDelegate(didFinishLaunchingWithOptions)");
MainVC = (MainViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog (@"applicationDidEnterBackground");
[MainVC ExecuteBackgroundAction];
[self backgroundHandler];
}

- (void)backgroundHandler {
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"backgroundHandler1");
dispatch_async(dispatch_get_main_queue(),^{
if( bgTask != UIBackgroundTaskInvalid) {
bgTask = UIBackgroundTaskInvalid;
}
});
}];

// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while ([Common getOpMode] == OP_MODE_BACKGROUND) {
[MainVC bleScanForBackground];
[NSThread sleepForTimeInterval:1.0f];
}
});
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog (@"applicationWillEnterForeground");
[MainVC ExecuteForegroundAction];
}
@end

MainViewController.m
//
// MainViewController.m
// Locker_BLE_F1
//
#import "MainViewController.h"

@interface MainViewController () {
}
@end

@implementation MainViewController

#pragma mark - View Controller
- (void)viewDidLoad {
[super viewDidLoad];
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

#pragma mark - BlueTooth (BLE, Central)
// method 1
- (void) centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBManagerStatePoweredOn) // CBCentralManagerStatePoweredOn)
{
NSLog (@"Please open the Bluetooth from System Setting");
return;
}
[self setBLEStatusWithLog:SCANNING];
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];
}

// method 2
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
[self.centralManager stopScan];
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];
NSString *strAdvertisementDataLocalName, *strDeviceName ;

strAdvertisementDataLocalName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if (strAdvertisementDataLocalName == nil) { return; }

strDeviceName = [strAdvertisementDataLocalName substringWithRange:NSMakeRange(0, REMOTE_DEVICE_NAME.length)];
if (![strDeviceName isEqualToString:REMOTE_DEVICE_NAME]) return;

NSLog(@"strAdvertisementDataLocalName= %@", strAdvertisementDataLocalName);

self.connectPeripheral = peripheral;
self.connectPeripheral.delegate = self;
...
}

#pragma mark - General Function
- (void) bleScanForBackground {
[self updateLog:@"bleScanForBackground " LogType:FLOW_LOG];
NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];
}

- (void) ExecuteBackgroundAction {
[self changeOpMode:OP_MODE_BACKGROUND];
if (self.isConnected == true) {
[self bleDisconnect:self.centralManager Peripheral:self.connectPeripheral];
}
}

- (void) ExecuteForegroundAction {
}
@end

...全文
593 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuzesong 2017-12-25
  • 打赏
  • 举报
回复
问题一的话 去年我在做蓝牙这块的时候,苹果是不支持后台扫描的,今年貌似也没有听过有更新这块.
fuminoaya 2017-12-25
  • 打赏
  • 举报
回复
引用 1 楼 wuzesong 的回复:
问题一的话 去年我在做蓝牙这块的时候,苹果是不支持后台扫描的,今年貌似也没有听过有更新这块.
謝謝回覆!! 我目前可以在後台掃描, 只是掃描間隔不固定, 有時掃描間隔會超過我的專案規格範圍以外.
When Apple released the first beta of i OS 7 at WWDC 2013 , I scoured through the API changes looking for anything out of the ordinary. That’s when I noticed the unex- pected addition of new API s pertaining to barcodes. At that time, barcodes were little more than visual noise to me, a necessity of mod- ern commerce but of no value to me as a consumer or app developer. Why would Apple devote precious resources to implementing functionality for that? Several third-party libraries for barcode scanning were available at that time. Some were commercial offerings too expensive for casual use. Others were open source projects requiring a great deal of work to understand or implement in your own apps. By adding support for barcodes within the i OS SDK , Apple made the technology acces- sible to all developers equally. Apple was sending a message: barcodes are important to us. This paradigm shift inspired me to learn all I could about barcode technologies. I began to research the barcode types supported by iOS and their capabilities and limi- tations, and all the new related i OS API s. A mere month after WWDC 2013 , I was contacted by Manning. They’d found me via my blog (cocoanetics.com) and inquired if I would be interested in writing an i OS book for them. They could not have contacted me at a more perfect moment! I was willing, able, and inspired to write, for more than a year, the book you’re now holding. June 2014 marked the 40-year anniversary of the first barcode being scanned at a point of sale. In other words, barcodes are a nearly ubiquitous, mature technology. The UPC you’ll find on all products sold in your supermarket was just the beginning. PREFACE xii Just look at any Apple product box. You’ll find several barcodes on the stickers offer- ing additional information such as the device’s serial number. Since October 2013, all iPhones can be used to scan barcodes. Together with always-on mobile internet and built-in device sensors, this enables a new breed of product-centric apps that weren’t feasible before. After reading this book, you’ll be able to build the exciting new apps that are bringing together the digital and physical worlds.

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧