mirror of
https://github.com/HEYAHONG/SimpleBLETool.git
synced 2025-05-08 05:26:40 +08:00
添加读取数据选项
This commit is contained in:
parent
42ee5268b7
commit
bde0d750ad
@ -2,6 +2,75 @@
|
||||
#include "wx/msgdlg.h"
|
||||
#include "misc.h"
|
||||
#include <thread>
|
||||
|
||||
#include <wx/treectrl.h>
|
||||
#include <wx/clipbrd.h>
|
||||
|
||||
class wxTreeCtrlPerhItemData:public wxTreeItemData
|
||||
{
|
||||
public:
|
||||
SimpleBLE::Peripheral perh;
|
||||
wxTreeCtrlPerhItemData(SimpleBLE::Peripheral _perh):perh(_perh)
|
||||
{
|
||||
|
||||
}
|
||||
virtual ~wxTreeCtrlPerhItemData()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class wxTreeCtrlServiceItemData:public wxTreeItemData
|
||||
{
|
||||
public:
|
||||
SimpleBLE::Peripheral perh;
|
||||
SimpleBLE::BluetoothUUID ServiceUUID;
|
||||
wxTreeCtrlServiceItemData(SimpleBLE::Peripheral _perh,SimpleBLE::BluetoothUUID _ServiceUUID):perh(_perh),ServiceUUID(_ServiceUUID)
|
||||
{
|
||||
|
||||
}
|
||||
virtual ~wxTreeCtrlServiceItemData()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class wxTreeCtrlServiceCharItemData:public wxTreeItemData
|
||||
{
|
||||
public:
|
||||
SimpleBLE::Peripheral perh;
|
||||
SimpleBLE::BluetoothUUID ServiceUUID;
|
||||
SimpleBLE::BluetoothUUID CharUUID;
|
||||
wxTreeCtrlServiceCharItemData(SimpleBLE::Peripheral _perh,SimpleBLE::BluetoothUUID _ServiceUUID,SimpleBLE::BluetoothUUID _CharUUID):perh(_perh),ServiceUUID(_ServiceUUID),CharUUID(_CharUUID)
|
||||
{
|
||||
|
||||
}
|
||||
virtual ~wxTreeCtrlServiceCharItemData()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class wxTreeCtrlServiceCharDescItemData:public wxTreeItemData
|
||||
{
|
||||
public:
|
||||
SimpleBLE::Peripheral perh;
|
||||
SimpleBLE::BluetoothUUID ServiceUUID;
|
||||
SimpleBLE::BluetoothUUID CharUUID;
|
||||
SimpleBLE::BluetoothUUID DescUUID;
|
||||
wxTreeCtrlServiceCharDescItemData(SimpleBLE::Peripheral _perh,SimpleBLE::BluetoothUUID _ServiceUUID,SimpleBLE::BluetoothUUID _CharUUID,SimpleBLE::BluetoothUUID _DescUUID):perh(_perh),ServiceUUID(_ServiceUUID),CharUUID(_CharUUID),DescUUID(_DescUUID)
|
||||
{
|
||||
|
||||
}
|
||||
virtual ~wxTreeCtrlServiceCharDescItemData()
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
extern const char * bluetooth_ico_xpm[];
|
||||
BLEPeripheralDialog::BLEPeripheralDialog(wxWindow* parent):GUIPeripheral(parent)
|
||||
{
|
||||
@ -30,6 +99,11 @@ BLEPeripheralDialog::~BLEPeripheralDialog()
|
||||
{
|
||||
OnCloseCb();
|
||||
}
|
||||
|
||||
if(m_treeCtrl->GetCount()!=0)
|
||||
{
|
||||
m_treeCtrl->DeleteAllItems();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -101,6 +175,140 @@ void BLEPeripheralDialog::OnDisConnect( wxCommandEvent& event )
|
||||
m_button_disconnect->Enable(false);
|
||||
}
|
||||
|
||||
void BLEPeripheralDialog::OnTreeItemRightClick( wxTreeEvent& event )
|
||||
{
|
||||
wxTreeItemData *data=dynamic_cast<wxTreeItemData *>(event.GetClientObject());
|
||||
if(data!=NULL)
|
||||
{
|
||||
m_treeCtrl->SelectItem(event.GetItem());
|
||||
wxMenu menu;
|
||||
SimpleBLE::Safe::Peripheral perh(Perh);
|
||||
|
||||
{
|
||||
auto menufunc=[&]( wxCommandEvent& event_menu )
|
||||
{
|
||||
if (wxTheClipboard->Open())
|
||||
{
|
||||
wxTheClipboard->SetData( new wxTextDataObject(m_treeCtrl->GetItemText(event.GetItem())));
|
||||
wxTheClipboard->Close();
|
||||
}
|
||||
};
|
||||
wxMenuItem *item=menu.Append(1000,_T("复制节点名称"));
|
||||
menu.Bind(wxEVT_COMMAND_MENU_SELECTED,menufunc,item->GetId(),item->GetId());
|
||||
}
|
||||
|
||||
menu.AppendSeparator();
|
||||
|
||||
{
|
||||
wxTreeCtrlPerhItemData *_Data=dynamic_cast<wxTreeCtrlPerhItemData *>(data);
|
||||
if(_Data!=NULL)
|
||||
{
|
||||
//设备
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wxTreeCtrlServiceItemData *_Data=dynamic_cast<wxTreeCtrlServiceItemData *>(data);
|
||||
if(_Data!=NULL)
|
||||
{
|
||||
//服务
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wxTreeCtrlServiceCharItemData *_Data=dynamic_cast<wxTreeCtrlServiceCharItemData *>(data);
|
||||
if(_Data!=NULL)
|
||||
{
|
||||
//特征
|
||||
{
|
||||
auto menufunc=[&]( wxCommandEvent& event_menu )
|
||||
{
|
||||
|
||||
std::optional<SimpleBLE::ByteArray> read_data;
|
||||
try
|
||||
{
|
||||
read_data=perh.read(_Data->ServiceUUID,_Data->CharUUID);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
wxMessageBox(_T("读取出错!"),_T("错误"));
|
||||
}
|
||||
if(read_data.has_value() && !read_data.value_or("").empty())
|
||||
{
|
||||
std::string str=read_data.value_or("");
|
||||
std::string hexstr;
|
||||
{
|
||||
for(size_t i=0; i<str.length(); i++)
|
||||
{
|
||||
char buff[10]= {0};
|
||||
snprintf(buff,sizeof(buff)-1,"%02X ",(int)str.c_str()[i]);
|
||||
hexstr+=buff;
|
||||
}
|
||||
}
|
||||
wxLogMessage(wxString(_T("成功读取到设备数据\r\n\t设备:%s\r\n\t服务:%s\r\n\t特征:%s\r\n\t数据:%s\r\n\t数据(HEX):%s")),wxString(_Data->perh.address()),_Data->ServiceUUID,_Data->CharUUID,wxString::FromUTF8(str.c_str(),str.length()),hexstr);
|
||||
wxMessageBox(_T("读取成功,请在主窗口的日志中查看读取结果!"),_T("提示"));
|
||||
}
|
||||
else
|
||||
{
|
||||
wxMessageBox(_T("读取失败或未读到有效数据!"),_T("提示"));
|
||||
}
|
||||
};
|
||||
wxMenuItem *item=menu.Append(1301,_T("读取"));
|
||||
menu.Bind(wxEVT_COMMAND_MENU_SELECTED,menufunc,item->GetId(),item->GetId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wxTreeCtrlServiceCharDescItemData *_Data=dynamic_cast<wxTreeCtrlServiceCharDescItemData *>(data);
|
||||
if(_Data!=NULL)
|
||||
{
|
||||
//描述符
|
||||
|
||||
{
|
||||
auto menufunc=[&]( wxCommandEvent& event_menu )
|
||||
{
|
||||
|
||||
std::optional<SimpleBLE::ByteArray> read_data;
|
||||
try
|
||||
{
|
||||
read_data=perh.read(_Data->ServiceUUID,_Data->CharUUID,_Data->DescUUID);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
wxMessageBox(_T("读取出错!"),_T("错误"));
|
||||
return;
|
||||
}
|
||||
if(read_data.has_value() && !read_data.value_or("").empty())
|
||||
{
|
||||
std::string str=read_data.value();
|
||||
std::string hexstr;
|
||||
{
|
||||
for(size_t i=0; i<str.length(); i++)
|
||||
{
|
||||
char buff[10]= {0};
|
||||
snprintf(buff,sizeof(buff)-1,"%02X ",(int)str.c_str()[i]);
|
||||
hexstr+=buff;
|
||||
}
|
||||
}
|
||||
wxLogMessage(wxString(_T("成功读取到设备数据\r\n\t设备:%s\r\n\t服务:%s\r\n\t特征:%s\r\n\t描述符:%s\r\n\t数据:%s\r\n\t数据(HEX):%s")),wxString(_Data->perh.address()),_Data->ServiceUUID,_Data->CharUUID,_Data->DescUUID,wxString::FromUTF8(str.c_str(),str.length()),hexstr);
|
||||
wxMessageBox(_T("读取成功,请在主窗口的日志中查看读取结果!"),_T("提示"));
|
||||
}
|
||||
else
|
||||
{
|
||||
wxMessageBox(_T("读取失败或未读到有效数据!"),_T("提示"));
|
||||
}
|
||||
};
|
||||
wxMenuItem *item=menu.Append(1401,_T("读取"));
|
||||
menu.Bind(wxEVT_COMMAND_MENU_SELECTED,menufunc,item->GetId(),item->GetId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PopupMenu(&menu);
|
||||
}
|
||||
}
|
||||
|
||||
void BLEPeripheralDialog::UpdateStatus()
|
||||
{
|
||||
if(mainwindow!=NULL)
|
||||
@ -164,7 +372,7 @@ void BLEPeripheralDialog::UpdateStatus()
|
||||
{
|
||||
if(m_treeCtrl->GetCount()!=0)
|
||||
m_treeCtrl->DeleteAllItems();
|
||||
wxTreeItemId root=m_treeCtrl->AddRoot(wxString(Perh.identifier()) + " [" << Perh.address() + "]");
|
||||
wxTreeItemId root=m_treeCtrl->AddRoot(wxString(Perh.identifier()) + " [" << Perh.address() + "]",-1,-1,new wxTreeCtrlPerhItemData(Perh));
|
||||
|
||||
auto services = perh.services();
|
||||
if(services.has_value())
|
||||
@ -184,13 +392,13 @@ void BLEPeripheralDialog::UpdateStatus()
|
||||
};
|
||||
for (auto service : *services)
|
||||
{
|
||||
wxTreeItemId serviceid=m_treeCtrl->AppendItem(root,wxString(_T("服务: "))+ToDesc(service.uuid()));
|
||||
wxTreeItemId serviceid=m_treeCtrl->AppendItem(root,wxString(_T("服务: "))+ToDesc(service.uuid()),-1,-1,new wxTreeCtrlServiceItemData(Perh,service.uuid()));
|
||||
for (auto characteristic : service.characteristics())
|
||||
{
|
||||
wxTreeItemId charid=m_treeCtrl->AppendItem(serviceid,wxString(_T("特征: "))+ToDesc(characteristic.uuid()));
|
||||
wxTreeItemId charid=m_treeCtrl->AppendItem(serviceid,wxString(_T("特征: "))+ToDesc(characteristic.uuid()),-1,-1,new wxTreeCtrlServiceCharItemData(Perh,service.uuid(),characteristic.uuid()));
|
||||
for (auto& descriptor : characteristic.descriptors())
|
||||
{
|
||||
wxTreeItemId descid=m_treeCtrl->AppendItem(charid,wxString(_T("描述符: "))+ToDesc(descriptor.uuid()));
|
||||
wxTreeItemId descid=m_treeCtrl->AppendItem(charid,wxString(_T("描述符: "))+ToDesc(descriptor.uuid()),-1,-1,new wxTreeCtrlServiceCharDescItemData(Perh,service.uuid(),characteristic.uuid(),descriptor.uuid()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ protected:
|
||||
virtual void OnUnpair( wxCommandEvent& event );
|
||||
virtual void OnConnect( wxCommandEvent& event );
|
||||
virtual void OnDisConnect( wxCommandEvent& event );
|
||||
virtual void OnTreeItemRightClick( wxTreeEvent& event );
|
||||
|
||||
private:
|
||||
|
||||
|
@ -224,6 +224,7 @@ GUIPeripheral::GUIPeripheral( wxWindow* parent, wxWindowID id, const wxString& t
|
||||
m_button_unpair->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnUnpair ), NULL, this );
|
||||
m_button_connect->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnConnect ), NULL, this );
|
||||
m_button_disconnect->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnDisConnect ), NULL, this );
|
||||
m_treeCtrl->Connect( wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, wxTreeEventHandler( GUIPeripheral::OnTreeItemRightClick ), NULL, this );
|
||||
}
|
||||
|
||||
GUIPeripheral::~GUIPeripheral()
|
||||
@ -233,6 +234,7 @@ GUIPeripheral::~GUIPeripheral()
|
||||
m_button_unpair->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnUnpair ), NULL, this );
|
||||
m_button_connect->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnConnect ), NULL, this );
|
||||
m_button_disconnect->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( GUIPeripheral::OnDisConnect ), NULL, this );
|
||||
m_treeCtrl->Disconnect( wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, wxTreeEventHandler( GUIPeripheral::OnTreeItemRightClick ), NULL, this );
|
||||
|
||||
m_mgr.UnInit();
|
||||
|
||||
|
@ -142,6 +142,7 @@ class GUIPeripheral : public wxFrame
|
||||
virtual void OnUnpair( wxCommandEvent& event ) { event.Skip(); }
|
||||
virtual void OnConnect( wxCommandEvent& event ) { event.Skip(); }
|
||||
virtual void OnDisConnect( wxCommandEvent& event ) { event.Skip(); }
|
||||
virtual void OnTreeItemRightClick( wxTreeEvent& event ) { event.Skip(); }
|
||||
|
||||
|
||||
public:
|
||||
|
@ -1076,6 +1076,7 @@
|
||||
<property name="window_extra_style"></property>
|
||||
<property name="window_name"></property>
|
||||
<property name="window_style"></property>
|
||||
<event name="OnTreeItemRightClick">OnTreeItemRightClick</event>
|
||||
</object>
|
||||
<object class="wxStatusBar" expanded="0">
|
||||
<property name="bg"></property>
|
||||
|
Loading…
x
Reference in New Issue
Block a user