coreBluetooth CBUUID转为字符串

我找不到任何官方的方式拿回一个UUID字符串CBUUID。 这些uuid可以2到16字节长。

目标是CBUUIDs存储在一个文件作为字符串的某个地方,然后复活(CBUUID UUIDWithString:]等。这是迄今为止我所。

// returns a simple 4 byte string for 16bit uuids, 128 bit uuids are in standard 8-4-4-4-12 format
// the resulting string can be passed into [CBUUID UUIDWithString:]
+(NSString*)CBUUIDToString:(CBUUID*)cbuuid;
{
    NSData* data = cbuuid.data;
    if ([data length] == 2)
    {
        const unsigned char *tokenBytes = [data bytes];
        return [NSString stringWithFormat:@"%02x%02x", tokenBytes[0], tokenBytes[1]];
    }
    else if ([data length] == 16)
    {
        NSUUID* nsuuid = [[NSUUID alloc] initWithUUIDBytes:[data bytes]];
        return [nsuuid UUIDString];
    }

    return [cbuuid description]; // an error?
}

这个好像有点问题。。

我设置了以下类别为CBUUID这样做:

@interface CBUUID (StringExtraction)

- (NSString *)representativeString;

@end

@implementation CBUUID (StringExtraction)

- (NSString *)representativeString;
{
    NSData *data = [self data];

    NSUInteger bytesToConvert = [data length];
    const unsigned char *uuidBytes = [data bytes];
    NSMutableString *outputString = [NSMutableString stringWithCapacity:16];

    for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; currentByteIndex++)
    {
        switch (currentByteIndex)
        {
            case 3:
            case 5:
            case 7:
            case 9:[outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; break;
            default:[outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]];
        }

    }

    return outputString;
}

@end

输入:

NSLog(@"UUID string: %@", [[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"] representativeString]);
NSLog(@"UUID string2: %@", [[CBUUID UUIDWithString:@"1800"] representativeString]);

它产生以下输出:

UUID string: 0bd51666-e7cb-469b-8e4d-2742f1ba77cc
UUID string2: 1800

并保持适当的断字为16字节uuid,同时支持简单的2字节uuid。

好的,下面是一个简单的 Swift 代码示例,可以连接蓝牙打印机,并根据输入的字符串生成二维码并打印。同时,代码中也包含了设置二维码位置的功能。 需要注意的是,这个代码示例依赖于第三方库 `CoreBluetooth` 和 `QRCodeGenerator`,请确保已经正确安装这些库。 ```swift import UIKit import CoreBluetooth import QRCodeGenerator class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { // 蓝牙设备的名称 let DEVICE_NAME = "Your Bluetooth Printer Name" // 蓝牙相关变量 var centralManager: CBCentralManager! var targetPeripheral: CBPeripheral? var writeCharacteristic: CBCharacteristic? override func viewDidLoad() { super.viewDidLoad() // 初始化蓝牙中央设备管理器 centralManager = CBCentralManager(delegate: self, queue: nil) } // 扫描到蓝牙设备后的回调函数 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if peripheral.name == DEVICE_NAME { targetPeripheral = peripheral // 连接蓝牙设备 centralManager.connect(targetPeripheral!, options: nil) } } // 连接蓝牙设备成功后的回调函数 func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { targetPeripheral?.delegate = self targetPeripheral?.discoverServices(nil) } // 发现蓝牙服务后的回调函数 func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { if let services = peripheral.services { for service in services { // 找到可写的特征值 peripheral.discoverCharacteristics([CBUUID(string: "FFF1")], for: service) } } } // 发现蓝牙特征值后的回调函数 func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { if let characteristics = service.characteristics { for characteristic in characteristics { if characteristic.properties.contains(.write) { writeCharacteristic = characteristic // 生成二维码并打印 let qrCode = QRCode("Hello, world!") let imageData = qrCode?.image(size: CGSize(width: 200, height: 200)) let data = UIImagePNGRepresentation(UIImage(data: imageData!)!) peripheral.writeValue(data!, for: characteristic, type: .withResponse) // 设置二维码位置 let position = Data([0x1B, 0x24, 0x00, 0x00, 0x1B, 0x61, 0x01]) peripheral.writeValue(position, for: characteristic, type: .withResponse) } } } } // 开始扫描蓝牙设备 func startScanning() { centralManager.scanForPeripherals(withServices: nil, options: nil) } // 蓝牙状态发生变化时的回调函数 func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == .poweredOn { startScanning() } } } ``` 这个代码示例中,我们使用了 `QRCodeGenerator` 库来生成二维码,并使用了 `CoreBluetooth` 库来连接蓝牙设备和发送数据。其中,我们通过 `peripheral(_:didDiscoverCharacteristicsFor:error:)` 函数来发现蓝牙特征值,并找到可写的特征值来发送数据。同时,我们也在这个函数中生成二维码,并设置了二维码在纸张上的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值