Bluetooth Communication(IOS Core Bluetooth)

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

Major players in Bluetooth communication:

  1. Central
  2. Peripheral

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

A peripheral typically has data that is needed by other devices

Data structure on Peripheral:

  1. service
  2. characteristic

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).

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.


  • CBCentralManager
  • CBCentralManagerDelegate
  • CBPeripheralDelegate


  • CBPeripheralManager
  • CBPeripheralManagerDelegate


  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. 找到周遭的藍芽裝置:
    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:
    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:
  12. 讀取某個characteristic的值:
    peripheral.readValue(for: characteristic)
  13. 獲取到某個characteristic的值:
    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
  14. 對某個characteristic寫入值:
    peripheral.writeValue(dataVal, for: characteristic, type: CBCharacteristicWriteType.withResponse)
  15. 獲取某個characteristic已被寫入的資訊:
    func peripheral(CBPeripheral, didWriteValueFor: CBCharacteristic, error: Error?)