Мне нужна помощь в следующем: есть приложение, позволяющее настроить WiFi через адаптер Bluetooth. Я хочу переписать приложение из http://www.berrylan.org/ в выделенный проект swift.
Для начала мне нужно отправить команду определенному uuid, чтобы устройство давало мне локальные сети. См. Информацию здесь: https://github.com/nymea/nymea-networkmanager
Что работает: Connect + Discovery + Получение характеристик
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
print("error: \(error)")
return
}
let services = peripheral.services!
print("Found \(services.count) services! :\(services)")
for service in services {
if (service.uuid == CBUUID(string: "E081FEC0-F757-4449-B9C9-BFA83133F7FC")) {
peripheral.discoverCharacteristics(nil, for: service)
return
}
}
}
Что не работает:
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let error = error {
print("error: \(error)")
return
}
print("name " + peripheral.name!)
let characteristics = service.characteristics!
print("Found \(characteristics.count) characteristics!")
for characteristic in characteristics {
print("Found \(characteristic.uuid)!")
if characteristic.properties.contains(.write) {
characteristicx = characteristic
if (characteristic.uuid == CBUUID(string:"e081fec1-f757-4449-b9c9-bfa83133f7fc")) {
var str = inputText.text ?? ""
str = "{\"c\":0}\n"
guard let strData = str.data(using: .utf8) else {
return
}
peripheral.setNotifyValue(true, for: characteristic)
peripheral.writeValue(strData, for: characteristic, type: .withResponse)
return
}
}
}
}
Эта функция должнаТриггер didWriteValueFor и это делает, но он дает ноль в момент, когда он печатает значение:
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
print(characteristic.value)
}
Он должен дать мне ответ на печать, как это:
[{
"c": 0, // Integer: Command: describing the method called
"r": 0 // Integer: Response error code. See list of response error codes.
"p": [
{
"e": "My network", // String: ESSID: the name of the wifi network
"m": "AA:BB:CC:DD:EE", // String: The MAC address of the wifi access point
"s": 86, // Integer: Signal strength [0-100] %
"p": 0 // Integer: Protected [0,1]; 0 - open, 1 - protected
}
]
}]