This document provides a brief overview of how Cellular works. You should read it before exploring the code. Cellular uses a MVVM (Model–view–viewmodel) architecture for the Android companion app, and a Model/View architecture for the macOS main app.
For the main app, the BluetoothModel
controls incoming connections from the companion and sends notifications. The WLANModel
controls the usage of Wi-Fi to connect and disconnect from hotspot, and scan for available networks. The LocationModel
is just used to grant the Location Services permission.
For the companion app, the BluetoothModel
initiates outgoing connections to the main app and receives notifications. There are view models for each view to manage the business logic of each view. The BluetoothService
is a Service that runs all the time in the background to keep the Bluetooth connection alive and listens to signal, network type and battery changes on the phone.
Central = GATT Client
Peripheral = GATT Server
Notes:
- The Mac is always the peripheral and the Android device is always the central (this is to bypass a limitation of Android devices not being able to advertise for longer than a set period).
- All user data sent should be encrypted with the shared PIN unless otherwise stated.
- All peripherals should advertise the encrypted "Hello world" message as a secondary form of verification besides the UUID/MAC address.
- There are two characteristics, the command and the notification characteristic. To send a command from the Android device to the Mac, the Android device writes to the command characteristic. To send a command from the Mac to the Android, the Mac writes to its notification characteristic. The Android device needs to be subscribed to notifications on the notification characteristic.
Command characteristic messages:
Message | Number |
---|---|
Hello world | 0 |
Sharing hotspot details | 1 |
Sharing phone info | 2 |
Connect to hotspot | 3 |
Disconnect from hotspot | 4 |
Unlink device | 5 |
Notification characteristic messages:
Message | Number |
---|---|
Enable hotspot | 0 |
Disable hotspot | 1 |
Indicate that hotspot is connected | 2 |
Enable see phone info | 3 |
Disable see phone info | 4 |
Unlink device | 5 |
Disconnect device | 6 |
- The Mac shows a QR code containing the randomised service UUID and a 6 digit PIN.
- The Android device scans the QR code, connects to the Mac, and remembers the service UUID and shared PIN.
- Bluetooth bonding is initiated (handled by system).
- The Android device shares the "Hello world" command with the Mac and the Mac stores the UUID of the Android device. From then, the Mac will always ensure that the UUID of the Android device matches the stored UUID.
- The Android device writes the hotspot SSID and password to the Mac's data characteristic (with command "Sharing hotspot details" attached).
- If the Mac receives the data and decrypts it successfully, the Android device is considered linked.
- The Android device writes the network signal strength, mobile data network type and battery level to the Mac's command characteristic (with message "Sharing phone info" attached).
- The Mac sends an "Enable hotspot" message to the Android device by updating the value of the notification characteristic if the Mac initiates this connection.
- The Android device turns on the mobile hotspot.
- The Android device writes the "Connect to hotspot" command to the Mac's command characteristic.
- The Mac receives the command and connects to the hotspot using the saved SSID and password.
If initiated on the Mac:
- The Mac sends a "Disconnect from hotspot" message to the Android device by updating the value of the notification characteristic.
- The Android device turns off the mobile hotspot if the hotspot was turned on by Cellular.
If initiated on the Android device:
- The Android device turns off the mobile hotspot if the hotspot was turned on by Cellular.
- The Android device writes the "Disconnect from hotspot" command to the Mac's command characteristic.
- The Mac disconnects from the hotspot.
If devices are connected:
- The Mac sends a "Unlink device" message to the Android device by updating the value of the notification characteristic, or the Android device writes the "Unlink device" message to the Mac's command characteristic.
- The Android device disconnects from the Mac, including unsubscribing from notifications, un-bonding from the Mac and clearing the data store and variables.
- When the Android device unsubscribes from the notification characteristic, the Mac changes the shared PIN and deletes the service UUID, stops advertising the service and clears all variables. However, settings are not cleared.
If devices are disconnected:
- The Android device un-bonds from the Mac and clears the data store and variables.
- The Mac changes the shared PIN and deletes the service UUID, stops advertising the service and clears all variables. However, settings are not cleared.