使用CoreBluetooth检测Apple Watch

时间:2015-12-29 21:24:36

标签: ios bluetooth core-bluetooth apple-watch

我正在使用我的iPhone上的Core Bluetooth发现蓝牙设备,但它没有拿起Apple Watch。有什么我想念的吗?这是我在下面使用的代码:

#pragma mark - CBCentralManagerDelegate

// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{     NSLog(@"%@", [peripheral name]);
}


// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch ([central state])
{
    case CBCentralManagerStatePoweredOff:
        NSLog(@"CoreBluetooth BLE hardware is powered off");
        break;
    case CBCentralManagerStatePoweredOn:
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
        [[self bluetoothManager] scanForPeripheralsWithServices:nil options:nil];
        break;
    case CBCentralManagerStateResetting:
        NSLog(@"CoreBluetooth BLE hardware is resetting");
        break;
    case CBCentralManagerStateUnauthorized:
        NSLog(@"CoreBluetooth BLE state is unauthorized");
        break;
    case CBCentralManagerStateUnknown:
        NSLog(@"CoreBluetooth BLE state is unknown");
        break;
    case CBCentralManagerStateUnsupported:
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
        break;
    default:
        break;
}
}

2 个答案:

答案 0 :(得分:0)

当我运行以下代码时,我的Apple Watch会出现:

<code id="code"></code>

[self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:@"180A"]]]; 是设备信息服务。

不确定您可以发现和订阅哪些特征(如果有),但您绝对可以检测到该手表的存在。

答案 1 :(得分:-2)

您不要使用Core Bluetooth连接到Apple Watch,而是使用另一个称为连接性的框架

https://developer.apple.com/documentation/watchconnectivity/using_watch_connectivity_to_communicate_between_your_apple_watch_app_and_iphone_app