我有一个使用BLE连接到的设备。首先检查BLE是否打开,如果打开,则开始扫描。然后,在发现设备之后,我将外围设备存储在变量中,然后尝试连接到它。连接成功。之后,我开始发现服务和特性,并设法成功阅读它们。问题是,每当我尝试将数据写入设备时,我总是会收到此错误。
Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo={NSLocalizedDescription=The specified device has disconnected from us.
这是我的代码:
public func centralManagerDidUpdateState(_ central: CBCentralManager)
{
switch central.state {
case .unauthorized:
print("App does not support BLE")
case .poweredOff:
print("Bluetooth is turned off")
case .poweredOn:
print("Bluetooth is turned on")
print("Start scanning peripherals...")
central.scanForPeripherals(withServices: nil, options: nil)
default:
break
}
}
// To receive cell broadcast data
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
let device = Device();
if(device.fromScanData(peripheral: peripheral, name: peripheral.name, rssi: RSSI.intValue, advertisementData: advertisementData))
{
if(device.Name.isEmpty || device.Name.count == 0){
return;
}
if(device.SN.isEmpty || device.SN.count != 8){
return;
}
if self.device.SN == device.SN
{
if !isDeviceConnected
{
print("Stop scanning")
print("Connecting...")
self.peripheral = peripheral
self.peripheral?.delegate = self
bluetoothManager.stopScan()
bluetoothManager.connect(self.peripheral!, options: nil)
}
}
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected successfully");
print("Trying to get equipment services and features...");
device.Peripheral = peripheral
self.peripheral?.discoverServices(nil)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("Failed to connect");
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("Connection has been disconnected");
isDeviceConnected = false;
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?){
guard let services = peripheral.services else {
if error != nil
{
print("Did Discover Services Error:\(String(describing: error!))")
}
else
{
print("Empty services")
}
return
}
print("Device Services......");
print("-------------- ("+peripheral.identifier.uuidString+")Service Services information: --------------------");
print("Number of services:\(services.count)")
for service in services
{
print("---- Service UUID:\(service.uuid) -----");
self.peripheral?.discoverCharacteristics(nil, for: service);
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?){
guard let characteristics = service.characteristics else {
if error != nil
{
print("Did Discover Characteristics Error:\(String(describing: error!))")
}
else
{
print("Empty Characteristics")
}
return
}
print("Feature Characteristic UUID List:");
for characteristic in characteristics
{
print("UUID: \(characteristic.uuid)");
self.peripheral?.readValue(for: characteristic);
}
}
func checkToken() {
print("Start Writing")
device.IsSaveOverwrite = true
let characteristic = characteristicList[characteristicHandle.GetCharacteristicUUID(type: .IsSaveOverwrite)]
let data = characteristicHandle.GetValue(device: device, type: .IsSaveOverwrite)
peripheral?.writeValue(data, for: characteristic!, type: CBCharacteristicWriteType.withResponse);
}
//Read the property successfully callback
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if(error != nil)
{
print("Did Update Characteristic with UUID \(characteristic.uuid) Error:\(String(describing: error!))")
return;
}
readConfigs.ConfigRespond(uuid: characteristic.uuid.uuidString);
device = characteristicHandle.SetDevice(device: device, type: characteristicHandle.GetCharacteristicType(uuid: characteristic.uuid), bytes: characteristic.value!);
// Check if all characteristics have been read, if so, start writing
if readConfigs.IsComplete() && readConfiguration == false
{
readConfiguration = true
hideActivityIndicator()
print("Read all characeteristics")
checkToken()
}
}
// Never gets called
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
print("Write Config")
}
请问有人指出了什么?