完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
嗨,大家好,
我用PSoC创建者4和Android Studio制作了一个将PSoC设备连接到Android设备的BLE应用程序。我使用了如何创建一个PSoC Android BLE应用教程系列,HTTP://www. CyPress .COM/VIET-Labaly/PSOC软件/WOR-CRATET-PSOC-ANDROID-BUL-AP-LASEN-1-GETtiT-STARTED/98221,并对其进行了修改,使其只在LED上关闭,而不是附加的CAPSENSE功能。 然而,在教程中,不是使用两个按钮,一个打开,一个关闭LED,它使用了一个开关。开关将作为一个组件,具有两个功能,一个开启,一个关闭LED。虽然很好,但我想有两个按钮来打开/关闭LED。 如何将Android演播室中的程序从一个开关转换为两个按钮,以便最终结果相同,因为我将能够控制LED并打开/关闭它。 谢谢, 安德鲁·柯林斯 以上来自于百度翻译 以下为原文 Hi All, I have made a BLE app that connects the PSoC device to the Android device, using PSoC Creator 4 and Android Studio. I used the "How to Create a PSoC Android BLE App tutorial series, http://www.cypress.com/video-lib ... ting-started/498221, and modified it so that it only turns off and on an LED, instead of the additional capsense feature. However, in the tutorials, instead of using two buttons, one to turn on, and one to turn off the LED, it used one switch. The switch would work as one component with two functions, one to turn on, and one to turn off the LED. Although that is good, I would like to have two buttons to turn on/off the LED. How do you convert the program in Android Studio from one switch to two buttons, so that the end result will be the same in that I will be able to control the LED and turn it on/off? Thanks, Andrew Collins |
|
相关推荐
9个回答
|
|
将Android代码更改为具有两个按钮(通过修改名称等将切换代码复制为两个按钮以使其独一无二)
然后更改在按下“开关”时运行的代码,这些代码是在按下时为每个按钮运行的代码。 以上来自于百度翻译 以下为原文 Change the android code to have two buttons (duplicate the switch code into two buttons by modifying names etc. to make them unique) Then change the code that runs when the "switch" is presssed to be code that runs for each button when pressed |
|
|
|
yuhe82 发表于 2018-10-19 08:44 你好, 我已经尝试转换Android程序,并且已经成功地完成了大部分程序。但是,有一段代码我很难从开关转换到按钮: /*这将被称为当LED开关/感动/ led_switch。setoncheckedchangelistener(新compoundbutton。oncheckedchangelistener() { 公共无效oncheckedchanged(compoundbutton buttonview,布尔校核){ / /打开或关闭的开关mpsoccapsenseledservice状态LED。writeledcharacteristic(校核); } (}); 你知道怎么把这个转换成两个按钮吗? 谢谢, 安得烈 以上来自于百度翻译 以下为原文 Hi, I have tried to convert the Android Program, and have successfully completed most of it. However, there is one section of code that I am having trouble to convert from switch to button: /* This will be called when the LED On/Off switch is touched */ led_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // Turn the LED on or OFF based on the state of the switch mPSoCCapSenseLedService.writeLedCharacteristic(isChecked); } }); Do you know how to convert this into two buttons? Thanks, Andrew |
|
|
|
60user196 发表于 2018-10-19 08:54 它看起来像一个开关状态改变的事件处理程序;所以你需要改变这一到两个事件处理程序,在每一个按钮。这里是一个线程在谈论Android事件处理按钮的更多的技术细节:按钮点击听众在Android堆栈溢出 另一个可能有帮助的线程:Java- Android:如何处理按钮点击-堆栈溢出 我会尝试像(复制粘贴从第二螺纹连接): clickbutton =按钮(按钮)findViewById(r.id.clickbutton);clickbutton。setonclicklistener(新onclicklistener() { @Override public void OnClick(视图){ //做自动生成方法存根***做你想要的点击这里*** } });然后,创建两个;一个按钮1把LED在一个按钮2关闭LED关闭。 以上来自于百度翻译 以下为原文 It looks like a handler for a switch event state changed; So you would need to change it from one to two event handlers, with one for each button. Here is a thread talking about the more technical details of the android event handling for buttons: Button Click Listeners in Android - Stack Overflow And another thread that might help: java - Android: how to handle button click - Stack Overflow I would try something like (copy-pasted from second thread link): Button clickButton = (Button) findViewById(R.id.clickButton);Then, create two of these; One for button 1 to turn the LED on, and one for button 2 to turn the LED off. |
|
|
|
yuhe82 发表于 2018-10-19 09:09 嗨,Pratt, 感谢有用的信息!然而,当我改变代码的时候,我遇到了一些问题。 下面是我从切换到按钮的代码: 按钮点击按钮=(按钮)FiefView ByID(R.ID.BTNYON);点击按钮。StutOnCLIKListNER(new Mepublic活动)({公共空投OnCutton(视图V){MPSoCopsNeSeldService,Wrue EdLoad(true);}}); *BTNYON代表LED上的LED按钮。 出现的问题有: “不能解析符号”StEngCLIKListNER“-行2”变量“OnCalk”从未使用“-行4”不能解析符号“V”-行4 你知道如何解决这些问题吗? 以上来自于百度翻译 以下为原文 Hi Pratt, Thanks for the useful information! However, when I was changing the code, I ran into a few problems. Below is my code that I converted from switch to button:
*btn_ON stands for the button that turns the LED on The problems that occured were:
Do you know how to fix these problems? |
|
|
|
对不起,我不太了解Android的开发。 但是从外观来看,所有这些问题中的三个都与未被识别的StutnCLIKistListar函数有关。 检查是否需要导入或包含按钮/组件的库,或者对OnCutter侦听器函数的库声明/引用更为明确。 以上来自于百度翻译 以下为原文 Sorry, not really I don't know much about android development. But from the looks of it, all three of those issues are related to the setOnClickListener function not being recognized. Check if you need to import or include a library for buttons/components, or be more explicit with the library declaration/reference for the onclick listener function. |
|
|
|
yuhe82 发表于 2018-10-19 09:27 普拉特 别担心。通过进一步的研究,我发现如果我把整个程序块(对于按钮的OnCLCKISTER)移动到OnCuto空洞的括号内,那么程序就不会出错了! 我已经做了LED按钮没有问题,所以我决定复制它的LED关闭。然而,当我这样做时,它什么也不做。我把所有的东西都换掉了,这样就可以使LED关闭,而不是打开,我从PSoC上做了两个不同的UUIDs(一个特性),一个打开,另一个关闭。我知道这些应该是可行的,因为我在CyScript应用程序中试用了我的应用程序,看看它是否有效。第一个特性使它开启,第二个特性关闭它。 这是我的PSoC代码,打开它: Read编写(!WrReqPARAM-&;HANDALVALION.Valuy Val[0 ]; ……并关掉它: Read编写(!WrReqPARAM-&;HANDALVALION.Valuy Val[1 ]; 但是,如果在这两个函数中没有问题,我可能已经把问题缩小到Android Studio前面的那个程序集合中,这是先前用于交换的: 公共空写特性(布尔值){ 字节[]ByTeVal=新字节〔1〕; 如果(值){ BytVal[0 ] =(字节)(1); }否则{ BytVal[0 ] =(字节)(0); } log(i)(标记,“LED”+值); MeDeLeCwitSt=值; 设定值(ByTEVAL); 书写特征(MeDeLeimTITC); } 当我为LED关闭按钮复制这个按钮时,我就这样做了(字节)(1)和(字节)(0),但是这并没有做任何事情。 你知道这段程序在做什么吗?如果是的话,你能做它使它关闭LED而不是打开它? 以上来自于百度翻译 以下为原文 Pratt, No worries. Through a bit more research I found that if I moved the entire chunk of program (for the onClickListener of the Button) inside the brackets of the onCreate void, then the program would run with no errors! I have made the LED ON button with no problem, so I decided to duplicate it for the LED OFF. However, when I do this, it does nothing. I switched everything so it is supposed to make the LED turn off instead of on, and I made two different UUIDs (characteristics) from the PSoC, one turning it on and the other turning it off. I know that these should work because I tried out my app in the CySmart app to see if it works. The first characteristic turns it on, and the second turns it off. Here is my PSoC code for turning it ON: red_Write(!wrReqParam->handleValPair.value.val[0]); ...And for turning it OFF: red_Write(!wrReqParam->handleValPair.value.val[1]); However, if there is no problem in those two functions, I may have narrowed down the problem to this set of program in Android Studio that was used earlier for the switch: public void writeLedCharacteristic(boolean value) { byte[] byteVal = new byte[1]; if (value) { byteVal[0] = (byte) (1); } else { byteVal[0] = (byte) (0); } Log.i(TAG, "LED " + value); mLedSwitchState = value; mLedCharacterisitc.setValue(byteVal); mBluetoothGatt.writeCharacteristic(mLedCharacterisitc); } When I duplicated this for the LED OFF button, I had it so the (byte) (1) and (byte) (0) would be switched, but this does not do anything. Do you understand what this section of programming is doing? If so, can you make it so that it turns off the LED instead of turning it ON? |
|
|
|
60user196 发表于 2018-10-19 09:45 啊,我想我看到问题了。 WrutelEddioType()使用一个特性,并写0或1来告诉演示项目打开或关闭LED。 因为你把开关的功能分成两个按钮,并且需要两个单独的特性,每个按钮一个,当LED打开或关闭时发出信号,那么只有其中一个的特性将被正确写入。 如果您的代码被绑定到调用写特性(ON/OFF)函数,那么您可以只将内部更改为: 公共空写特性(布尔值){ 字节[]ByTeVal=新字节〔1〕; 如果(值){ BytVal[0 ] =(字节)(1); MaSimultC.StValk((字节)(1));/ /将值设置为特征结构,将其发送到远程设备 MrBuutoToGATT.WrreEngult(MaNeMICITC);/ /将结构发送到远程设备 }否则{ BytVal[0 ] =(字节)(0); MuFrimeTimeC.StValk((字节)(1));/ /将值设置为特征结构以将其发送到远程设备 MrBuutoToGATT.WrreEngulink(MoFosiNeimTC);/ /将结构发送到远程设备 } log(i)(标记,“LED”+值); mledswitchstate =值;/ /不知道这是什么,可能是一个局部变量为GUI的开关状态跟踪 } moffcharacteristic和moncharacteristic将打开/关掉它的两个特征的UUID处理 (如果你不需要分两按键都有各自的特点,那么这似乎是把事情复杂化了) 基本上,这writeledcharacteristic功能作为一个切换,切换1和0之间的特征值,这可能是显示重新使用mledswitchstate GUI。 你可能要改变按钮火灾不同writeledcharacteristic()功能,或者让他们调用相同的函数有不同的价值观,进而改变其特征处理用于写入远程装置改变为你写作的价值的两个特点S. PS:最有可能的是,有一个位置的mledcharacteristic UUID是集。你将需要改变位置的代码为两个“moncharacteristic”和“moffcharacteristic”结构中的示例代码,上面写上正确的两UUID。 以上来自于百度翻译 以下为原文 Ah; I think I see the problem The writeLedCharacteristic() is using a single characteristic, and writing a 0 or 1 to it to tell the demo project to turn the LED on or off. Since you split the functionality of the switch into two buttons, and want two separate characteristics, one for each button, to signal when the LED turns on or turns off, then the characteristic for only one of them will get written correctly If you have your code tied to calling the writeLedCharacteristic(ON/OFF) function, then you could merely change the inside to be: public void writeLedCharacteristic(boolean value) { byte[] byteVal = new byte[1]; if (value) { byteVal[0] = (byte) (1); mONCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device mBluetoothGatt.writeCharacteristic(mONCharacterisitc); //sends the structure to the remote device } else { byteVal[0] = (byte) (0); mOFFCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device mBluetoothGatt.writeCharacteristic(mOFFCharacterisitc); //sends the structure to the remote device } Log.i(TAG, "LED " + value); mLedSwitchState = value; //Not sure what this is for, probably a local variable to track the switch state for the gui } mOFFCharacteristic and mONCharacteristic would be the UUID handles for the two characteristics for turning it on/off (If you don't need to split the two buttons to have their own characteristics, then this seems like overcomplicating it) Basically, the writeLEDCharacteristic function worked as a toggle, for toggling the value of the characteristic between 1 and 0, and this is probably shown back to the gui using mLedSwitchState. You will want to change the buttons to fire two different writeLEDCharacteristic() functions, or have them call the same function with different values, and then change which characteristic handle is used to write to remote device to change which of the two BLE Characteristics you are writing values to. PS: Most likely, there is a location where the UUID of the mLEDCharacteristic is being set. You will need to change that location of code to write the correct two UUIDs for the two "mOnCharacteristic" and "mOFFCharacteristic" structures in the sample code above. |
|
|
|
yuhe82 发表于 2018-10-19 09:56 非常感谢普拉特!这两个按钮终于工作了,现在我可以成功地把LED打开/打开! 我真的很感激你帮助我解决这个问题。 再次感谢 安德鲁·柯林斯 以上来自于百度翻译 以下为原文 Thanks so much Pratt! The two buttons finally work, and now I can successfully turn the LED Off/On! I am truly grateful for how much you helped me to solve this problem. Thanks again, Andrew Collins |
|
|
|
我很高兴它正在工作。 欢迎你给我小小的帮助! 以上来自于百度翻译 以下为原文 I'm glad it is working And you're welcome for the little help I gave! |
|
|
|
只有小组成员才能发言,加入小组>>
757个成员聚集在这个小组
加入小组2129 浏览 1 评论
1871 浏览 1 评论
3687 浏览 1 评论
请问可以直接使用来自FX2LP固件的端点向主机FIFO写入数据吗?
1806 浏览 6 评论
1552 浏览 1 评论
CY8C4025LQI在程序中调用函数,通过示波器观察SCL引脚波形,无法将pin0.4(SCL)下拉是什么原因导致?
622浏览 2评论
CYUSB3065焊接到USB3.0 TYPE-B口的焊接触点就无法使用是什么原因导致的?
460浏览 2评论
CX3连接Camera修改分辨率之后,播放器无法播出camera的画面怎么解决?
455浏览 2评论
411浏览 2评论
使用stm32+cyw43438 wifi驱动whd,WHD驱动固件加载失败的原因?
1099浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-24 20:29 , Processed in 1.007289 second(s), Total 91, Slave 75 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (威廉希尔官方网站 图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号