I am looking for equivalent of inputStream, OutputStream and BluetoothSocket in Swift.
What do we use when we are coding with Swift instead of those things in Android?
I have an app for Android now I want to create for iOS as well.
CodePudding user response:
As @Sulthan already commented:
Simply said, there are no direct counterparts. You cannot rewrite the functionality 1-1 only changing names of classes.
- For BLE devices use Core Bluetooth framework.
- For Bluetooth Classic you need to use External Accessory framework (only for MFI accessories).
CodePudding user response:
Typically Android's BluetoothSocket is used with SPP, which is not available on iOS. How you need to redesign this depends on what you're doing, what your firmware offers, and how much control you have over the Bluetooth device's development.
For MFi(*) devices, iAP2 (via ExternalAccessory) is very similar to SPP, so the same basic protocol and designs can be used. But in most cases, you have to develop a completely different protocol (most commonly over BLE GATT). CoreBluetooth and the class you've listed (CBPeripheral, etc) are how you interact with BLE GATT.
You can also dig into L2CAPP. It provides an interface similar to SPP, but I haven't worked with any firmware ADK that supports using it as an alternative.
(*) MFi is "Made for iPhone," and requires an extra chip in the hardware and has to go through Apple certification. It's worth looking into if you're building your own hardware. It gives you a few extra capabilities, though not nearly as many as people expect.