know from the peripheral that whether it is disconnected from central - iphone

I am having a BLE device acting as peripheral and an iPhone4s as central.Both are connected via BluetoothLE connection.My question is that can i get to know from the peripheral end that it has been disconnected from the central.

Depending on the implementation of your peripheral (HCI interface or proprietary) you would either get a Disconnection Complete Event (Bluetooth Core Spec 4.0 - Volume 2 Part E section 7.7.5) or the proprietary equivalent event.
Could you provide some more information about the peripheral you are using?

If the Peripheral disconnect you can catch the didDisconnect (on the iOS side)
-(void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)
You can also ask iOS about "Known" devices (only those PAIRED, thus remembered/Cached in iOS6)
Go through each one and check the peripheral.isConnected flag.
Unfortunately iOS seems to also cache this for too long so some times you will see the isConnected flag even though connection has been lsot.
From Peripheral side you must keep track of didConnects / didDisconnects.

Related

CoreBluetooth APIMISUSE (send command - peripheral not connected)

The system i'm working with uses the following scenario:
Connecting to bluetooth LE device
Discover Services & Characteristics
Write command to TX characteristic and receive response
Above works fine 90% of time. Now and then the system gets into a state where 3rd step constantly fails (there is no response from device whatsoever even though step 1 and 2 succeeded. Restarting the app / phone / BLE device DOES not remedy this. Block is constant. What does resolve the problem is manually unpairing the device from iOS system settings. Looking at BLE diagnostic logs i get this:
"pon. mar 7 21:27:30 Preferences[380]: [CoreBluetooth] API MISUSE: can only accept commands while in the connected state"
However prior to sending the commands i've debugged the app and i'm 100% the connection is established and services&characteristics have been discovered. Any Idea? Anybody facing similiar problems?
Did you implement centralManager:didDisconnectPeripheral:error: in your central manager delegate?
It will notify you when a peripheral disconnects. Could be that the peripheral disconnected or there was a connection error. You should always make sure to only do read/write operations while in the connected state.
Maybe also have a look at this method: centralManager:didFailToConnectPeripheral:error:

Is an iPhone being peripheral and central with connecting to several peripheral iPhones one after another doable?

My app needs to detect other iPhones and connect to all of them one at a time quickly (Those iPhones also have the same functionality). So I implemented both peripheral and central. I only try connecting to another phone if:
if (![_discoveredPeripheral isConnected] && [[UIDevice currentDevice] name] < [advertisementData objectForKey:#"kCBAdvDataLocalName"])
(I advertise the name of the device to avoid both devices to connect to each other at the same time)
It works correctly, but after a few minutes I get this error:
[1622:1103] CoreBluetooth[ERROR] XPC connection interrupted, resetting
Is the problem in my algorithm of connecting? Or is it not doable to be a peripheral and connect as a central at the same time?
I was also working with the same kind of application. As I understand you are trying to use same device for the central as well as peripheral application .You cannot do this . Because what I have observe is peripheral application is only can be detected when it is in foreground and in same device you cant run the two program in the foreground at a time . So you need to install the central and peripheral application in two separate devices .

Why GKSession always tries to use bluetooth when running on a device and doesn't on simulator?

I'm trying to implement GameKit connection without using GKPeerPickerController. What i need is to establish WiFi connection, not bluetooth.
This is how i do that
self.gameSession = [[GKSession alloc] initWithSessionID:#"test" displayName:nil sessionMode:GKSessionModePeer];
self.gameSession.available = YES;
self.gameSession.delegate = self;
self.gameSession.disconnectTimeout = 0;
[self.gameSession setDataReceiveHandler:self withContext:nil];
My problem is that devices always try to connect over Bluetooth. At least i think so cause the following always appears in the console
BTM: attaching to BTServer
Even if i turn bluetooth off it always tries to deal with bluetooth, not wifi. Moreover - if bluetooth is on - devices never see each other if i don't use GKPeerPickerController.
Also, simulator never tries to look for bluetooth and is always able to establish wifi connection and find any device easily. How do i make GKSession choose WiFi connection?
May be your issue arising with Jailbroken Device,
GKSession and GKSessionDelegate implementations works for both bluetooth and WiFi.
these two class checks wifi and bluetooth, and choose an appropriate transmission medium.
If your iPhone is Jailbroken, then there might be some issues with BlueTooth or wifi while connecting over those transmission medium, and possibly that is prevent to Work with gamekit Properly.
And also try to use
picker.connectionTypesMask = GKPeerPickerConnectionTypeOnline
check with Condition in Delegate methods
if (type == GKPeerPickerConnectionTypeOnline) {
}
Update :
Refer this sample code of Apple..
https://developer.apple.com/library/ios/#samplecode/GKTapper/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010283

Detecting when Bluetooth is disabled on iOS5

I'm developing blog speaker app.
I wanna pause the audio when bluetooth is disabled like iPod app. I thought it's not possible without using private api after reading this.
Check if Bluetooth is Enabled?
But, my customer told me that Rhapsody and DI Radio apps both support it.
Then I found iOS5 has Core Bluetooth framework.
https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CoreBluetooth_Framework/CoreBluetooth_Framework.pdf
CBCentralManagerStatePoweredOff status seems like the one.
But, the description says this api only supports Bluetooth 4.0 low energy devices.
Did anyone try doing the same thing?
I want to support current popular bluetooth headsets, or bluetooth enabled steering wheel on the car. I don't know if it's worth trying when it only supports some brand new bluetooth.
For audio, focusing on Bluetooth specifically sounds like the wrong approach.
I think what you're looking for is Handling Audio Hardware Route Changes.
You'll notice that all of the following cause the the built-in iPod app to pause:
Bluetooth device is removed (possibly because bluetooth has been disabled).
Headphones are unplugged.
Device is removed from docking station.
You get all that correct behavior when you use the Audio Session API.
On BLE you will get an update on the manager containing the state:
enum {
CBCentralManagerStateUnknown = 0, // State unknown,
update imminent.
CBCentralManagerStateResetting, // The connection with the system service was momentarily lost,
update imminent.
CBCentralManagerStateUnsupported, // The platform doesn't support Bluetooth Low Energy.
CBCentralManagerStateUnauthorized, // The app is not authorized to use Bluetooth Low Energy.
CBCentralManagerStatePoweredOff, // Bluetooth is currently powered off.
CBCentralManagerStatePoweredOn, // Bluetooth is currently powered on and available to use.
};
which you can check on the mandatory callback with e.g
centralManager:didUpdateState...{
if ([manager state] == CBCentralManagerStatePoweredOff)
{
[musicPlayer pause]
}
}

iphone ios 3.0.1, device is not being able to get a device token nor reporting an error?

ok i have an iphone with ios 3.0.1
i have wifi turned on, no cellular network.
I have created my provisioning profile, my certificate correctly. why?... gonna tell you in a while
anyway, when i run the application, i have put NSLog in
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
but when i run the application i don't get anything, no success nor error.
anyway i tried the SAME APPLICATION and run it on iphone with ios 4.0 and it did work and it printed the device token.
Oh and they are both using the same wifi network.
could it be the device? the ios ? or the security of the wifi connection?
and shouldn't at least give me an error in that case?
could that be caused by the missing library file
warning: Unable to read symbols for "/Library/MobileSubstrate/MobileSubstrate.dylib" (file not found).
thank you
According to Apple Documentation :
No Delegate Callbacks
When the first push-capable app is installed on a device, iOS attempts to
establish a persistent network connection to the push service that
will be shared by all push-capable apps on the device.
If neither delegate callback
application:didRegisterForRemoteNotificationsWithDeviceToken:
nor
application:didFailToRegisterForRemoteNotificationsWithError:
is called, that means that this
connection has not yet been
established.
This is not necessarily an error
condition. The device may not have
Internet connectivity at all because
it is out of range of any cell towers
or Wi-Fi access points, or it may be
in airplane mode. Instead of treating
this as an error, your app should
continue normally, disabling only that
functionality that relies on push
notifications.
Keep in mind that network availability
can change frequently. Once the
persistent connection to the push
service succeeds, one of the
previously-mentioned application
delegate methods will be called.
Push notifications use the cellular
data network whenever possible, even
if the device is currently using Wi-Fi
for other network activity such as web
browsing or email. However, the push
service will fall back to Wi-Fi if
cellular data service isn't available.
If your device is capable of using the
cellular data network, check that it
has an active cellular data plan. Turn
off Wi-Fi in Settings and see if you
can still browse the web with Safari,
for example. On the other hand, if the
push service is using Wi-Fi, any
firewalls between your device and the
Internet must allow TCP traffic to and
from port 5223.
NOTE: There is a separate persistent
connection to the push service for
each environment. iOS establishes a
persistent connection to the sandbox
environment for development builds; ad
hoc and distribution builds connect to
the production environment.
About second part of your question, MobileSubstrate is not the culprit, this is a normal log message... from a jailbroken iPhone :)