目的是想让从机仅主机可连接,其他设备无法扫描。根据官方提供的CH579 CH573 CH582 蓝牙从机设置白名单 - debugdabiaoge - 博客园 (cnblogs.com),在从机的 Peripheral_Init( )函数添加如下代码:
void Peripheral_Init( ) { Peripheral_TaskID = TMOS_ProcessEventRegister( Peripheral_ProcessEvent ); // Setup the GAP Peripheral Role Profile { uint8 initial_advertising_enable = TRUE; uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; // Set the GAP Role Parameters GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData ); GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); } // Set the GAP Characteristics GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); // Set advertising interval { uint16 advInt = DEFAULT_ADVERTISING_INTERVAL; GAP_SetParamValue( TGAP_DISC_ADV_INT_MIN, advInt ); GAP_SetParamValue( TGAP_DISC_ADV_INT_MAX, advInt ); } // Setup the GAP Bond Manager { uint32 passkey = 0; // passkey "000000" uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8 mitm = TRUE; uint8 bonding = TRUE; uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; GAPBondMgr_SetParameter( GAPBOND_PERI_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey ); GAPBondMgr_SetParameter( GAPBOND_PERI_PAIRING_MODE, sizeof ( uint8 ), &pairMode ); GAPBondMgr_SetParameter( GAPBOND_PERI_MITM_PROTECTION, sizeof ( uint8 ), &mitm ); GAPBondMgr_SetParameter( GAPBOND_PERI_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap ); GAPBondMgr_SetParameter( GAPBOND_PERI_BONDING_ENABLED, sizeof ( uint8 ), &bonding ); } // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes DevInfo_AddService(); // Device Information Service SimpleProfile_AddService( GATT_ALL_SERVICES ); // Simple GATT Profile ble_ota_add_service(); // Init Connection Item peripheralInitConnItem( &peripheralConnList ); // Register callback with SimpleGATTprofile SimpleProfile_RegisterAppCBs( &Peripheral_SimpleProfileCBs ); // 添加白名单 GAPRole_BroadcasterSetCB( &Broadcaster_BroadcasterCBs ); { uint8 filter_policy = GAP_FILTER_POLICY_WHITE;//允许白名单中的设备扫描和连接。 uint8 mac0[6]={0x1C,0xE0, 0x0D, 0xE4, 0xC2, 0x84};//指定的主机mac地址 uint8 macType = 1; //指定地址类型 LL_ClearWhiteList(); LL_AddWhiteListDevice(macType,mac0); GAPRole_SetParameter( GAPROLE_ADV_FILTER_POLICY, sizeof( uint8 ), &filter_policy );//设置白名单 } // Setup a delayed profile startup tmos_set_event( Peripheral_TaskID, SBP_START_DEVICE_EVT ); }
但是呢,主机这边总是尝试连接,但总是失败。主机打印信息如下:
Discovering... Device found... Connecting... Connected... Disconnected...Reason:3e Discovering...
如此循环,就是连接不了从机。如果修改为
uint8 filter_policy = GAP_FILTER_POLICY_WHITE_SCAN;//允许任何设备扫描,但只能主机扫描连接。
主机倒是可以和从机连接和通信,但不是我要的效果。
请问这是什么问题,还是代码没完善?
热门产品 :
CH585: 集成高速USB和NFC的蓝牙MCU