I'm trying to implement device discovery using bluetooth in IOS 5.0.1 iPhone 4S.
I'm using the private framework BluetoothManager.
My code is:
- (IBAction)searchForDevices:(id)sender
{
[self.indicator setHidden:NO];
[[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];
}
- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
self.label.text = #"Availability changed!";
[btCont setDeviceScanningEnabled:YES];
}
- (void)deviceDiscovered:(BluetoothDevice *)device
{
[self.indicator setHidden:YES];
self.label.text = device.address;
My bluetooth headset is discovered.
deviceDiscovered callback function is called,
but device.address does NOT contain the MAC address of the bluetooth device. The app is crashing.
Also, device.name return the name of the notification (BluetoothDeviceDiscoveredNotification) instead of the name of the device discovered.
Any suggestions how can I retrieve the MAC address of my bluetooth headset this way?
use this code:
- (void)deviceDiscovered:(NSNotification *) notification {
BluetoothDevice *bt = [notification object];
NSLog(#"name: %# address: %#",bt.name, bt.address);
If this is a jailbreak app, you can use the key kLockdownBluetoothAddressKey via liblockdown.dylib
Related
I am trying to send a notification from my iPhone to the Watch device using
CFNotificationCenterAddObserver on the watch and CFNotificationCenterPostNotification.(I am NOT testing on the Xcode simulator).
This is my code in the iOS app:
#include <CoreFoundation/CoreFoundation.h>
...
- (void)sendLogOutNotificationToWatch{
dispatch_async(dispatch_get_main_queue(), ^{
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
});
}
And this is how I use it on the apple watch extension app:
#implementation InterfaceController
.....
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
....
[self registerToNotification];
}
- (void)registerToNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#com.test.app" object:nil];
CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userLoggedOut ) name:#"com.test.app" object:nil];
CFNotificationCenterAddObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), didReceivedDarwinNotification, CFSTR( "NOTIFICATION_TO_WATCH" ), NULL, CFNotificationSuspensionBehaviorDrop );
}
void didReceivedDarwinNotification()
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"com.test.app" object:nil];
}
- (void)didDeactivate {
[super didDeactivate];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"com.test.app" object:nil];
CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)userLoggedOut{
[self showAlertViewwithTitle:#"Notice" andMessage:#"User logged out on iPhone device!"];
}
You should use WatchConnectivity to send a message from the iPhone to the Apple Watch.
The API's are almost identical on both the Watch and iPhone. If your watch app is not running, or the screen is off you should use transferUserInfo. If your watch app is running and the screen is on you can use sendMessage. I normally wrap these calls, attempting to use sendMessage first and if it fails use transferUserInfo:
// On the iPhone
func trySendMessage(message: [String : AnyObject]) {
if self.session != nil && self.session.paired && self.session.watchAppInstalled {
self.session.sendMessage(message, replyHandler: nil) { (error) -> Void in
// If the message failed to send, queue it up for future transfer
self.session.transferUserInfo(message)
}
}
}
On the watch you will need to implement both session:DidReceiveMessage and session:didReceiveUserInfo. Note that I don't bother checking if the watch is reachable, because if it is not (or if it starts reachable and moves out of range after the check but before the transfer has finished) then the data will still get sent when it is back in range from transferUserInfo.
Is this on watchOS 2? As others suggested, you need to use WatchConnectivity. Darwin notification won't work since the watchOS 2 process no longer sits on the phone as it used to in watchOS 1. Besides, WatchConnectivity is so much more convenient.
Can application send a message in twitter without user intervention under certain conditions? For example when charging the battery less 10%?
please, sorry for bad english!
You can monitor battery level like so:
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];
if(device.batteryLevel <= 10)
[self twittBatteryLevel: device.batteryLevel];
}
And then implement -(void)twittBatteryLevel:(float)batteryLevel as you would using twitter API
Prior iOS 8 all works fine. The problem is:
I have two observers in different classes:
class1:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishParseUser:)
name:USERS_LOADED_NOTIFICATION_ID object:nil];
class2:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishParseUser:)
name:USERS_LOADED_NOTIFICATION_ID object:nil];
and notification is posted in some other place:
[FBRequestConnection startWithGraphPath:#"me/friends?fields=id,first_name,last_name,picture.type(small)" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
[[NSNotificationCenter defaultCenter] postNotificationName:USERS_LOADED_NOTIFICATION_ID object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: currentUser, #"user", friends, #"friends", nil]];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
addObserver method is called for both of mentioned classes, however notification is being delivered just to one observer. If I delete this observer(which receives the notification), then another one receives the notification.
Prior to iOS 8 both observers receive the notification.
Can you, please, help me with this issue?
Found answer.
There is another way in iOS 8 to register for receiving remote notifications. I get nil for device token and app break on line:
NSDictionary *item = #{UID_ID : sCurrentUserId, #"deviceToken": appDelegate.deviceToken, #"handle": #"", #"friends": friends};
and second observer never receives notification.
You need to register your NSNotifications to work on iOS 8 and later but don't need to register if your iOS version is less than iOS 8.
Use Following Code
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[vComp objectAtIndex:0] intValue] >= 8) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationActivationModeBackground categories:nil]];
}
happy Coding
I want to check the internet connection in my iPhone without the use of Reachability Classes. i want to constantly check the connection when a particular event is triggered in my view. I also want to determine if the connection is from a Wifi or through 2G or 3G connection. I have already tried using Reachability classes. But these classes just return the value if the Wifi is On (Though the net cable is unplugged from the Wifi Router). I have tried
[Reachability reachabilityForInternetConnection]
and
[Reachability reachabilityWithHostName:#"www.google.com"];
But the above methods doesn't seem to work properly inspite of network disconnections.
Also is there any way we can determine the type of netwrok 2G or 3G in iOS6? I know that we have Core Telephony Framework that works only in iOS 7. But i just want to know if i can determine the cellular network iOS 6.0. Please help me.
Working code without reachability classes and also working in iOS 6:
- (NSNumber *) dataNetworkTypeFromStatusBar {
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:#"statusBar"] valueForKey:#"foregroundView"] subviews];
NSNumber *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(#"UIStatusBarDataNetworkItemView") class]]) {
dataNetworkItemView = subview;
break;
}
}
return [dataNetworkItemView valueForKey:#"dataNetworkType"];
}
And the value keys I've found so far:
0 = No wifi or cellular
1 = 2G and earlier?
2 = 3G?
3 = 4G
4 = LTE
5 = Wifi
Or in iOS7 use CoreTelephony framework
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(#"Current Radio Access Technology: %#", telephonyInfo.currentRadioAccessTechnology);
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note)
{
NSLog(#"New Radio Access Technology: %#", telephonyInfo.currentRadioAccessTechnology);
}];
I am able to get EA notification when connecting to an external device (MFi compliant) via USB but not via Bluetooth. Why is EA notification not being fired for Bluetooth connection, contrary to what the documentation suggests?
1) Did you set the Protocol String correctly in your InfoPlist?
2) Did you set the Protocol String correctly in your bluetooth device?
3) Did you register for incoming connection event? like this way:
-(void) <someMethod> {
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryConnected:)
name:EAAccessoryDidConnectNotification
object:nil];
}
-(void)accessoryConnected: (NSNotification *)notification {
EAAccessory *accessory = [[notification userInfo] objectForKey:EAAccessoryKey];
NSLog(#"%# connected", accessory.name);
}