1

I'm trying to establish a connection between my PC running Ubuntu and my iPhone via Bluetooth automatically when it becomes available, after being manually paired beforehand. I've seen this to be possible with certain peripherals, mainly audio. For example, my phone will automatically connect to a Bluetooth speaker when it is turned on and Bluetooth is active on my phone; another example is my phone automatically connects to my car's radio system via Bluetooth when I turn the car on.

I'm not able to connect my phone to my PC without first initiating the connection from the smartphone's Bluetooth menu. I'm thinking that I could possibly write an application for the PC to attempt to connect to the device every few minutes or something, but it seems that the phone needs to be the device to initiate the connection.

The only information that I need for what I'm trying to do ultimately is that the devices can pair successfully. Essentially I'm trying to build a sort of proximity trigger between my phone and my PC without using Wi-Fi and GPS - I can't use these for some specific reasons.

Is there any way to make this happen?

jordan
  • 23
  • 5
  • Is this for classic Bluetooth or Bluetooth Low Energy (BLE)? For classic Bluetooth there is still limited support on the iOS side compared to BLE so it will be a bit more challenging. – Youssif Saeed Jul 26 '21 at 06:25
  • It can be either, really. All I'm looking to do is to see if a connection is made, and to get the MAC address of the connected smartphone. – jordan Jul 26 '21 at 15:51

1 Answers1

1

Yes this should be doable as long as you use the Background Processing feature for iOS apps. In the example I'll give below, we'll have the PC be the peripheral and the phone be the central, but you can really have it working either way. You will need to do the following:-

  • First initial connection needs to be performed in the foreground (this is due to iOS's background limitations).
  • On the iOS side, you need an application that acts as a central that scans and connects to the remove device (check this example as a starting point).
  • Upon connection, you need to bond with the PC. Bonding is important as it will prevent you from having to do the pairing again in the future. However, pairing/bonding is managed by the iPhone's OS so you cannot write it in your application, so the workaround is to have an encrypted characteristic on the PC side that will force the iPhone to bond (this is covered later).
  • On the PC side, you need to have a BlueZ script that acts as a peripheral that is always advertising. You can do this using bluetoothctl (check the examples here and here).
  • Before you start advertising, you need to have a GATT server on the PC side (to do this, check this example).
  • When registering characteristics, ensure that one of them has the encrypt-read property (you can find a full list of the properties here).
  • Now when you attempt to read this characteristic from the iOS side, the two devices should bond (make sure that your PC is bondable which you can do this via these commands).
  • Once the devices are paired, your iOS app needs to be working in the background constantly scanning and attempting to connect to the same peripheral (have a look at this and this example).

You can find more useful information at the links below:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
  • Lots of good information here, thank you! I figured I would end up having to run a background service on the phone in the end. Going to test it out now – jordan Jul 27 '21 at 14:46