search

Home  >  Q&A  >  body text

objective-c - iOS 有什么办法可以直接跳转到手机蓝牙设置界面连接蓝牙?

1.问题描述:
项目需要在APP应用中检测蓝牙是否已经打开,如果手机蓝牙没有开启,则需要弹出提示框,并点击跳转到手机蓝牙设置界面开启蓝牙。

2.尝试解决办法:

在iOS9之前,一只都是使用这个接口跳转的:

 let url = NSURL(string: "prefs:root=Bluetooth");
 if UIApplication.sharedApplication().canOpenURL(url!) {
      UIApplication.sharedApplication().openURL(url!)
 }

但是现在手机升级iOS10之后,这个方法就失效了。
请问有什么好的解决办法吗?

PS:
如果能直接在APP里面连接打开手机蓝牙开关,并且连接蓝牙就更好了!

高洛峰高洛峰2837 days ago761

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-18 09:50:47

    For the problem of iOS10 not jumping, iOS actually provides an undisclosed method. But moderation is a problem, but we can find ways to bypass it.

    NSString * defaultWork = [self getDefaultWork];
    NSString * bluetoothMethod = [self getBluetoothMethod];
    NSURL*url=[NSURL URLWithString:@"Prefs:root=Bluetooth"];
    Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace") ;
    [[LSApplicationWorkspace performSelector:NSSelectorFromString(defaultWork)] performSelector:NSSelectorFromString(bluetoothMethod) withObject:url withObject:nil];
    Use ASCII values ​​to assemble and combine methods. This bypasses review.

    -(NSString *) getDefaultWork{
    NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x64,0x65,0x66,0x61,0x75,0x6c,0x74,0x57,0x6f,0x72,0x6b,0x73,0x70, 0x61,0x63,0x65} length:16];

    NSString *method = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];

    return method;
    }

    -(NSString *) getBluetoothMethod{

    NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x6f, 0x70, 0x65, 0x6e, 0x53, 0x65, 0x6e, 0x73, 0x69,0x74, 0x69,0x76,0x65,0x55,0x52,0x4c} length:16];
    NSString *keyone = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
    NSData *dataTwo = [NSData dataWithBytes:(unsigned char []){0x77,0x69,0x74,0x68,0x4f,0x70,0x74,0x69,0x6f,0x6e,0x73} length:11];
    NSString *keytwo = [[NSString alloc] initWithData:dataTwo encoding:NSASCIIStringEncoding];
    NSString *method = [NSString stringWithFormat:@"%@%@%@%@",keyone,@":",keytwo,@":"];

    return method;
    }
    The above is the method to enter the Bluetooth interface. There can also be other pages to jump to. The setting page is @"@"Prefs:root=TETHERING", and the wifi is @"Prefs:root=WIFI". Note that the P in Prefs is capitalized. There are also disadvantages to writing this way. If Apple's undisclosed method is once modified, we must do it again. Modify.

    I have the answer at https://segmentfault.com/q/10... It is currently possible to jump to the settings page.

    reply
    0
  • Cancelreply