iOS to Mac OS X [core] bluetooth data transfer - ios

My objective here is to create a connection between a device running iOS to a device running Mac OS X, via bluetooth. I know that I might be able to use CoreBluetooth for this but I don't understand how since I don't see a method to setup a service on the iOS device and broadcast it as an available service for a device running Mac OS X. In other words, I simply want to setup a connection to get the iOS device to send data to the Mac OS X device. Also, how would I go about to specify how to setup properties of the service. I've tried researching this stuff but most of the documentation makes little sense to me. The data transfer is small so bluetooth is good enough for the job. I'm trying to avoid Bonjour for this, and the Game center framework for P2P since OS X can't handle that (I think).

In iOS6 the iPhone 4S, 5 and New iPad can work both as Peripheral and as Central in Bluetooth Smart / Low Energy mode.
Try downloading "LightBlue" APP from APP Store. It let's you put the iPhone4S or 5 into Peripheral mode with random Services which you can then read from the Mac (if you have a newer one with BT Low Energy, I use the Retina for that but also the new iMac and Mac Mini got BT Low Energy).
You are correct that Core Bluetooth only give access to Bluetooth Low Energy which doesn't allow for the MFI chip.

Just ran across this today... and just wanted to give another answer to anyone needing to communicate to a non-Bluetooth LE device from your Mac.
The way to go is with IOBluetooth, and IOBluetoothUI.
They are both frameworks for the Mac, and they allow you to communicate with both old bluetooth, and Bluetooth LE 4.0, I believe. Also, I'm pretty positive you can act as a Central and Peripheral Device using this framework.

Related

Test bluetooth connection between iOS simulation in Xcode and a physical device? [duplicate]

I'm trying some iOS test applications on the new Mac mini, that supports Bluetooth Low Energy. The CoreBluetooth framework is used in those. However, I'm not able to get Bluetooth working in the iPhone simulator, that is part of Xcode.
When I allocate a new CBCentralManager, centralManagerDidUpdateState: receives CBCentralManagerStatePoweredOff which stands for Bluetooth is currently powered off.
The first time I ran the text application, a box was then opened up that looked pretty bugged (only language variables were used, not the actual texts) and with two buttons. The first led me to the settings panel where there was an option to enable Bluetooth. However, after I told it to enable Bluetooth, it just shows the spinning animation, and it won't either complete or cancel. Even after rebooting the Mac, there is still only the spinning animation. The box also does not open up anymore.
Essentially, I think that the iPhone simulator should support Bluetooth Low Energy. Otherwise, it does not really make sense that there is an option in the settings application on the simulator. Also, the central manager state is CBCentralManagerStatePoweredOff but not CBCentralManagerStateUnsupported which would stand for The platform doesn't support Bluetooth Low Energy. This gives also hope that support could exist.
Does the iPhone simulator support Bluetooth Low Energy?
If yes, how can I enable it?
The simulator does support Bluetooth Low Energy (4.0) according to this appnote from Apple. The only problem is that even if you have a computer with BLE inside, you will not be able to use the simulator together with it, because (I think) you occupy the availability on BLE for other devices to discover your computer, thereby restricting the functionality of the Mac.
So if you go get yourself a BLE USB dongle you will be able to use it in simulator.
EDIT: Adding information from #JoeShaw:
Unfortunately it appears as though Core Bluetooth support has been dropped from the simulator for iOS 7. Reference: doubleencore.com/2013/09/whats-new-in-bluetooth-le-ios-7. In addition, the linked technote seems to have been removed.
I have been using the simulator to test BLE apps - but you need to be on OSX 10.7. As Wilhelmsen mentioned, you also need a BLE USB dongle. In addition, you need to set an NVRAM setting:
$sudo nvram bluetoothHostControllerSwitchBehavior="never"
See this Technical Note from Apple for more details on using the simulator to test BLE apps: http://developer.apple.com/library/ios/#technotes/tn2295/_index.html
According to http://www.doubleencore.com/2013/09/whats-new-in-bluetooth-le-ios-7/, Core Bluetooth support has been dropped from the simulator as of iOS 7. I haven't figured out why yet, but it means you will need to test on real hardware in the future.
It also appears as though Apple has removed Tech Note 2295, as I get redirected when I hit the URL.
I've found that Apple writes in their own samples that the simulator cannot be used to test Core Bluetooth-based applications.
Important:
This project requires a Bluetooth LE Capable Device (Currently only the iPhone 4S) and will not work on the simulator.
I've been working on a BLE app for the past week and can say with confidence that no, the simulator does not support BLE unless the mac that your using has BLE(Macbook Air) - but even then i'm not 100% sure how to enable this(most likely by just turing on BLE on your mac and on the simulator).
If you don't have a BLE enabled mac, have to find yourself a 4s to do testing with!
One alternative approach to using CoreBluetooth in the Simulator is to use Nordic Semiconductor's CoreBluetooth-Mock library, which allows you to stub-out CoreBluetooth and provide simulated/mock peripherals for use when working with the simulator:
https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock
It does require some minor code changes, but the interface it provides is almost identical to that of CoreBluetooth.
It's also great for writing integration tests too.
Actually I tried in the simulator and does not work.
The simulator does not recognize if you have a Bluetooth 4.0 in your computer but if you are using Command line tool projects in Xcode then it works. just for the cases you want to create something in Command line tool.

Connecting an iPhone app to a PC app via Bluetooth

