Bluetooth Communication(IOS Core Bluetooth)

IOS Core Bluetooth是based on BLE(Bluetooth low energy) protocol

Major players in Bluetooth communication:

  1. Central
  2. Peripheral

Central:
A central typically uses the information served up by peripherals to accomplish some particular task

Peripheral:
A peripheral typically has data that is needed by other devices

Data structure on Peripheral:

  1. service
  2. characteristic

Service:
A service is a collection of data and associated behaviors for accomplishing a function or feature of a device (or portions of that device).
Note: Services themselves are made up of either characteristics or included services (that is, references to other services).

Characteristic:
A characteristic provides further details about a peripheral’s service.

Note: 各別的Service和各別Characteristic用不同的UUID區分

The way that central and peripheral communicate:
After a central has successfully established a connection to a peripheral, it can discover the full range of services and characteristics the peripheral has to offer (advertising data might contain only a fraction of the available services).

A central can also interact with a peripheral’s service by reading or writing the value of that service’s characteristic. For example, your app may request the current room temperature from a digital thermostat, or it may provide the thermostat with a value at which to set the room’s temperature.

Central會用到的class和delegate:

  • CBCentralManager
  • CBCentralManagerDelegate
  • CBPeripheralDelegate

Peripheral會用到的class和delegate:

  • CBPeripheralManager
  • CBPeripheralManagerDelegate

Central和Peripheral溝通流程:(以Central的角度來看)

  1. 初始化centralManager
    centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main, options: [CBCentralManagerOptionShowPowerAlertKey: false])
  2. 檢查目前的藍芽狀態:
    Callback: centralManagerDidUpdateState
    (p.s. 藍芽狀態可能是: On/Off/resetting/unauthorized/unknown/unsupported)
  3. 開始掃描peripheral:
    centralManager?.scanForPeripherals(withServices: nil, options:[CBCentralManagerScanOptionAllowDuplicatesKey: true])
  4. 找到周遭的藍芽裝置:
    Callback:
    centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
  5. 連結指定的peripheral:
    centralManager.connect(peripheral, options: nil)
  6. 通知連到指定的peripheral: func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
  7. 尋找指定的service: peripheral.discoverServices([CBUUID(string: BluetoothConstants.service_uuid)])
  8. 找到指定peripheral上的services:
    Callback:
    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
  9. 尋找service中的characteristics:
    peripheral.discoverCharacteristics(nil, for: service)
  10. 找到指定service包含的characteristics:
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
  11. 設定指定的characteristic為notify:
    peripheral.setNotifyValue(true, for: characteristic)
    Note: the peripheral then calls the peripheral(_:didUpdateValueFor:error:) method of its delegate object whenever the characteristic value changes 補充 1: CBCharacteristicProperties: read/write/writeWithoutResponse/notify/… 補充 2: https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html
  12. 讀取某個characteristic的值:
    peripheral.readValue(for: characteristic)
  13. 獲取到某個characteristic的值:
    Callback:
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
  14. 對某個characteristic寫入值:
    peripheral.writeValue(dataVal, for: characteristic, type: CBCharacteristicWriteType.withResponse)
  15. 獲取某個characteristic已被寫入的資訊:
    Callback:
    func peripheral(CBPeripheral, didWriteValueFor: CBCharacteristic, error: Error?)

Ref:
https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothOverview/CoreBluetoothOverview.html#//apple_ref/doc/uid/TP40013257-CH2-SW17
https://www.jianshu.com/p/38a4c6451d93