使用IOBluetooth框架的音频数据污染了我的RFCOMM通道



我正在开发一个简单的主机程序(c++/xcode/osx),该程序可以建立与蓝牙客户端的连接,并打开用于数据传输的RFCOMM通道。当我从BT客户端向主机发送数据时,我会从看起来像蓝牙音频代理的设备中获得混合了随机转储的数据。我是否可能与OS X蓝牙音频代理共享我的RFCOMM通道?

我的蓝牙设备需要同时支持串行(SPP)和免提(HFP)模式。因此,我坚信问题是围绕HFP或SCO配置文件的数据污染了我的RFCOMM通道。有没有办法确保我只通过通道获取数据?

这是我项目的浓缩版。当客户端连接时,它开始回显字母a。主机应用程序打印它从客户端接收的数据,如果接收到的数据是a,它还会记录OK!n

#include <IOBluetooth/IOBluetoothUserLib.h>
#include <IOBluetooth/IOBluetoothUtilities.h>
#include <IOBluetoothUI/IOBluetoothUI.h>
#include <IOBluetoothUI/IOBluetoothUIUserLib.h>
#include "testApp.h"
// Callback function create-connection
void bluetoothCreateConnection_c(void *userRefCon, IOBluetoothDeviceRef deviceRef, IOReturn status) {
printf("Created connectionn");
}
// Callback for RFCOMM events
void rfcommEventListener_c (IOBluetoothRFCOMMChannelRef channel_ref, void *userRefCon, IOBluetoothRFCOMMChannelEvent *event) {
if (event->eventType == kIOBluetoothRFCOMMNewDataEvent) {
const char *data_const = (const char *)event->u.data.dataPtr;
char *dataAsBytes = const_cast<char *>(data_const); 
string in_s = (string)dataAsBytes;
printf("%s", dataAsBytes);
if(in_s == "a") {
printf("OK!n");
}
}
}
void testApp::setup() {
BluetoothDeviceAddress *addr;
string s = "00-16-a4-00-72-3d"; // hardcoded BT address
CFStringRef str_addr; 
str_addr = CFStringCreateWithCString(kCFAllocatorDefault, s.c_str(), kCFStringEncodingMacRoman); 
IOBluetoothCFStringToDeviceAddress(str_addr, addr);
IOBluetoothDeviceRef dev = IOBluetoothDeviceCreateWithAddress(addr);
IOBluetoothDeviceOpenConnection(dev, &bluetoothCreateConnection_c, NULL);
printf("in openRFCOMMChanneln");
CFArrayRef device_services = IOBluetoothDeviceGetServices(dev);
printf("Getting SDP service recordn");
IOBluetoothSDPServiceRecordRef service_record = (IOBluetoothSDPServiceRecordRef) CFArrayGetValueAtIndex(device_services, 0);
UInt8 channel_id;
IOBluetoothRFCOMMChannelRef channel_ref;
printf("Finding channel IDn");
IOBluetoothSDPServiceRecordGetRFCOMMChannelID(service_record, &channel_id);
// Open channel and listen for RFCOMM data
IOBluetoothDeviceOpenRFCOMMChannelAsync(dev, &channel_ref, channel_id, &rfcommEventListener_c, NULL);
}

这是程序的输出:

