licheedev / Android-SerialPort-Tool

Android串口调试助手
601 stars 168 forks source link

Battery and charging command callback #17

Open alirazaseptems opened 3 years ago

alirazaseptems commented 3 years ago

Hello, can you please guide me how can I get the status of a command. For example when I sent a battery command "40BF". it does not always return the battery level. I want to know If I could get some data that tells me if the command is executed successfully or not. The same happens when I send command for charging one out of 10 times. Can you please guide me about this. Thank you.

licheedev commented 3 years ago

@alirazaseptems https://github.com/licheedev/SerialportApiSample i just wrote a simple demo.i hope that will help you.

alirazaseptems commented 3 years ago

I have used serialport manager in java. Can I get some code in java using serial port manager. thanks.

mOpened = SerialPortManager.instance().open(mDevice) != null; if (mOpened) { // ToastUtil.showOne(this, "成功打开串口"); Toast.makeText(this, "成功打开串口", Toast.LENGTH_SHORT).show(); SerialPortManager.instance().sendCommand(batteryCommand);

and I get callback in this method. which does not always give me callback. @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(IMessage message) { }

licheedev commented 3 years ago

oh, forget the terrible code on Android-SerialPort-Tool,i wrote it just for finding out which serialport is avaiable.

in fact, the major code from https://github.com/licheedev/SerialportApiSample is in java,like this SerialWorker.java now the send-receive operation is synchronized, you can wrap the send method with some concurrency tools, like Java Concurrency package or RxJava very easily. so you can write the callback by your own.

licheedev commented 3 years ago

@alirazaseptems ok, i copy the Android-SerialPort-Tool repo https://github.com/licheedev/Android-SerialPort-Tool_Copy i change the code like this SerialPortManager.java

now you can get the response from the callback directly

  SerialPortManager.instance().sendCommand(text, new Callback<byte[]>() {
            @Override
            public void onSuccess(@Nullable byte[] bytes) {
                // TODO: 2020/11/10  
                ToastUtil.showOne(MainActivity.this, "send success with response");
            }

            @Override
            public void onFailure(@NonNull Throwable tr) {
                // TODO: 2020/11/10  
                ToastUtil.showOne(MainActivity.this, "send success without response");
            }
        });
roshenLiang commented 3 years ago

想问下 日后像升级这能开多一个新库吗 别把之前的删除 我之前一直用着 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1' 觉得蛮好的 哈哈

licheedev commented 3 years ago

想问下 日后像升级这能开多一个新库吗 别把之前的删除 我之前一直用着 'com.github.licheedev.Android-SerialPort-API:serialport:1.0.1' 觉得蛮好的 哈哈

@roshenLiang 其实也没删,我改版本之前建了个分支 https://github.com/licheedev/Android-SerialPort-API/tree/1.x.x Jitpack上构建的老版本还在 https://jitpack.io/#licheedev/Android-SerialPort-API