私はAdafruitのnRF8001を使ってArduino経由でiOSデバイスに接続するためのチュートリアルを読んでいます。これまで私はすべてを正しく設定しており、アプリでうまく動作します。
私は自分のアプリを書こうとしていますが(今のところ)まったく同じことをしています。私はチュートリアルを読んでいます。私ができる限りコードをコピーしたことを理解しています。接続後
はここのために私のコードです:物事が働いているように見える(UIワイズ)、しかし私は、デバイスへの接続を過ぎて何もすることができないよう、あなたが見ることができるように、私が呼び出していますSwift BLE "didDiscoverServices"が実行されていません。何か不足していますか?
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
//What to do when it discovers a peripheral, add it to the array list
print("Peripheral found: " + (peripheral.name ?? "Unknown Name"))
peripheralsFoundNames.append((peripheral.name ?? "Unknown Name"))
peripheralsFoundData.append((advertisementData.description))
peripheralsFoundCB.append(peripheral)
peripheralsFoundRSSIs.append(RSSI)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected to device!")
displayStatusAlert(localmsg: "Connection Succesful!")
NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_READY_KEY), object: self)
data?.length = 0 //clear any data that might be stored
peripheral.discoverServices([BLETemperatureService])
print("Here at didConnect, connected to:" + peripheral.name!)
// Here needs to add code to check if it's a single or multi-channel device via the advertisement data or some other constant, maybe the name?
}
明示的にperipheral.discoverServices、そして私は実行するprint文を持っています。それから私は、以下のLINES OF(以下NOTEなしがANY TIME(少なくともprint文)で実行さているように見えるん持っている:。
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("here at diddisoverservices")
if ((error) != nil){
displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown"))
}
guard let services = peripheral.services
else{
return
}
for service in services {
peripheral.discoverCharacteristics(nil, for: service)
}
print ("Discovered!")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if ((error) != nil){
displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown"))
}
guard let characteristics = service.characteristics
else{
return
}
for characteristic in characteristics {
//looks for the right characteristic
print("looking for characteristic")
if characteristic.uuid.isEqual(BLERXCharacteristic) {
deviceConnectedRXChar = characteristic
//Once found, subscribe to the this particular characteristic
peripheral.setNotifyValue(true, for: deviceConnectedRXChar!)
peripheral.readValue(for: characteristic)
print("Rx Characteristic: \(characteristic.uuid)")
}
if characteristic.uuid.isEqual(BLETXCharacteristic){
deviceConnectedTXChar = characteristic
print("Tx Characteristic: \(characteristic.uuid)")
}
peripheral.discoverDescriptors(for: characteristic)
}
print ("Characteristic discovered")
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if characteristic == deviceConnectedRXChar {
if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) {
receivedDataString = ASCIIstring
print("Value Recieved: \((receivedDataString as String))")
NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_SENT_DATA), object: nil)
}
}
だから
peripheral.discoverServices([BLETemperatureService])
を行う前に、あなたがperipheral.delegate = self
を行う必要がありを
CBPeripheral
オブジェクトdelegate
を設定あなたは周辺機器についての強い参照を持っていますか?第二:もし 'peripheral.discoverServices(nil)'をやっているのであれば、それは機能しますか? – LarmedidDiscoverPeripheralコードを追加しました。さらに、(nil)で試しても同じ結果が得られました。 "hered diddiscoverservices"という印字は実行されません。 –
'peripheralalsFoundCB.append(peripheral)'の後、 'central.connect(周辺機器、オプション:[]) 'それを発見しましたが、接続を開始しませんでした。デバイスには同時接続が限られています。何百ものデバイスを搭載している環境であれば、そのようなことができます。 – Larme