【Mac/iOS】解决CoreBluetooth中Characteristic的Properties显示问题

项目中希望能显示Characteristic的所有Properties,在外设管理器中,CBPeripheral类并未提供其显示方法,但在CBCharacteristic中含有.properties属性值。因此可以通过获取属性值对比系统定义的数值以获取其Properties的功能。

代码如下,直接复制代码到工程,调用即可。

使用方法:

// 已有 CBPeripheral  myCharacteristic
NSLog(""Properties:\(propertiesString(properties: myCharacteristic.properties)!) \n")

代码:

   //显示属性权限
   func propertiesString(properties: CBCharacteristicProperties)->(String)!{
       var propertiesReturn : String = ""

       if (properties.rawValue & CBCharacteristicProperties.broadcast.rawValue) != 0 {
           propertiesReturn += "broadcast|"
       }
       if (properties.rawValue & CBCharacteristicProperties.read.rawValue) != 0 {
           propertiesReturn += "read|"
       }
       if (properties.rawValue & CBCharacteristicProperties.writeWithoutResponse.rawValue) != 0 {
           propertiesReturn += "write without response|"
       }
       if (properties.rawValue & CBCharacteristicProperties.write.rawValue) != 0 {
           propertiesReturn += "write|"
       }
       if (properties.rawValue & CBCharacteristicProperties.notify.rawValue) != 0 {
           propertiesReturn += "notify|"
       }
       if (properties.rawValue & CBCharacteristicProperties.indicate.rawValue) != 0 {
           propertiesReturn += "indicate|"
       }
       if (properties.rawValue & CBCharacteristicProperties.authenticatedSignedWrites.rawValue) != 0 {
           propertiesReturn += "authenticated signed writes|"
       }
       if (properties.rawValue & CBCharacteristicProperties.extendedProperties.rawValue) != 0 {
           propertiesReturn += "indicate|"
       }
       if (properties.rawValue & CBCharacteristicProperties.notifyEncryptionRequired.rawValue) != 0 {
           propertiesReturn += "notify encryption required|"
       }
       if (properties.rawValue & CBCharacteristicProperties.indicateEncryptionRequired.rawValue) != 0 {
           propertiesReturn += "indicate encryption required|"
       }
       return propertiesReturn
   }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,以下是一个简单的示例代码,展示如何从蓝牙设备获取音频数据。 ```objc // 导入相关框架 #import <CoreBluetooth/CoreBluetooth.h> #import <AudioToolbox/AudioToolbox.h> // 定义蓝牙设备的服务和特征UUID #define AUDIO_SERVICE_UUID @"XXXXXX" #define AUDIO_CHARACTERISTIC_UUID @"XXXXXX" // 定义音频队列相关参数 #define kNumberBuffers 3 #define kBufferDurationSeconds 0.1 // 定义音频数据处理回调函数 void AudioInputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions, const AudioStreamPacketDescription *inPacketDescs) { // 处理音频数据 // ... // 将音频数据添加到音频队列 AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL); } @interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate> @property (nonatomic, strong) CBCentralManager *centralManager; @property (nonatomic, strong) CBPeripheral *peripheral; @property (nonatomic, strong) CBService *audioService; @property (nonatomic, strong) CBCharacteristic *audioCharacteristic; @property (nonatomic, assign) AudioQueueRef audioQueue; @property (nonatomic, assign) AudioQueueBufferRef audioBuffers[kNumberBuffers]; @property (nonatomic, assign) SInt64 packetIndex; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化蓝牙心管理器 self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; // 初始化音频队列 [self initAudioQueue]; } - (void)initAudioQueue { // 定义音频格式 AudioStreamBasicDescription audioFormat; audioFormat.mSampleRate = 16000.0; audioFormat.mFormatID = kAudioFormatLinearPCM; audioFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; audioFormat.mFramesPerPacket = 1; audioFormat.mChannelsPerFrame = 1; audioFormat.mBitsPerChannel = 16; audioFormat.mBytesPerPacket = 2; audioFormat.mBytesPerFrame = 2; // 创建音频队列 AudioQueueNewInput(&audioFormat, AudioInputCallback, (__bridge void *)(self), CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &_audioQueue); // 计算音频队列缓冲区的大小 UInt32 bufferByteSize = kBufferDurationSeconds * audioFormat.mSampleRate * audioFormat.mBytesPerFrame; // 创建音频队列缓冲区 for (int i = 0; i < kNumberBuffers; i++) { AudioQueueAllocateBuffer(_audioQueue, bufferByteSize, &_audioBuffers[i]); } // 启动音频队列 AudioQueueStart(_audioQueue, NULL); } - (void)centralManagerDidUpdateState:(CBCentralManager *)central { if (central.state == CBManagerStatePoweredOn) { // 开始搜索蓝牙设备 [central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:AUDIO_SERVICE_UUID]] options:nil]; } } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI { // 连接蓝牙设备 self.peripheral = peripheral; [central connectPeripheral:peripheral options:nil]; } - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 扫描蓝牙设备的服务和特征 peripheral.delegate = self; [peripheral discoverServices:@[[CBUUID UUIDWithString:AUDIO_SERVICE_UUID]]]; } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"Error discovering services: %@", error); return; } // 获取蓝牙设备的音频服务 for (CBService *service in peripheral.services) { if ([service.UUID isEqual:[CBUUID UUIDWithString:AUDIO_SERVICE_UUID]]) { self.audioService = service; break; } } // 扫描音频服务的特征 [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:AUDIO_CHARACTERISTIC_UUID]] forService:self.audioService]; } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { NSLog(@"Error discovering characteristics: %@", error); return; } // 获取音频服务的特征 for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:AUDIO_CHARACTERISTIC_UUID]]) { self.audioCharacteristic = characteristic; // 设置特征通知,当蓝牙设备有音频数据时,会自动通知应用程序 [peripheral setNotifyValue:YES forCharacteristic:characteristic]; break; } } } - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error updating value: %@", error); return; } // 从特征获取音频数据并添加到音频队列 NSData *audioData = characteristic.value; if (audioData) { AudioQueueBufferRef buffer = self.audioBuffers[self.packetIndex % kNumberBuffers]; memcpy(buffer->mAudioData, audioData.bytes, audioData.length); buffer->mAudioDataByteSize = (UInt32)audioData.length; AudioQueueEnqueueBuffer(self.audioQueue, buffer, 0, NULL); self.packetIndex++; } } @end ``` 注意,上述代码仅为示例代码,可能需要根据实际情况进行修改和调整。此外,还需要在Info.plist文件添加相应的蓝牙权限和音频权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值