zoukankan      html  css  js  c++  java
  • iOS开发- 蓝牙后台接收数据(BLE4.0)

    近期在做一个蓝牙相关的项目, 须要在应用进入后台, 或者手机属于锁屏状态的情况下, 仍然保持蓝牙连接, 而且能正常接收数据。

    本来以后会非常麻烦, 可是学习了下..发现就2步而已。简单的不能再简单了。


    好了。

    以下是详细实现办法。


    1.在xxx-info.plist文件里, 新建一行  Required background modes  , 增加以下两项。

    App shares data using CoreBluetooth  和  App communicates using CoreBluetooth

    如图所看到的:



    增加这个项后, 你会发现, 当应用进入后台后, 蓝牙还是保持连接的。

    可是, 进入后台后, 尽管应用还挂着, 可以正常接收数据。可是,  来数据了, 假设须要我们实时响应, 那就要用到推送了。

    也就是, 当数据来的时候, 弹出一个提示框, 提示用户来数据了。


    2. 设置本地推送

    这里的方法写在AppDelegate.m中。  receiveData相应你接收到数据的响应函数。

    -(void)receiveData:(NSData*)data
    {
        NSLog(@"收到数据了");
        
        //收到数据, 设置推送
        UILocalNotification *noti = [[UILocalNotification alloc] init];
        if (noti)
        {
            //设置时区
            noti.timeZone = [NSTimeZone defaultTimeZone];
            //设置反复间隔
            noti.repeatInterval = NSWeekCalendarUnit;
            //推送声音
            noti.soundName = UILocalNotificationDefaultSoundName;
            //内容
            noti.alertBody = @"接收到数据了";
            noti.alertAction = @"打开";
            //显示在icon上的红色圈中的数子
            noti.applicationIconBadgeNumber = 1;
            //设置userinfo 方便在之后须要撤销的时候使用
            NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
            noti.userInfo = infoDic;
            //增加推送到uiapplication
            UIApplication *app = [UIApplication sharedApplication];
            [app scheduleLocalNotification:noti];
        }
    }

    #pragma mark - 接收到推送
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"来电提示"
                                                        message:notification.alertBody
                                                       delegate:nil
                                              cancelButtonTitle:@"接听"
                                              otherButtonTitles:@"挂断",nil];
        [alert show];
        //这里,你就行通过notification的useinfo。干一些你想做的事情了
        application.applicationIconBadgeNumber -= 1;
    }


  • 相关阅读:
    javascript 中的暗物质 闭包
    关于使用 jBox 对话框的提交问题
    Orchard 的项目结构解决方案文件夹的原理与使用
    翻译:创建 Windows8 应用 Part I: Hello, world!
    翻译:FireBug 1.10 新特性
    SQL数据库设计经验
    我的女孩
    在WINDOWS下安装MRTG全攻略网络流量监控
    ASP实用函数库
    DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6911598.html
Copyright © 2011-2022 走看看