我有一个可以正常工作的BLE应用程序,甚至还存档了它的多个工作版本。突然,连接设备后我没有收到回调。当用户点击已扫描设备的ListView中的条目时,我连接到了骨膜周围。另一个奇怪的事情是,在分析问题时,我从发出连接呼叫的情况开始,先收到回叫,然后立即断开连接回叫。因此,我进行了一个项目的清理和重建。然后,connect回调不再完全被调用。我回到该应用程序的已知工作版本,它们执行相同的操作(不会发生连接回调)。今天,我被迫在iPad和Mac上更新OS和Xcode的更新,但同样的事情发生了。任何帮助将不胜感激。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CBCentralManagerDelegate, CBPeripheralDelegate, UITextFieldDelegate
{
var btManager: CBCentralManager!
var btDeviceList: [CBPeripheral] = []
var btDevice: CBPeripheral!
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
{
print("Found BLE device: " + peripheral.name!);
// Add the scanner only if its not there already
var i = 0
var foundIt = false
for tempDevice in btDeviceList
{
if tempDevice.identifier == peripheral.identifier
{
foundIt = true
break
}
i = i + 1
}
if (!foundIt)
{
btDeviceList.append(peripheral)
btListView.reloadData()
}
}
// Called when connected to a BLE device
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
{
print("Connected to: " + peripheral.name!)
// Discover the services for the device
peripheral.discoverServices(nil)
}
// Called when a row in the table list view is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
btManager.stopScan()
btDevice = btDeviceList[indexPath.row]
btDevice.delegate = self
print("Connecting to: " + btDevice.name!)
btManager.connect(btDevice, options: nil)
}
} // View Controller