BLU蓝牙上位机求指导
最近在 网上查到一工具
系统平台低功耗蓝牙开发接口库,提供BLE设备的枚举、扫描、连接和检测,蓝牙服务和特征的枚举、读写、订阅,控制器版本查询等API函数。支持Windows、Android等系统。
下载地址:
http://www.wch.cn/downloads/WCHBleLib_MultiOS_ZIP.html
想将C++代码转换为AARDIO,但现在回调函数不会用,求指导
大致看了下, 回调在aardio里根据调用方式stdcall的有
thread.tostdcall
raw.tostdcall
看你那个dll是stdcall,用对应的转换下aar里面的函数成为函数指针即可.
可以参考上面的例子, 还有aardio的sio串口库里其实也有这样的回调, 先琢磨下, 试试手, 还是不行的话, 贴上你的代码还有运行遇到的问题.
自己研究出来也是个学习过程, 比别人直接给代码记忆深刻. O(∩_∩)O~
import win.ui;
/*DSG{{*/
mainForm = win.form(text="BLU蓝牙PC上位机";right=959;bottom=591)
mainForm.add(
button={cls="button";text="检查";left=376;top=40;right=451;bottom=69;dl=1;dt=1;z=1};
button2={cls="button";text="检查";left=376;top=78;right=451;bottom=107;dl=1;dt=1;z=4};
button3={cls="button";text="检查";left=376;top=123;right=451;bottom=152;dl=1;dt=1;z=7};
button4={cls="button";text="检查";left=376;top=163;right=451;bottom=192;dl=1;dt=1;z=10};
button5={cls="button";text="检查";left=376;top=200;right=451;bottom=229;dl=1;dt=1;z=13};
button6={cls="button";text="打开蓝牙";left=380;top=237;right=454;bottom=273;z=16};
edit={cls="edit";left=193;top=41;right=361;bottom=69;dl=1;dt=1;edge=1;multiline=1;z=2};
edit2={cls="edit";left=193;top=79;right=361;bottom=107;dl=1;dt=1;edge=1;multiline=1;z=5};
edit3={cls="edit";left=193;top=124;right=361;bottom=152;dl=1;dt=1;edge=1;multiline=1;z=8};
edit4={cls="edit";left=193;top=163;right=361;bottom=191;dl=1;dt=1;edge=1;multiline=1;z=11};
edit5={cls="edit";left=193;top=200;right=361;bottom=228;dl=1;dt=1;edge=1;multiline=1;z=14};
richedit={cls="richedit";left=1;top=350;right=963;bottom=590;edge=1;multiline=1;z=17};
static={cls="static";text="是否支持低功耗蓝牙:";left=0;top=40;right=189;bottom=71;dl=1;dt=1;font=LOGFONT(h=-20);transparent=1;z=3};
static2={cls="static";text="是否可以作为从机:";left=0;top=78;right=188;bottom=109;dl=1;dt=1;font=LOGFONT(h=-20);transparent=1;z=6};
static3={cls="static";text="是否可以作为主机:";left=1;top=123;right=189;bottom=154;dl=1;dt=1;font=LOGFONT(h=-20);transparent=1;z=9};
static4={cls="static";text="是否支持安全连接:";left=2;top=163;right=190;bottom=194;dl=1;dt=1;font=LOGFONT(h=-20);transparent=1;z=12};
static5={cls="static";text="是否支持广播包:";left=0;top=200;right=188;bottom=231;dl=1;dt=1;font=LOGFONT(h=-20);transparent=1;z=15}
)
/*}}*/
import console;
var blu=raw.loadDll("res/blu.dll",,"scdcll")
//blu.WCHBLEInit();//初始化调用库,只有执行1次;
mainForm.button.oncommand = function(id,event){
if(blu.WCHBLEIsLowEnergySupported()){
mainForm.edit.text="支持";
}
else {
mainForm.edit.text="不支持";
}
}
mainForm.button2.oncommand = function(id,event){
if(blu.WCHBLEIsPeripheralRoleSupported()){
mainForm.edit2.text="支持";
}
else {
mainForm.edit2.text="不支持";
}
}
mainForm.button3.oncommand = function(id,event){
if(blu.WCHBLEIsCentralRoleSupported()){
mainForm.edit3.text="支持";
}
else {
mainForm.edit3.text="不支持";
}
}
mainForm.button4.oncommand = function(id,event){
if(blu.WCHBLEAreLowEnergySecureConnectionsSupported()){
mainForm.edit4.text="支持";
}
else {
mainForm.edit4.text="不支持";
}
}
mainForm.button5.oncommand = function(id,event){
if(blu.WCHBLEIsAdvertisementOffloadSupported()){
mainForm.edit5.text="支持";
}
else {
mainForm.edit5.text="不支持";
}
}
mainForm.button6.oncommand = function(id,event){
//设备枚举
//class BLENameDevID{BYTE Name[1024] ; BYTE DevID[1024]; int Rssi}
BLENameDevID = raw.toarray( 24 )
BLENameDevID.array[1] = {BYTE Name[260] ; BYTE DevID[260]; int Rssi};
//var BLENameDevID = { BYTE Name[260] ; BYTE DevID[260]; int Rssi }
var pNum=10;
var DevIDFilter="";//蓝牙名称,空为所有;
WCHBLEEnumDevice=blu.api("WCHBLEEnumDevice","int (int scanTimes,string &DevIDFilter,struct &pBLENameDevIDArry,addr &pNum)");
a=WCHBLEEnumDevice(2500,DevIDFilter,BLENameDevID,pNum)
//console.dump(WCHBLEEnumDevice(2500,DevIDFilter,BLENameDevID,pNum))
for(i=1;10;1){
if(tostring(BLENameDevID.array[i].Name)[1]!=""){
mainForm.richedit.appendText(BLENameDevID.array[i].Name,BLENameDevID.array[i].DevID,BLENameDevID.array[i].Rssi )
mainForm.richedit.appendText('\r\n' )
}
}
pFunReadCallBack = function (a,b) {
console.log(a,b);
}
bmac="BluetoothLE#BluetoothLE00:e0:4c:82:5a:0a-85:cb:3e:cf:00:13"
ConnectStatus=raw.tostdcall( pFunReadCallBack,"void(int,byte)");
WCHBLEOpenDevice=blu.api("WCHBLEOpenDevice","void(string deviceID,pointer ConnectStatus)" )
var flag=WCHBLEOpenDevice(bmac,ConnectStatus)
console.dump(flag)
}
mainForm.show();
return win.loopMessage();
回复#4 @chenqiang :
看你上面的callback定义, 貌似没啥问题,
dll引用是stdcall,你拼错了,void*可以用ADDR, UCHAR用BYTE,
另外你这里演示的是open的callback, 函数名最好一致
我这里调用dll会提示少了个api-ms-xxxxxx.dll, 网上下载了个还是不行, 没法测试, 可能需要注意的地方是raw.tostdcall, 如果调用会报错, 那么可以改用多线程的thread.tostdcall,当然如果用了thread,那么callback函数里就要加一句import console , 才能输出信息. 你自己再测测.
这里void*
var blu=raw.loadDll("res/blu.dll",,"stdcall") FunDevConnChangeCallBack = function (a,b) { console.log(a,b); } ConnectStatus=raw.tostdcall( FunDevConnChangeCallBack,"void(ADDR,BYTE)"); WCHBLEOpenDevice(bmac,ConnectStatus)
没设备测写了个简单的测试:
import win.ui; /*DSG{{*/ mainForm = win.form(text="BLU蓝牙PC上位机";right=657;bottom=368) mainForm.add( button={cls="button";text="Button";left=376;top=276;right=602;bottom=331;z=1}; button2={cls="button";text="Button";left=79;top=268;right=305;bottom=323;z=2} ) /*}}*/ import console; console.open() var dll = raw.loadDll("/WCHBLEDLL.dll","dddd","stdcall"); //pFunDevConnChangeCallBack = dll.api("pFunDevConnChangeCallBack","void(ADDR,BYTE )" ) WCHBLEEnumDevice=dll.api("WCHBLEEnumDevice","void(INT,string,struct&,INT&)" ) WCHBLEOpenDevice=dll.api("WCHBLEOpenDevice","ADDR(string deviceID,pointer ConnectStatus)" ) WCHBLECloseDevice = dll.api("WCHBLECloseDevice","void(ADDR)" ) FunDevConnChangeCallBack = function (a,b) { import console console.dump(a,b); } ConnectStatus=raw.tostdcall( FunDevConnChangeCallBack,"void(ADDR,BYTE)"); var dev,num = WCHBLEEnumDevice(2000,"",{BYTE name[260];BYTE devID[260];int rssi},10); console.log(num); console.dump(dev) console.log(dev.name) console.log(dev.devID) var devHwnd;// = WCHBLEOpenDevice(dev.devID,ConnectStatus) mainForm.button.oncommand = function(id,event){ WCHBLECloseDevice(devHwnd); } mainForm.button2.oncommand = function(id,event){ devHwnd = WCHBLEOpenDevice(dev.devID,ConnectStatus) if(devHwnd==null){ console.log("打开失败") }else { console.log("ok") } } mainForm.show(); win.loopMessage();
用蓝牙鼠标测试,能连接能显示, 但是回调的那个并没有执行, 我估计应该是dll这个回调有问题,或者还没理解这个dll咋用....
登录后方可回帖
很想将原来的上位机全部换为AARDIO,支持jacen.就是不会回调