Minimal iOS BluetoothManager Example - iphone

I've been constructing a minimal example for detecting nearby Bluetooth devices using the BluetoothManager private framework in iOS 5.0.
Using an answer found in this question: Finding generic Bluetooth devices within reach
Here's my viewDidLoad method to register for the BluetoothAvailabilityChangedNotification. I also register for the BluetoothDeviceDiscoveredNotification as well.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(bluetoothAvailabilityChanged:)
name:#"BluetoothAvailabilityChangedNotification"
object:nil];
btCont = [BluetoothManager sharedInstance];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(deviceDiscovered:)
name:#"BluetoothDeviceDiscoveredNotification"
object:nil];
When I get the Bluetooth availability changed notification, I set the device scanning enabled, as outlined in one of the answers in the aforementioned link.
- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
NSLog(#"BT State: %d", [btCont enabled]);
[btCont setDeviceScanningEnabled:YES];
}
For completeness, here's the deviceDiscovered notification method.
- (void)deviceDiscovered:(NSNotification *) notification
{
NSLog(#"Discovered one!");
}
The logs produced by running the test application are as follows:
BTM: attaching to BTServer
BTM: posting notification BluetoothAvailabilityChangedNotification
BT State: 1
BTM: setting device scanning enabled
Unfortunately, the phone isn't picking up any Bluetooth devices at all, even though I know there are proximate discoverable devices (verified using an Android device).
Some things I have tried already:
Calling [btCont setPowered: YES]; and registering for the associated power state change notification, executing setDeviceScanningEnabled:YES in the callback
Calling [btCont resetDeviceScanning] prior to the setDeviceScanningEnabled call
Calling the scanForConnectableDevices:(unsigned int)arg1; method, guessing that arg1 may be some kind of timeout value. I've tried a variety of values with no success.
Any thoughts would be much appreciated.
Thanks!

As far as I know, the bluetooth manager gets the list after OS has filtered the results.
You will only get the nearby headset devices and not all generic devices.
you have to use scanForServices:
// start scan
[btManager setDeviceScanningEnabled:YES];
[btManager scanForServices:0xFFFFFFFF];

Related

Detect low battery warning ios [duplicate]

This question already has answers here:
Detecting the type of iPhone interrupt
(2 answers)
Closed 10 years ago.
I'm making a turnbased game. To prevent users from cheat, the turn will automatically get passed to the opponent if the user close the app in the middle of a turn. This because so the user can't close the app, restart it, and beging the turn from the beginning.
There are two cases that should penalize the player however. If a phone call gets in, or the low battery warning appears. I can detect the phone call coming in and respond, but I don't know what to do with the battery?
Any suggestions would be awesome
Battery monitoring is enabled by setting to YES a property of the UIDevice singleton:
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
iPhone OS provides two type of battery monitoring events, one for when the state changes (e.g., charging, unplugged, full charged) and one that updates when the battery’s charge level changes. As was the case with proximity monitoring, you register callbacks to receive notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
ALso refer this link.

How to detect if user did a screenshot with home+power in iOS?

I'm trying to detect if user take a screenshot in order to warn them this is a video with copyright. I've tried Darwin Notification, but it didn't work...
NSMetadataQuery might be a solution, but we want the app to run on iOS4 as well. Can somebody help me?
Thanks!:p
It is not possible to detect a screenshot has (or will) be taken on iOS 5.
In iOS 4.0, there was a Darwin notification fired PictureWasTakenNotification, but not anymore.
The documentation does not mention anything, and using NSNotificationCenter to subscribe to all notifications only shows silence after taking a screenshot.
before ios 4.0 you can register for PictureWasTakenNotification .This notification is sent when screenshot is taken.No notifications will be sent before the screenshot was taken.
But after that this has been discontinued and IMHO there is no other way round... :/
you can detect it with adding an observer to UIApplicationUserDidTakeScreenshotNotification .
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note) {
// executes after screenshot
}];

Prevent iPhone from locking when connected to charger and app is running

I'm writing an iPhone app. When the app is running and the iPhone is charging, there is no need to lock the iPhone. Is it possible to prevent the locking of the iPhone when the device is charging and my app is running?
You can subscribe to UIDeviceBatteryStateDidChangeNotification notification to get the moment when your iphone begins/stops to charge. Then in case iphone is charging you can set idleTimerDisabled property in UIApplication object to YES to prevent device to go to sleep:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateBatteryState:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
- (void) updateBatteryState:(NSNotification*)notification{
[UIApplication sharedApplication].idleTimerDisabled =
([UIDevice currentDevice].batteryState == UIDeviceBatteryStateCharging);
}
P.S. If user decides to put device to sleep with sleep/wake button there's no way to prevent him of doing so
This is not possible with current SDK.
EDIT: hmm, haven't got the question correctly from the first read - look on other replies for correct answer; my guess was, you were asking about if it possible to prevent appearance of the sync/charge screen when connecting device via usb or to the wall outlet

External Accessory Framework - giving me multiple device connect notifications

I've started working with the External Accessory Framework and the Apple Camera Connector. I've got some barebones code working but am seeing some odd behavior.
Basically: when I connect the Camera Connector, I get three events
- EAAccessoryDidConnectNotification
- EAAccessoryDidDisconnectNotification
- EAAccessoryDidConnectNotification
So it sees the device connecting, then disconnecting, then connecting. I've tried manually connecting the device slowly, quickly, etc. - doesn't make a difference.
The code I am using to set up notifications is boilerplate (below). Is there any known reason why I might be getting that connection hiccup?
(fwiw - I am planning to use this with the Bass MIDI lib to determine when a keyboard has been connected/disconnected).
--thanks!
- (void)viewWillAppear:(BOOL)animated
{
// watch for the accessory being connected
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
// watch for the accessory being disconnected
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
}

UIDeviceOrientationDidChangeNotification doesn't work while device is on table?

Hi there i have a problem,
In my AppDelegate i have used method beginGeneratingDeviceOrientationNotifications to start notify me when device starts rotating.
It works fine if i hand-held my ipad but when it is kept on table it doesn't work as expected.
it fires UIDeviceOrientationUnknown notification.
Also this notification gets started after UI launches not on splash screen.
following is my code:
if([[[PulseUIFactory Factory] GetUICreator] IsIPad])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
current device stars giving proper values.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
then some where i launches my UI as
[self Launch_UI];
but notification starts responding after [self Launch_UI]; call even if notification is registered before its call...
Please any help is appreciable!!!
When you place your device on a table, [[UIDevice currentDevice] orientation] will return UIDeviceOrientationFaceUp. Then if your device remains sitting on the table face up, it doesn't matter how you rotate it on the table, the current device orientation will still be UIDeviceOrientationFaceUp.
If the device has issues determining the orientation, you may get UIDeviceOrientationUnknown. See this tutorial on how to handle device rotation using UIDeviceOrientationDidChangeNotification.
Regarding your notification only firing after the UI is loaded, the UIDeviceOrientationDidChangeNotification will only fire when the device is rotated. So if you are not rotating your device until after the UI loads, you wont get a notification. If this is not the cause of the issue, I'd have to see more of your code to have a better idea of what is going on.