Swift 不允许蓝牙写入
Swift Bluetooth Writing is not permitted
我尝试用 Bluejay 库将命令形式 iphone 写入 BT ble,如下所示
func write(command: Command) {
let weakSelf = self
Utils.delay(0.3) {
debugPrint("[\(self.getTodayString())]Writing to device: \(command.string)");
debugPrint("hunter: weakSelf.bdeCharacteristics = \(weakSelf.bdeCharacteristics) and bdeService= \(weakSelf.bdeService) , and isConnected = \(weakSelf.isConnected)");
weakSelf.bluejay.write(to: weakSelf.bdeCharacteristics, value: command.string) { (result) in
switch result {
case .success:
debugPrint("Write to sensor location is successful.")
//case .cancelled:
//debugPrint("Cancelled write to sensor location.")
case .failure(let error):
debugPrint("Failed to write to sensor location with error: \(error.localizedDescription)")
}
}
}
}
写命令时,日志如下:
"[2020-1-13 0:30:24]Writing to device: SystemOn:\n[=12=]"
2020-01-13 00:30:24.247 [Bluejay] [main] > Requesting write on Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB...
2020-01-13 00:30:24.248 [Bluejay] [main] > Queue will start Bluejay.DiscoverService...
2020-01-13 00:30:24.248 [Bluejay] [main] > Queue has removed Bluejay.DiscoverService because it has finished.
2020-01-13 00:30:24.249 [Bluejay] [main] > Queue is empty, nothing to update.
2020-01-13 00:30:24.249 [Bluejay] [main] > Queue will start Bluejay.DiscoverCharacteristic...
2020-01-13 00:30:24.250 [Bluejay] [main] > Bluejay.WriteCharacteristic added to queue with UUID: 15A4B937-7BCB-4F02-B6D5-96E04D480320.
2020-01-13 00:30:24.250 [Bluejay] [main] > Queue has removed Bluejay.DiscoverCharacteristic because it has finished.
2020-01-13 00:30:24.251 [Bluejay] [main] > Queue will start Bluejay.WriteCharacteristic...
2020-01-13 00:30:24.252 [Bluejay] [main] > Started write to Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB on 845E44D2-42EE-7B03-CC5D-10675DF5DB09.
2020-01-13 00:30:24.253 [Bluejay] [main] > Failed writing to Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB on 845E44D2-42EE-7B03-CC5D-10675DF5DB09 with error: Writing is not permitted.
"Failed to write to sensor location with error: Writing is not permitted."
结果显示:无法写入传感器位置,出现错误:不允许写入。
注:项目原形swift3升级为swift4
根据
i found this solution
CBCharacteristicWriteWithResponse 在 swift 4
之后更改为 withoutResponse
所以我用 withResponse 替换 withResponse ,这个问题就解决了。
我尝试用 Bluejay 库将命令形式 iphone 写入 BT ble,如下所示
func write(command: Command) {
let weakSelf = self
Utils.delay(0.3) {
debugPrint("[\(self.getTodayString())]Writing to device: \(command.string)");
debugPrint("hunter: weakSelf.bdeCharacteristics = \(weakSelf.bdeCharacteristics) and bdeService= \(weakSelf.bdeService) , and isConnected = \(weakSelf.isConnected)");
weakSelf.bluejay.write(to: weakSelf.bdeCharacteristics, value: command.string) { (result) in
switch result {
case .success:
debugPrint("Write to sensor location is successful.")
//case .cancelled:
//debugPrint("Cancelled write to sensor location.")
case .failure(let error):
debugPrint("Failed to write to sensor location with error: \(error.localizedDescription)")
}
}
}
}
写命令时,日志如下:
"[2020-1-13 0:30:24]Writing to device: SystemOn:\n[=12=]" 2020-01-13 00:30:24.247 [Bluejay] [main] > Requesting write on Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB... 2020-01-13 00:30:24.248 [Bluejay] [main] > Queue will start Bluejay.DiscoverService... 2020-01-13 00:30:24.248 [Bluejay] [main] > Queue has removed Bluejay.DiscoverService because it has finished. 2020-01-13 00:30:24.249 [Bluejay] [main] > Queue is empty, nothing to update. 2020-01-13 00:30:24.249 [Bluejay] [main] > Queue will start Bluejay.DiscoverCharacteristic... 2020-01-13 00:30:24.250 [Bluejay] [main] > Bluejay.WriteCharacteristic added to queue with UUID: 15A4B937-7BCB-4F02-B6D5-96E04D480320. 2020-01-13 00:30:24.250 [Bluejay] [main] > Queue has removed Bluejay.DiscoverCharacteristic because it has finished. 2020-01-13 00:30:24.251 [Bluejay] [main] > Queue will start Bluejay.WriteCharacteristic... 2020-01-13 00:30:24.252 [Bluejay] [main] > Started write to Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB on 845E44D2-42EE-7B03-CC5D-10675DF5DB09. 2020-01-13 00:30:24.253 [Bluejay] [main] > Failed writing to Characteristic: 0000FFB2-0000-1000-8000-00805F9B34FB, Service: 0000FFB0-0000-1000-8000-00805F9B34FB on 845E44D2-42EE-7B03-CC5D-10675DF5DB09 with error: Writing is not permitted. "Failed to write to sensor location with error: Writing is not permitted."
结果显示:无法写入传感器位置,出现错误:不允许写入。
注:项目原形swift3升级为swift4
根据 i found this solution
CBCharacteristicWriteWithResponse 在 swift 4
之后更改为 withoutResponse所以我用 withResponse 替换 withResponse ,这个问题就解决了。