BLE 更改要写入的 UUID 特征
BLE changing which UUID characteristic to write to
我对使用 Swift 进行编码还很陌生,所以我希望有人能帮助我给出一个简单的答案。
我基本上是在尝试将特征 UUID 发送到写入函数,然后切换写入函数以写入该 UUID。它只会继续使用我原来的那个。是否有捷径可寻?我发现了所有外围设备、服务和特征,并且已经连接到正确的设备。我只是无法让它切换到正确的特征 UUID。任何帮助都是极好的!我是一名 firmware/hardware 开发人员,所以 Swift 和 Xcode 对我来说还不是日常用品。
func writePosition(position: UInt16, characteristicUUID: CBUUID) {
var position_low: UInt8 = 0
var position_high: UInt8 = 0
var position16: UInt16 = 0;
position_low = (UInt8) (position) // (position)
position_high = (UInt8)((position >> 8))
// See if characteristic has been discovered before writing to it
if self.positionCharacteristic == nil {
return
}
var bytes:[UInt8] = [0x00, 0x00]
bytes[1] = 0x01 //low byte is [1], high byte is [0] so reversed from LightBlue app
bytes[1] = position_low //low byte is [1], high byte is [0] so reversed from LightBlue app
bytes[0] = position_high //low byte is [1], high byte is [0] so reversed from LightBlue app
let uuidsForBTService: [CBUUID] = [characteristicUUID]
//new code to try and set the correct UUID automatically
for service in peripheral!.services
{
if service.UUID == BLEServiceUUID
{
peripheral!.discoverCharacteristics(uuidsForBTService, forService: service as! CBService)
}
}
//find the correct characteristic to set to and then write to it
//go through each characteristic and look at its UUID then if it matches, set us to that one so we write to it later on
for characteristic in self.peripheral!.services {
if characteristic.UUID == characteristicUUID//Position1PWMCharUUID
{
self.positionCharacteristic = (characteristic as! CBCharacteristic)
peripheral!.setNotifyValue(true, forCharacteristic: characteristic as! CBCharacteristic)
// Send notification that Bluetooth is connected and all required characteristics are discovered
self.sendBTServiceNotificationWithIsBluetoothConnected(true)
}
}
//end of code to set the UUID
// Need a mutable var to pass to writeValue function
//var positionValue = position
//let data = NSData(bytes: &positionValue, length: sizeof(UInt16))
let data = NSData(bytes: bytes, length: 2)
self.peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}
请记住,在第二个 for
循环中,您正在寻找 CBCharacteristic
的地方,您仍在迭代外围设备的服务和不是您的 ble 服务的特征(外围设备有服务,服务有特征)。
下面的代码就是得到你需要的CBCharacteristic
var theService: CBService?
// Find service
for service in peripheral.services as! [CBService] {
if service.UUID == BLEServiceUUID {
theService = service
break
}
}
var theCharacteristic: CBCharacteristic?
// Find characteristic
for characteristic in theService?.characteristics as! [CBCharacteristic] {
if characteristic.UUID == characteristicUUID {
theCharacteristic = characteristic
break
}
}
// Write
if let characteristic = theCharacteristic {
peripheral.writeValue(data, forCharacteristic: characteristic, type: .WithResponse)
}
在您的代码中,您总是写入 self.positionCharacteristic
。只需将您的数据写入您刚刚找到的CBCharacteristic
。
此外,如果您需要更多帮助,我写了一个简单的 swift class 来管理中央连接、读取和写入任务。
希望对您有所帮助!
我对使用 Swift 进行编码还很陌生,所以我希望有人能帮助我给出一个简单的答案。
我基本上是在尝试将特征 UUID 发送到写入函数,然后切换写入函数以写入该 UUID。它只会继续使用我原来的那个。是否有捷径可寻?我发现了所有外围设备、服务和特征,并且已经连接到正确的设备。我只是无法让它切换到正确的特征 UUID。任何帮助都是极好的!我是一名 firmware/hardware 开发人员,所以 Swift 和 Xcode 对我来说还不是日常用品。
func writePosition(position: UInt16, characteristicUUID: CBUUID) {
var position_low: UInt8 = 0
var position_high: UInt8 = 0
var position16: UInt16 = 0;
position_low = (UInt8) (position) // (position)
position_high = (UInt8)((position >> 8))
// See if characteristic has been discovered before writing to it
if self.positionCharacteristic == nil {
return
}
var bytes:[UInt8] = [0x00, 0x00]
bytes[1] = 0x01 //low byte is [1], high byte is [0] so reversed from LightBlue app
bytes[1] = position_low //low byte is [1], high byte is [0] so reversed from LightBlue app
bytes[0] = position_high //low byte is [1], high byte is [0] so reversed from LightBlue app
let uuidsForBTService: [CBUUID] = [characteristicUUID]
//new code to try and set the correct UUID automatically
for service in peripheral!.services
{
if service.UUID == BLEServiceUUID
{
peripheral!.discoverCharacteristics(uuidsForBTService, forService: service as! CBService)
}
}
//find the correct characteristic to set to and then write to it
//go through each characteristic and look at its UUID then if it matches, set us to that one so we write to it later on
for characteristic in self.peripheral!.services {
if characteristic.UUID == characteristicUUID//Position1PWMCharUUID
{
self.positionCharacteristic = (characteristic as! CBCharacteristic)
peripheral!.setNotifyValue(true, forCharacteristic: characteristic as! CBCharacteristic)
// Send notification that Bluetooth is connected and all required characteristics are discovered
self.sendBTServiceNotificationWithIsBluetoothConnected(true)
}
}
//end of code to set the UUID
// Need a mutable var to pass to writeValue function
//var positionValue = position
//let data = NSData(bytes: &positionValue, length: sizeof(UInt16))
let data = NSData(bytes: bytes, length: 2)
self.peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}
请记住,在第二个 for
循环中,您正在寻找 CBCharacteristic
的地方,您仍在迭代外围设备的服务和不是您的 ble 服务的特征(外围设备有服务,服务有特征)。
下面的代码就是得到你需要的CBCharacteristic
var theService: CBService?
// Find service
for service in peripheral.services as! [CBService] {
if service.UUID == BLEServiceUUID {
theService = service
break
}
}
var theCharacteristic: CBCharacteristic?
// Find characteristic
for characteristic in theService?.characteristics as! [CBCharacteristic] {
if characteristic.UUID == characteristicUUID {
theCharacteristic = characteristic
break
}
}
// Write
if let characteristic = theCharacteristic {
peripheral.writeValue(data, forCharacteristic: characteristic, type: .WithResponse)
}
在您的代码中,您总是写入 self.positionCharacteristic
。只需将您的数据写入您刚刚找到的CBCharacteristic
。
此外,如果您需要更多帮助,我写了一个简单的 swift class 来管理中央连接、读取和写入任务。
希望对您有所帮助!