How do I remove paired Bluetooth devices on iOS?

邮差的信 提交于 2019-12-24 07:21:17

问题


I want my app could remove paired bluetooth devices. Becaues if device paried with iPhone, the device couldn't used for another device. I tried CBCentralManager cancelPeripheralConnection, but it doesn't work. they're still paired.

Or there's some other app still connected this server?

iPhone5,iOS7


回答1:


You cannot unpair a device programmatically in iOS.

The cancelPeripheralConnection is only to disconnect your apps connection to the device.

Discussion

This method is nonblocking, and any CBPeripheral class commands that are still pending to peripheral may or may not complete. Because other apps may still have a connection to the peripheral, canceling a local connection does not guarantee that the underlying physical link is immediately disconnected. From the app’s perspective, however, the peripheral is considered disconnected, and the central manager object calls the centralManager:didDisconnectPeripheral:error: method of its delegate object.




回答2:


cancelPeripheralConnection: should work.

When you connect with a peripheral (Bluetooth device) probably you are doing it in:

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    [centralManager connectPeripheral: peripheral
                              options: nil];
}  

It is important to do it keeping the peripheral and the centralManager as a @property:

@property (nonatomic,strong) CBPeripheral *connectingPeripheral;
@property (nonatomic,strong) CBCentralManager *centralManager;

Then:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Starting Up a Central Manager
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self 
                                                               queue:nil
                                                             options: nil];
}

And:

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    self.connectingPeripheral = peripheral;

    [centralManager connectPeripheral: self.connectingPeripheral
                              options: nil];
}  

Then when the connection is successfully stablished, the central manager object calls: centralManager:didConnectPeripheral:

After that you can call in your code:

[self.centralManager cancelPeripheralConnection:self.connectingPeripheral];


来源:https://stackoverflow.com/questions/22755703/how-do-i-remove-paired-bluetooth-devices-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!