<预>运行[切换到过程41228]正在运行…在openRFCOMMChannel中2011-06-13 13:44:04.762测试(调试)[41228:a0f]***__NSAutoreleaseNoPool():类NSCF阵列的对象0x1d27770在没有池的情况下自动释放-只是泄漏获取SDP服务记录正在查找通道ID已创建连接a好的!aa好!aa好!一()*\251\253\274_蓝牙版本号_持久服务ZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionarya)*\251\253\274_BluetohVersionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionarya\251\253\274_BluetohVersionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionarya_BluetoothVersionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetohAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryauetoothVersionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对设备s_PersistentportsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryathVersionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对设备s_PersistentportsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryasionNumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对设备s_PersistentportsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryaumber_PersistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionarya_SistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryarsistentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryaentServicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryarvicesZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryasZHIDDevices_COAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryaDevices_SCOAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]成对的Devices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryaes_SCOAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryaSCOAudioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryradioDevices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServicesMstreamtyped\201\350\204@\204\204NSMutableDictionaryavices_DaemonNoRoleSwitchDeviceList_!DaemonControllersConfigurationKey_ControllerPowerState_BluetoothAutoSeekHIDDevices_PersistentPortsZPANDevices]PairedDevices_PersstentPortsServia\232a00-16-a4-00-17-7e_00-16-a4-72-3d_00-23-7f-49-98-a6_00-1a-10-04-13-9a\323a6-a4-000-17-7e_00-49-98-a6-_00-1a-10-104-13-9a\323a00-17-7e-00-16-a-00-72-3d_00-23-7f-49-98-a6_00-1a-10-04-13-9a\323 a-7e_00-16-a-00-72-3d_00-23-7-f-49-98-al6-_00-1a-10-44-13-9a \323a00-16-a \323a6-a4-00-72-3d_00-23-7f-49-98-a6_00-1a-10-04-13-9a\323a00-72-3d_00-23-7f-49-98-a6_00-1a-10-04-13-9a\323a-3d_00-23-7f-49-98-al6_00-1a-10-04-13-9a\323a00-23-7f-49-98-Al6_00-1a-10-04-13-9a\323a3-7f-49980-al6_00-1 a-10-04-139a\323 a49-98-a6_00-1a-10-04-13-9 a\323a-a6_00-1 a-10-04-113-9a\\323a00-1a-10-04-13-9a\323a-10-3a\323a\323aOK!aYAgentPathXDriverID_IOAudioControls_W/System/Library/CoreServices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudioAgent_00-16-a4-00-17-e-SCO \244antPathXDriverID-IOAudioControl s_W/System/Library/CoreServices/BluetoghAudioAgent/app/Contents/MacOS/BluetoghAudioagenT_00-16-a-00-17-7e-SCO \244 ahXDriverID/IOAudioControls_W/System/Library/CoreServices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudioAgent_100-16-a4-00-17-e-SCO \244averID_IOAudioControls_W/System/Library/CoreServices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudieAgent_00-16-a-00-17-7e-SCO\244a_IOAudioControls_W/System/Libraly/CoreService/BluetohAudioAgent.app/Contents/MacOS/BlueoothAudioAgent_100-16-a4-00-17-e-SCO \244aControls_W/System/Library/CoreServices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudioaAgent_100-16-a4-00-1-7e-SCO \244 aols_W/System/Library/CoreServices/Blueto齿AudioAgent/app/Contents/MacOS/Blueto齿AudioaAgent_00-16-a4-00-17-7-e-SCO \ 244aW/System/Librare/CoreServices/BluetoothAudioAgent.ap/Contents/MacOS/BluetoghAudioAgent_0-16-a4-00-07-7-SCO \244atem/Library/CoreServices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudioAgent_00-16-a4-00-17-e-SCO \244aibrary/CoreServices/Blueto齿AudioAgent/app/Contents/MacOS/Blueto齿AudioagenT_00-16-a-00-17-7e-SCO \244 ay/CoreServices/BluetoothAudioAgent.app.Contents/MacOS/BluetoghAudioAgent_100-16-a100-17-7-SCO \244aeServices/BluetoothAudioAgent.app/Contents/MacOS/BBluetoothaudioagent_00-16-a-00-17-7e SCO\244aices/BluetoothAudioAgent.app/Contents/MacOS/BluetoothAudieAgent_00-16-a4-00-17-e-SCO\244aBluetoothAudioAgent.app/Contents/MacOS/BluetoothAudioAgent_00-16-a4-00-1-7e-SCO \244aoothAudioAgent.app/Contents/MacOS/Blueto齿AudioAgent_100-16-a-00-17-7e-SCO \ 244audioAgent/app/Contents/MacOS/Blueto齿AudioagenT_00-16-a100-17-7-SCO-7e SCO \244aapp/Contents/MacOS/BluetoothAudioAgent_100-16-a4-00-17-e-SCO \244aontents/MacOS/BluetoothAudio Agent_100-16-a4-00-1-7e-SCO \244 ats/MacOS/BluetoothAudioAgent_100-16-a-00-17-7-7e-SCO \ 244valuetoothAudioPagent_100-16-a-00-177-e-SCO \ 244 aothAudioAgent_00-16-a-00-17-77-e-SCO \n44 adioAgent_10-16-a4-0-17-77-SCO \244 aent_10-16-a4-00-17-7e-SCO \244a6-a4-00-17-7-e-SCO \244 a00-17-7e-SCO \244a-7e-SCO \ 244aCO\244aOK!好的!a%aOK!aa好!a.a好的!aIOA音频控制类型_IOAudioControlSubType_IOAudioControlChannelID_IOAudio控制值_IOAudio-ControlChannelName_IOAAudioLevelControlMaxDB_IOAudioBlevelControlMaxValue_IOAudioPlevelControlMinValue_IOAAudioBLevelControlMinDB_IOAAudioControlUsagelvlvlmeaioControlType_IOAAudioPontrolSubType_IOA音频控制通道ID_IOAAudioSontrolValue_IOAaudioControlChannelName值_IOAudioLevelControlMinValue_IOAudioClevelControlMinDB_IOAudio ControlUsagelevlevelMetrolType_IOAudioBontrolSubType_IOAAudioControlChannelID_IOAudioSontrolValue_IOAAudioControlChannelName_IOAudioPlevelControlMaxDB_IOAAudioPLevelControlMaxValue_IOAaudioLevelControl MinValue_IoaAudioLevelcontrolMinDB_IoaAudioControl Usagelevellmeype_IOAAudioSontrolSubType控制通道名称_IOAudioLevelControlMaxDB_IOAudioClevelControlMaxValue_IOAudioPlevelControlMinValue_IOAAudioLevelControl最小DB_IOAAudioControlUsagelevllmeaOAAudioCntrolSubType_IOAAudioSontrolChannelID_IOAAudioBontrolValue_IOAaudioControlChannel名称_IOAAudioPlevelControl最大DB_IOAaudioLevel控件最大值_IOAAudio LevelControlMinvalue_IOAAudiioLevelControl最小DB_IOA AudioControl使用的音频控件子类型_IO音频控制通道ID_IOAudioControlValue_IOAudioControlChannelName_IOAAudioLevelControlMaxDB_IOAAudioControlMaxValue_IOAAudioPlevelControlMinValue_IOAaudioLevelControl MinDB_IOAudioBontrolUsagelvlmentrolSubType_IOAudioPontrolChannel ID_IOAAudioBontrolValue_IOAAudio控制通道名称_IOAAudio级别控制MaxDB_IoaAudioLevel控制MaxValue_IoaAudioControl MinValue_IoaudioLevel控制MinDB_IOAudioC控制UsagelevllmeaaSubType_IOAudioControlChannelID_IOAudioControlValue_IOAudioBontrolChannelName_IOAudioPlevelControlMaxDB_IOAudioSLevelControlMaxValue_IOAAudioLevelControlMinValue_IOAaudioLevelControl MinDB_IOAAudioControlUsagelelvlmeaaIOAudioControl ChannelID-IOAudioCntrolValue_IOA AudioControlChannel Name_IOAAudioPlevelControl MaxDB_IOA AudioClevelControlmaxValue_IOAAudio LevelControlminValue_IOAadioLevelControlMinDB_IOAudioControlUsagelvlmeaioControlChannelID_IOAudioControlValue_IOAudioBontrolChannelName_IOAudioPlevelControlMaxDB_IOAAudioLevelControlMaxValue_IOAAudioPLevelControlMinValue_IOAaudioLevelcontrolMinDB_IoaAudioControlUsegelvlmeatrolChannelID-IOAudioCntrolValue_IoaAudioControlChannel Name_IOAAudioClevelControlmaxDB_IOAaudioClevelControl MaxValue_IoaudioLevelControl MinValue_IoaaudioLevel ControlMinDB_IO音频控制UsagelevlvlmeahannelID_IOAudioControlValue_IOAudioControlChannelName_IOAudioPlevelControlMaxDB_IOAudioBLevelControlMaxValue_IOAAudioLevelControlMinValue_IOAaudioLevelControl MinDB_IOAAudioControlUsagelev lvlmealID_IOAAudioBontrolValue_IOAadioControlChannel Name_IOAAudioPLevelControlmaxDB_IOAaudioControlMaxValue_IOAudioLevelControlMinValue_IoaAudioLevel ControlMinDB_IoaAudioControl UsagelevelvlmeaIOA音频控制值_IO音频控制通道名称_IO音频级别控制最大DB_IO音频级控制最大值_IO声音级控制最小值_IO音乐级控制最小DB_IO音乐控制使用gelevlvlmeaioControlValue_IO音频控制通道名称_I奥音频级控制MaxDB_IO声音级别控制最大值-IO音频级控制最小值_I奥音频级别控制最小DB_iO音频控制使用gelev_IOAudioLevelControlMaxDB_IOAudioLevelControl MaxValue_IOAudioControlControlMinValue_IOAAudioLevel ControlMinDB_IOAAudioControlUsegelevlvlmeaValue_IOAaudioControlChannelName_IOAAudiioLevelControlmaxDB_IOAaudioLevelcontrolMaxValue_IoaAudioLevel=ControlMinValue-IOAAudioLevel ControlMinDB_iOAAudioControlUsagelevllmea_IOAAudioPontrolChanelName_IOAudio Level ControlMaxDB_iOAudioLevel ControlMaxValue_IOA AudioLevel Control最小值_IOAudioLevelControlMinDB_IOAudioControlUsagelevllmeaAudioControlChannelName_IOAudio LevelControlMaxDB_IOAAudioLevelControl最大值_IOAAudio LevelControl最小值_IOA AudioLevel ControlMinDB-IOAAudioControlUsagelevallmeaControlChannelName_iOAAudioLevel ControlMaxDB-IOAudioLevel Control最大值_IoaAudioLevel控制最小值_IOAudioLevel控制最大DB_IOAudioLevel控制最大值_IOAudio Level控制最小值_IOAAudioLevel控制最小DB_IOAAudioControlUsagelvelmannelName_IOAudioControlMaxDB_IOAaudioLevel控制最高值_IOAaudioControlMinValue_IOAAudioBontrolUsegelvelmaaaaaaaaaaa aaaaaaametrol UsgelevlvlmeaoAudioLevel控制MaxDB_IOAudioLevel控制最大值_IOAudio Level控制最小值_IOAaudioLevel控制最小DB_IOAaudio控制UsgelevLVlmeaoRadioLevel控制最大DB_IOAAudioLevel控制器最大值_IoaudioLevel控制器最小值_IoaAudioLevel控件最小DB_Ioaudio控制UsagelvlvlvlmeaveControlevlvlmeantrolMaxDB_IOAudioLevelControlMaxValue_IOAudio LevelControlMinValue_IOAAudioLevelcontrolMinDB_IOAAudio ControlUsagelevllmeaMaxDB_IOA AudioLevel ControlMaxValue-IOAudioLevel ControlMinValue-IOAAudioLevel ControlMi

好的,我很确定我找到了我的问题。我复制到char *的数据指针没有0终止。我重写了这个:

const char *data_const = (const char *)event->u.data.dataPtr;
char *dataAsBytes = const_cast<char *>(data_const); 
string in_s = (string)dataAsBytes;

对此:

const char *data_const = (const char *)data.dataPtr;
char *dataAsBytes = const_cast<char *>(data_const);
dataAsBytes[data.dataSize] = '';
printf("%s %i %in", dataAsBytes, data.dataSize);

最新更新