Bluetooth Sessions

This is a beautiful piece of code, this lovely system allows any program to request the use of Bluetooth which can then finish the session which will restore the Bluetooth to its original state before the request.

Requires linking the IOBluetooth and foundation frameworks

BTRequestSession();
Used to enable bluetooth session

BTFinishSession();
Used to finish your session and restore bluetooth back to its orginal state

BTPowerState()
Returns the current bluetooth state, BT_ON == 1, BT_OFF == 0

BTSetPowerState(int powerState)
Sets bluetooth state; BT_ON == 1, BT_OFF == 0

//
// BluetoothController.m
// SyncBar
//
// Created by Andrew James on 10/07/07.
// semaja2.net 2007 Licenced under the GPL.
//

#import <Foundation/Foundation.h>
// link against IOBluetooth.framework

#define BT_ON 1
#define BT_OFF 0

int state = -1;

inline bool BTPowerState()
{
return IOBluetoothPreferenceGetControllerPowerState();
}

int BTSetPowerState(int powerState)
{
IOBluetoothPreferenceSetControllerPowerState(powerState);

if (BTPowerState() != powerState) {
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

int BTRequestSession()
{
state = BTPowerState();
switch(state) {
case BT_ON:
return EXIT_SUCCESS;
case BT_OFF:
return BTSetPowerState(BT_ON);
default:
return EXIT_FAILURE;
}
}

int BTFinishSession()
{
switch(state) {
case BT_ON:
return EXIT_SUCCESS;
case BT_OFF:
return BTSetPowerState(BT_OFF);
default:
return EXIT_FAILURE;
}
}

Leave a Reply

*