Detecting Apple Watch Using CoreBluetooth - ios

I am discovering bluetooth devices using Core Bluetooth from my iPhone, but it does not pick up the Apple Watch. Is there something I'm missing? Here is the code I'm using below:
#pragma mark - CBCentralManagerDelegate
// method called whenever you have successfully connected to the BLE peripheral
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
}
// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{ NSLog(#"%#", [peripheral name]);
}
// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch ([central state])
{
case CBCentralManagerStatePoweredOff:
NSLog(#"CoreBluetooth BLE hardware is powered off");
break;
case CBCentralManagerStatePoweredOn:
NSLog(#"CoreBluetooth BLE hardware is powered on and ready");
[[self bluetoothManager] scanForPeripheralsWithServices:nil options:nil];
break;
case CBCentralManagerStateResetting:
NSLog(#"CoreBluetooth BLE hardware is resetting");
break;
case CBCentralManagerStateUnauthorized:
NSLog(#"CoreBluetooth BLE state is unauthorized");
break;
case CBCentralManagerStateUnknown:
NSLog(#"CoreBluetooth BLE state is unknown");
break;
case CBCentralManagerStateUnsupported:
NSLog(#"CoreBluetooth BLE hardware is unsupported on this platform");
break;
default:
break;
}
}

My Apple Watch shows up when I run the following code:
[self.centralManager retrieveConnectedPeripheralsWithServices:#[[CBUUID UUIDWithString:#"180A"]]];
180A is the Device Information service.
Not sure which characteristics you can discover and subscribe to, if any, but you can definitely detect the watch's presence.

U do not use Core Bluetooth to connect to Apple Watch you use another framework called connectivity
https://developer.apple.com/documentation/watchconnectivity/using_watch_connectivity_to_communicate_between_your_apple_watch_app_and_iphone_app

Related

iOS8 and BTLE | CBCentralManager unable to find peripherals

I have an iOS app that is connecting to a device (arduino) using a BTLE. Everything is working fine on my iPad iOS 7. After upgrading to iOS 8, the CBCentralManager is not finding any peripherals.
- (void)startScanningForSupportedUUIDs
{
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
I don't know what can be the problem.
I have the solution, for some reason in iOS 8 there is some delay after instantiate your CBManager. You need to start to scan when the CBCentralManager is on, in this method:
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOff:
NSLog(#"CoreBluetooth BLE hardware is powered off");
break;
case CBCentralManagerStatePoweredOn:
{
NSLog(#"CoreBluetooth BLE hardware is powered on and ready");
NSArray *uuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID], nil];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[centralManager scanForPeripheralsWithServices:uuidArray options:options];
}
break;
case CBCentralManagerStateResetting:
NSLog(#"CoreBluetooth BLE hardware is resetting");
break;
case CBCentralManagerStateUnauthorized:
NSLog(#"CoreBluetooth BLE state is unauthorized");
break;
case CBCentralManagerStateUnknown:
NSLog(#"CoreBluetooth BLE state is unknown");
break;
case CBCentralManagerStateUnsupported:
NSLog(#"CoreBluetooth BLE hardware is unsupported on this platform");
break;
default:
break;
}
In IOS 7 you could get away by starting a BLE scan even before the CBCentralManager was ready. IOS 7 used to spit out a warning in such cases -
CoreBluetooth[API MISUSE] can only accept commands while in the powered on state
With IOS8 - the warning no more appears and the scan does not actually start. To overcome the problem, wait for the CBCentral to power on - ie, wait for CBCentral manager to get to the "CBCentralManagerStatePoweredOn" state and then start the scan. It works fine with that change:)

Unable to discover peripheral Bluetooth Low Energy devices

I recently upgraded my phone to the iOS Beta version 8 and installed my iOS app . Unfortunately my app no longer is able to discover my peripheral BLE device. I checked for any documentation which says if there has been any change but found none. Has there been any known API changes that have been introduced as part of iOS 8 ? I am testing on iPhone 5s
My code was working earlier on IOS version 7.xx
Relevant piece of code :
[self.CM scanForPeripheralsWithServices:nil options:nil];
Where are you launching the scan?
You should call
self.CM = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
in the viewDidLoad or anywhere else you need to to that, and then scan for peripherals only when the centralmanager state is on:
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) {
case CBCentralManagerStatePoweredOff:
NSLog(#"CoreBluetooth BLE hardware is powered off");
break;
case CBCentralManagerStatePoweredOn:
{
NSLog(#"CoreBluetooth BLE hardware is powered on and ready");
[self.CM scanForPeripheralsWithServices:nil options:nil];
}
break;
case CBCentralManagerStateResetting:
NSLog(#"CoreBluetooth BLE hardware is resetting");
break;
case CBCentralManagerStateUnauthorized:
NSLog(#"CoreBluetooth BLE state is unauthorized");
break;
case CBCentralManagerStateUnknown:
NSLog(#"CoreBluetooth BLE state is unknown");
break;
case CBCentralManagerStateUnsupported:
NSLog(#"CoreBluetooth BLE hardware is unsupported on this platform");
break;
default:
break; }

Detecting Bluetooth Enabled iPhone devices in IOS7 in IOS

I am using Core Bluetooth Framework in my app.
I know how to scan for peripherals and getting values from it.(like Heart Rate Monitor)
But what I want is to retrieve the surrounding iPhone Devices list that supports BLE 4.0 and Bluetooth Enabled ones.
I referred below links..
Uses IOBluetooth Framework
Uses CoreBluetooth For Getting Peripherals not the Devices List
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// I'm not sure how to make this work
NSLog (#"Discovered peripheral: %#", [peripheral name]);
[self.list addObject:peripheral.name]; // add peripheral name to foundarray
NSLog (#"UUID peripheral: %#", [peripheral UUID]);
NSLog (#"peripheral services before connected: %#", [peripheral services]);
NSLog(#"adversting data %#",[NSString stringWithFormat:#"%#",[advertisementData description]]);
NSLog(#"foundArray is %#", self.list);
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSLog(#"Central manager's state is updated to: %#", central);
if(central.state == CBCentralManagerStatePoweredOn)
{
//okay your good to go and can now scan
}
else
{
//Unable to use CentralManager methods so print out the central.state and find out why
}
}
I dont know whether Apple provides this or not..
Any Suggestions or Ideas will be appreciated..
Thank In Advance..
Try below code checkBluetoothAccess and requestBluetoothAccess method
- (void)checkBluetoothAccess {
if(!self.cbManager) {
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
We can ask the bluetooth manager ahead of time what the authorization status is for our bundle and take the appropriate action.
*/
CBCentralManagerState state = [self.cbManager state];
if(state == CBCentralManagerStateUnknown) {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(#"UNKNOWN", #"")];
}
else if(state == CBCentralManagerStateUnauthorized) {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(#"DENIED", #"")];
}
else {
[self alertViewWithDataClass:Bluetooth status:NSLocalizedString(#"GRANTED", #"")];
}
}
- (void)requestBluetoothAccess {
if(!self.cbManager) {
self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
/*
When the application requests to start scanning for bluetooth devices that is when the user is presented with a consent dialog.
*/
[self.cbManager scanForPeripheralsWithServices:nil options:nil];
}
You can only retrieve the surrounding iOS devices that support Bluetooth 4.0 if they are also advertising a specific service to identify them. You can't just see all iOS devices that are powered on and nearby. If they are advertising, you can just scan for nil and that will return the advertisement packets being seen.
Note: if you care about retrieving BLE devices currently connected from other apps, you can use the retrieveConnectedPeripheralsWithServices: method.

Couldn't Discover Peripheral in Core Bluetooth

I've iPhone app and I want to connect with bluetooth device to get Glucose Measurements. You can find the device from here.
After reading Apple documentation about Core Bluetooth I started reading these tutorial. Also I get the services ID's for bluetooth devices from these link here
So I started to code like in the tutorial after understanding the basics.
And these my Code:
#define POLARH7_HRM_DEVICE_INFO_SERVICE_UUID #"180A" // for Device Information service.
#define POLARH7_HRM_HEART_RATE_SERVICE_UUID #"1808" // For Glucose service.
NSArray *services = #[[CBUUID UUIDWithString:POLARH7_HRM_HEART_RATE_SERVICE_UUID], [CBUUID UUIDWithString:POLARH7_HRM_DEVICE_INFO_SERVICE_UUID]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:services options:nil];
self.centralManager = centralManager;
And I've implemented the delegates for CBCentralManagerDelegate and CBPeripheralDelegate
I receive a notification for centralManagerDidUpdateState
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
// Determine the state of the peripheral
if ([central state] == CBCentralManagerStatePoweredOff) {
NSLog(#"CoreBluetooth BLE hardware is powered off");
}
else if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(#"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([central state] == CBCentralManagerStateUnauthorized) {
NSLog(#"CoreBluetooth BLE state is unauthorized");
}
else if ([central state] == CBCentralManagerStateUnknown) {
NSLog(#"CoreBluetooth BLE state is unknown");
}
else if ([central state] == CBCentralManagerStateUnsupported) {
NSLog(#"CoreBluetooth BLE hardware is unsupported on this platform");
}
}
My NSLog logs : CoreBluetooth BLE hardware is powered on and ready.
But I did not receive a notification for central didDiscoverPeripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if ([localName length] > 0) {
NSLog(#"Found the heart rate monitor: %#", localName);
[self.centralManager stopScan];
self.polarH7HRMPeripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
These method is not being called.
So the central (my iPhone) couldn't discover the Peripheral which is my Glucose device.
I couldn't find the device when I search for it from Setting ->Bluetooth.
If centralManager:didDiscoverPeripheral:advertisementData:RSSI: is never called, this likely means that there is no peripheral with one of provided CBUUIDs. You could try to provide nil instead of the services-array and check if there is any peripheral available (don't do this in production mode).
OK, I think I figured out the problem after sending email to TaiDoc company they told me that these device dose not support iPhone bluetooth integration.
I tried to connect with Tom-Tom GPS watch and it's connected successfully :)
So I think the problem of the issue is a Hardware issue.

How to reconnect BLE device in background after turn off and then turn on?

I am working on a small project related to BLE. I have a requirement that I need to re-connect the device in background after manual turn off and then off bluetooth from iPhone->Settings->Bluetooth.
Just store the peripheral identifier or (UUID for < iOS 7), retrieve the peripheral, and call connect on it when the centralManager updates state to powered on.
For iOS 7:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere
NSArray *peripherals = [_cbCentralManager retrievePeripheralsWithIdentifiers:#[uuid]];
for(CBPeripheral *periph in peripherals)
{
[_cbCentralManager connectPeripheral:periph options:nil];
}
}
}
For iOS 6:
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
CFUUIDRef uuid;//the cfuuidref you've previously saved
[central retrievePeripherals:#[(id)uuid]];//now wait for the delegate callback below
}
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
for(CBPeripheral *periph in peripherals)
{
[_centralManager connectPeripheral:periph options:nil];
}
}
NOTE: these are just code snippets. You should also monitor CBCentralManagerStatePoweredOff (among others) and cancel all current peripheral connections when you get that update.

Resources