質問

I'm writing an application which is based on Apple's Temperature Sensor Application for iOS devices. I'm trying to implement a Disconnect button which will disconnect the currently connected device from the iPhone, however when the disconnect button is pressed there is a BAD_ACCESS error, I know this is memory based but I'm completely at a loss on how to fix it. Code follows.

- (IBAction)clickbutton:(id)sender 
{

    [[LEConnect sharedInstance] startScan:AccelerometerServiceUUID];
}

- (IBAction)disconnectButton:(id)sender 
{
    CBPeripheral    *peripheral;
    if(CBPeripheralStateDisconnected)
    {
        [[LEConnect sharedInstance] disconnectPeripheral:peripheral];
    }
}

The startScan button works correctly but the disconnect button does not. The code in the button is based on the code for finding devices shown below:

if (CBPeripheralStateConnected)
{
    [[LEConnect sharedInstance] connectPeripheral:peripheral];
    [currentlyConnectedDevice setText:[peripheral name]];
}

earlier in this function the same CBPeripheral *peripheral; pointer is made.

Sorry if this is a dumb question or has been asked before, I'm just really struggling and in desperate need of help! Thanks

役に立ちましたか?

解決

The disconnectButton method has two errors. First the peripheral variable is used without being initialized (are you ignoring compiler warnings?). Second, the if statement checks if the peripheral is disconnected and then disconnects it again (you should be checking that the peripheral is connected).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top