How to check if IOBluetoothDevice is peripheral (mouse or keyboard) and not something else - swift

I'm writing a small app that will operate with Bluetooth devices. I want it to only manage peripheral devices (mouse, keyboard, trackpad, etc.).
I'm using IOBluetoothDevice.pairedDevices() to get a list of devices. I need to filter it out to only include peripheral devices. I'm trying to use IOBluetooth instance properties deviceClassMajor and deviceClassMinor to do that. Unfortunately, apple documentation does not say what each value means.
Is there some document or spec that describes which deviceClassMajor and deviceClassMinor refers to a keyboard or mouse or trackpad? Or maybe there is another way to check if a particular device is peripheral?

Related

Core Bluetooth: Cannot get iPhone (the central) and Mac (the peripheral) to pair

I am developing an iPhone application and a Mac application that communicate with each other via Bluetooth LE. The iPhone is the central, and the Mac is the peripheral. I would like the iPhone application to be able to reconnect to the Mac application after a relaunch. Therefore, I save the UUID of the peripheral representing the Mac in NSUserDefaults, and then when the iPhone app launches, I call -[CBCentralManager retrievePeripheralsWithIdentifiers:], passing in the UUID. Although this method returns the peripheral, when I call -[CBCentralManager connectPeripheral:options:], the iPhone never reconnects to the Mac. According to this SO post, the problem is that the two devices need to be paired so that the UUID of the Mac is persistent. This brings me to my problem. I cannot for the life of me get the iPhone and the Mac to pair. According to this page of the Core Bluetooth Programming Guide, the way to require a paired connection is for the peripheral to set the characteristic's properties and permissions to the appropriate values. I quote:
You can ensure that only trusted devices have access to sensitive characteristic values by setting the appropriate characteristic properties and permissions. To continue the example above, to allow only trusted devices to retrieve a member’s email address, set the appropriate characteristic’s properties and permissions, like this:
emailCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:emailCharacteristicUUID
properties:CBCharacteristicPropertyRead
| CBCharacteristicPropertyNotifyEncryptionRequired
value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
However, this does not work. Even when I set the correct permissions and properties for the characteristic, the iPhone can still read/write to it without a pairing dialog being displayed. Elsewhere I am told that the way to initiate pairing is for the peripheral to reject a read/write request with an insufficient authentication error. According to this post,
To pair, you need to respond to a write request with an insufficient authentication error. For example, for an iOS peripheral you would write something like:
- (void)peripheralManager:(CBPeripheralManager *)peripheralManager didReceiveWriteRequests:(NSArray *)requests {
...
[peripheralManager respondToRequest:request withResult:CBATTErrorInsufficientAuthentication];
...
}
But this still doesn't work! When the Mac rejects the write request with an insufficient authentication error, the iPhone receives a -[CBPeripheralDelegate peripheral:didWriteValueForCharacteristic:error:] callback with an error that says, "Authentication is insufficient." No pairing dialog. I have no idea what to do and am considering dropping Core Bluetooth altogether if I cannot get this to work. If you have any idea how to get this to work, please let me know.
From Apple DTS:
"I have checked with other engineers here and we don’t believe that the pairing popup is possible between two iOS devices, but an iOS device and a BLE peripheral."
So, pairing popup happens only between a iOS/Mac app and a peripheral. rdar time.

Is it possible to pair with device constant pair key using core bluetooth

In my iPhone application I would like to communicate with a firmware device. During a connection they can to ask for a pairing key but they said they are not having provision to provide what is the key to enter.
In this situation USP(User Smart Phone) never knows the key to enter. What shall we do to communicate with device ?
they said they are not having provision to provide what is the key to enter
I'm not sure I completely understand this sentence but if the user is given a device that has a specific pin/key, then that has to be documented somewhere for them. This is the same situation as it is with a SIM card's PIN. That code is set by the manufacturer and the user gets to know it when she buys the card. If the code is the same for all devices, then you can add it to your application and show it to the user in some way.
The iPhone application cannot dismiss or enter any value in the pairing dialog, this is sure.

External Device reports no Protocol

I have an app that I have been working on and I am now at the point that I want to integrate some interaction with an external device via the dock connector. The device that I am using (the iDive 300) conforms to the Made For iPod program. I have written a separate simple app based on the EADemo code to gather information about the device. However, when I run this app the iDive reports nothing for the Name, Serial Number, Firmware, etc and also says that no protocols were found. I know this simple app is working correctly because I have connected to several other external devices and the Name, serial number, etc is populated for each device.
The other odd thing is that the iDive seems to work properly when plugged into my iPhone 4 (i.e. it will increase the volume and play songs and videos found on my iPhone via the buttons on the device). Shouldn't this mean that some protocol is in place for this device to communicate with the iPhone? Is it all possible for me to read data from this device (e.g. capture when the 'play','menu', or other buttons are pressed) if I don't know the protocol?
I am completely new to the External Accessories framework and any help is appreciated!
You should autopsy the app that is associated with iDive. In its info.plist, there should be a key:
UISupportedExternalAccessoryProtocols
whose value is an array(See this for the formal definition), and within it lies a string whose value looks like a reversed domain name, as the device protocol.
You should edit your Info.plist, add the key('Supported external accessory protocols' in plain English) for an array, put in the protocol as its item.
There's no guarantee that an accessory actually uses EA. It may communicate using the protocols defined by Apple.
EA is only necessary if you want to communicate using your own proprietary protocol.
Check the EADemo example from Apple... If it doesn't show up in the demo app, it's not EA.

Find focus in OS X

Is it possible to create an app for the mac (and iphone afterwards) that does something when it detects that the focus is on a certain object in the screen?
Meaning, the program runs in the background, and when it detects that the focus (or cursor) is on an edit box, it will run something.
Hopefully I made myself clear!
Thanks!
You can do this on the mac by using the Accessibility Framework.
Note that users will have to manually enable assistive devices and you will not be able to distribute your app on the Mac App Store due to Apple's soon-to-be-implemented sandboxing restriction.
On iOS, you can detect focus to certain but not all elements using specialized delegate methods such as textViewDidBeginEditing:. That said, as users use taps to navigate iOS apps most of the time, simple tap handling seems like a much better approach.
On the iPhone, you can only detect focus within your own app, there's no way to observe other apps from the background.
On the Mac, as 0x90 noted, the closest you'll get are the Accessibility APIs. The UIElementInspector sample code may help you to get started.

Can i check to see if the iPhone is docked?

I wish for my app to act differently when the phone is docked or otherwise connected to the computer. Is this possible?
I couldn't find anything offhand about whether it's docked, but if you look at the UIDevice class you'll find the batteryState property. If you enable battery monitoring in your app, you can check to see if the property is set to UIDeviceBatteryStateUnplugged, implying the device isn't docked. If it's another value (UIDeviceBatteryStateCharging or UIDeviceBatteryStateFull), the phone is connected to power.
Note this doesn't guarantee there's a computer at the other end; it could just as easily be a power adapter into a wall or other USB-powered device.
You can also monitor accessory connection changes using EAAccessoryManager class method 'registerForLocalNotifications' and adding observer for notifications EAAccessoryDidConnectNotification, EAAccessoryDidDisconnectNotification.for more reference see apple docs EAAccessoryManager