iOS CoreBluetooth - Peripheral connect to Central

2019-08-20 06:30发布

问题:

I have a basic BLE App that I am running with Objective-C. I am using PeripheralManager and running my iPhone8 as a peripheral with serivces, characteristics added.

What I would like to do is connect to a central device that I know by Device ID or when it comes within range.

I have written a BLE App for Android that does this now. All I have to do is trigger the device.connectGatt() when onConnectionStateChange() is connected. This is in the BluetoothGattServerCallback() eventHandler.

What is the equivant for this in iOS? Keep in mind I that this is at connected state, not when the central has subscribed for services. This seems obvious in BluetoothGatt on Android but not see anything obvious in iOS PeriphalManager.

Anyone done anything similar? Seems to be a basic question, please help.

回答1:

In CoreBluetooth it is the central that makes the connection.

If you know the device-specific identifier for a peripheral (ie, you have previously discovered it on this central device) then you can attempt to retrieve a CBPeripheral instance from the central and then issue a connect. This will complete when the device comes into range. If you allow background mode then this connect can be completed when your app is in the background.