我想在IOS8中使用CoreBluetooth.framework来实现数据传输,我确实在以下方法中发现了外设并尝试连接外设.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discover name : %@", peripheral.name);
[self.centralManager connectPeripheral:peripheral options:nil];
}
Run Code Online (Sandbox Code Playgroud)
但它没有调用委托方法didFailToConnectPeripheral或didConnectPeripheral ,我对它有什么问题,是代码错误还是IOS8需要一些额外的东西?怎么处理,谢谢!
这是我在github中的代码,我写了一个程序作为服务器,另一个是中央.
首先要做的事:运行OSX 10.10.4,iOS 4,Xcode 6.3.2,iPhone 6,Swift
简短的故事:我在这里有一个蓝牙LE设备,我希望在特征值改变时收到通知,例如通过用户输入.尝试订阅它不会成功,而是会产生错误Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found."
说来话长:所以,我有我只要我开始扫描外设BluetoothManager类$CBCentralManager.state是.PoweredOn.这很容易,我甚至是一个好公民,专门为有我想要的服务的人扫描
centralManager.scanForPeripheralsWithServices([ServiceUUID], options: nil)
Run Code Online (Sandbox Code Playgroud)
希望这会成功,我实现了以下委托方法:
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
if *this is a known device* {
connectToPeripheral(peripheral)
return
}
[...] // various stuff to make something a known device, this works
}
Run Code Online (Sandbox Code Playgroud)
所以继续前进,我们得到:
func connectToPeripheral(peripheral: CBPeripheral) {
println("connecting to \(peripheral.identifier)")
[...] // saving the peripheral in an array along the way so …Run Code Online (Sandbox Code Playgroud) bluetooth ios core-bluetooth bluetooth-lowenergy ios-bluetooth
我正在尝试从带有 CoreBluetooth 的 iPad 连接到 MacBook Pro。
这是我的代表团CBCentralManagerDelegate:
extension MasterViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
print("Scanning for peripherals")
central.scanForPeripherals(withServices: nil, options: nil)
Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.stopScan), userInfo: nil, repeats: true)
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Did discover peripheral", peripheral)
central.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Did connect to ", peripheral)
peripheral.delegate …Run Code Online (Sandbox Code Playgroud)