I am currently researching the possibilities of connecting an iPhone app to a windows app via Bluetooth.
The Windows App is written in C# and uses Serial ports to transfer Data. Windows allow me to connect a serial port to Bluetooth. This setup allows communication via Serial over Bluetooth.
I know this setup works because I have managed to communicate with this setup from an Android phone.
I don't seem to be able to do the same with an iPhone 5s (I can't even seem to pair the iPhone to the Windows PC). I haven't been able to test this with a more recent iPhone, but I don't expect it to work either since it's an Apple device with a Windows machine.
I've done some research regarding the possibilities of connecting an iPhone to something else (like a PC) over Bluetooth, and it doesn't seem to be possible.
I have found this SO answer which seems to say that I cannot use Bluetooth Classic to connect an iPhone to a PC.
Since I can't seem to be able to use Bluetooth Classic, I have also tried to use BLE, but I haven't worked out how to simulate the PC as a "peripheral device" to connect to it from the iPhone. I don't even know if that's possible.
So basically my questions are:
1) Is it possible to communicate via Bluetooth between an iPhone and a PC?
2) Would it be possible to use some sort of adapter for the iPhone to connect to the PC (For example, an Arduino with a Bluetooth chip, acting as a BLE peripheral, and sending the data to the PC?)
3) What alternative would I have to this? I know WiFi might work, but I am uncertain if the PC running the app will be network enabled when the project goes live.
You can connect an iPhone with PC over Bluetooth using BLE.
I tried with Linux machine (Ubuntu 16.04 with BLE dongle 4.0) which acts as BLE Peripheral and iPhone 8 with BLE 5.0 as central application. Both could able to successfully communicate using custom GATT service that we developed. The throughput i observed here was 2.5 kiloBytes per sec.
If you are so specific on Windows to act as BLE Peripheral, then i believe there are apps available in Windows store to download which enables Windows PC to act as peripheral. Once peripheral mode is enabled, you can always communicate to this over BLE using iPhone [recommended latest phones]. But you need to try this out.

Constantly Open BLE Connection Between OS X and iOS

I have been exploring CoreBluetooth and BLE connections between OS X and iOS, where I've discovered an interesting problem. I have the Mac implementing CoreBluetooth central and iOS implementing CoreBluetooth peripheral. Since iOS no longer broadcasts a constant, consistent MAC address or other identifier, there is no reliable way (that I can find) to allow the Mac to connect to the iOS peripheral whenever it is in range, including when the iOS app is running in the background.
By storing the last connected peripheral, I can get OS X to constantly search for the iOS device like shown:
[central connectPeripheral:self.lastPeripheral options:nil];
Which works if self.lastPeripheral was previously connected to the OS X client. This initial connection can only be created if the iOS app is open at the time; it will not work if it is backgrounded. This in itself is not a horrible issue, but every single time the OS X app quits, this connection must be reinstated by opening the iOS app.
Is there any way to connect and reconnect (when the device moves back into range) via BLE to an iOS device when the iOS application is backgrounded, especially to persist between app launches?
BluetoothLE is still a energy consuming technology. And Apple goes into great lengths to limit its "active" usage on iOS, in situation they don't deem as correct (e.g. background). That said, there are no guaranties on for how long a iOS device in peripheral mode continues to advertise (pre-requirement for reconnection).
Did you think about switching the roles. Making the OS X machine the peripheral, and the iOS device the central? I know from experience that iOS behaves much nicer when acting as a central.

Can I make my Mac as a Bluetooth Peripheral?

I'm working on an idea which requires my Mac to send some data to my iOS device. I know CoreBluetooth framework is not there for Mac OS and only for iOS, so is there any way that I can get my Mac to pose a peripheral ?
I've done some Googling around - haven't got a straight answer :(
The CoreBluetooth.Framework is there for OSX. It's embedded inside of the IOBluetooth.framework. Download the sample mac apps from the Apple Dev Site. The one difference is that the OSX version of the CoreBluetooth.Framework does not contain the CBPeripheralManager class. You should just have your iOS device act as the peripheral and the Mac as the central. The iOS device can act as both at the same time and data can be sent both ways.
If you are comfortable with writing JavaScript, you can use bleno. It is a node.js module for implementing BLE (Bluetooth Low Energy) peripherals, and it support Mac OS X 10.9 or later, Linux and Windows.
You can find an example for implementing a Battery Service in peripheral mode in their github repository:
https://github.com/sandeepmistry/bleno/tree/master/examples/battery-service

Exchange strings between Mac OS X and iOS via Bluetooth

For a project, I have to send informations such as strings or ints from an iOS app (on iPhone 5) to a Mac OS X app (on a MacBook Pro), while both the iPhone and the MacBook Pro are in place without Wifi. The obvious solution is using Bluetooth. So the question is: Can I do that, and how?
You should use the CoreBluetooth framework.
For Mac: you'll need either a Macbook Air or Mac-mini
For iOS: you'll need an iPhone 4s, iPhone 5, iPad 3, iPad 4, or new iPod touch.
--A short overview:
You'll need to host both the CBCentralManager and the CBPeripheralManager on one device, and then host just the CBPeripheralManager on the other device.
The CBCentralManager is responsible for connecting and maintaining connections to external devices.
The CBPeripheralManager is responsible for writing and reading from those connections.
You can either send a write/read request or you can advertise a custom CBService that contains a CBCharacteristic with your string/int value.
Look at the header files and docs because there are tons of methods you need to adopt. There's also really good WWDC2012 videos that illustrate these methods in depth.
(However, if you want to use just a Macbook Pro, you'll need to use the standard IOBluetoothFramework on Mac and external accessory framework on iOS )
This project should help you with the iOS side of sharing strings over Bluetooth: SimpleShare
It lets you easily send and receive arrays of strings from your iOS device over Bluetooth LE. You'd just need to manage the CBCentralManager on the Mac.

Resources