Swift开发iOS应用:TI CC2650模块蓝牙连接失败问题求助
Hey there! Let's tackle this CC2650 and iOS Core Bluetooth connection issue step by step—since you're adapting an HM10-focused codebase, there are key differences between the HM10 (a simplified BLE UART module) and TI's CC2650 that might be causing the problem.
1. Verify CC2650 BLE Advertising Configuration
The HM10 uses a straightforward advertisement packet, but the CC2650 might have custom settings that iOS can't detect:
- Ensure your CC2650 is set to connectable advertising mode. Non-connectable mode will make iOS ignore it entirely.
- Check the service UUID the CC2650 is advertising. The HM10 repo likely filters for the HM10's UART service UUID (
FFE0), but the CC2650 uses TI's default service UUIDs (e.g.,F000FFC0-0451-4000-B000-000000000000for its UART service). Update your scan filter in theBluetoothSerialclass to match this UUID.
2. Adjust Core Bluetooth Scan & Connection Logic
The HM10 code may have hardcoded behaviors that don't work for the CC2650:
- In the
centralManager(_:didDiscover:advertisementData:rssi:)delegate method, make sure you aren't filtering out the CC2650 by name. The module's default name might be something like "TI SensorTag" (or a custom name you set)—double-check it matches what your app expects. - When calling
connect(_:options:), try adding connection flags to improve reliability:centralManager.connect(peripheral, options: [ CBConnectPeripheralOptionNotifyOnConnectionKey: true, CBConnectPeripheralOptionNotifyOnDisconnectionKey: true ])
3. Fix GATT Service Discovery Setup
Unlike the HM10's simple UART service, the CC2650 requires proper GATT delegation setup:
- Immediately set the peripheral delegate after connecting. If you skip this, your app won't discover any services/characteristics, making it seem like the connection failed:
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { peripheral.delegate = self peripheral.discoverServices([yourCC2650ServiceUUID]) } - Confirm the CC2650's GATT server is configured correctly (if you're using TI's BLE Stack). Ensure the required services (like UART) are initialized and set to allow connections.
4. Debug with Core Bluetooth Logs
Use iOS's built-in tools to pinpoint the issue:
- Enable Core Bluetooth Debug Logging on your test device: Go to Settings > Developer > Core Bluetooth Debug Logging. Check Xcode's Console for scan results, connection attempts, and error codes (e.g.,
CBErrorConnectionTimeoutorCBErrorPeripheralDisconnected). - Test with TI's official SensorTag app first. If it connects to the CC2650, the problem is in your app's code, not the module itself.
5. Quick Code Tweaks for CC2650
Here's a adjusted snippet for your BluetoothSerial class to align with the CC2650:
import UIKit import CoreBluetooth protocol BluetoothSerialDelegate: AnyObject { // Add your delegate methods here } final class BluetoothSerial: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { weak var delegate: BluetoothSerialDelegate? var centralManager: CBCentralManager! let cc2650UARTServiceUUID = CBUUID(string: "F000FFC0-0451-4000-B000-000000000000") override init() { super.init() centralManager = CBCentralManager(delegate: self, queue: nil) } func startScanning() { guard centralManager.state == .poweredOn else { return } centralManager.scanForPeripherals(withServices: [cc2650UARTServiceUUID], options: nil) } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { // Filter for your CC2650's name if needed if peripheral.name == "YourCC2650Name" { centralManager.stopScan() centralManager.connect(peripheral, options: [ CBConnectPeripheralOptionNotifyOnConnectionKey: true ]) } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { peripheral.delegate = self peripheral.discoverServices([cc2650UARTServiceUUID]) } }
内容的提问来源于stack exchange,提问作者Ali Atay

