将命令传递给未通过Android上的蓝牙进行配置的设备

时间:2018-08-30 11:28:07

标签: android bluetooth device

在下面的代码中,保存按钮正在调用sendCommandToBleService()

@OnClick(R.id.saveConfig)
    public void onClick() {

        sendCommandToBleService();
    }

在此方法中,首先检查是否有任何值仅显示敬酒消息。

传递诸如pollnumber, geoloaction, ...之类的命令。

这些命令可以正常打印。

之后,使用bathAh,bathWatt作为参数传递给另一种方法:

private void sendCommandToBleService() {
        String pollNumber = mPollNumber.getText().toString();
        String geoLocation = mGeoLocation.getText().toString();
        String luminairePower = mLuminourPower.getText().toString();
        String projectName = mProjectName.getText().toString();
        String battAh = mBatteryAh.getText().toString();
        String pannelWatt = mPannelWatt.getText().toString();

        if (TextUtils.isEmpty(pollNumber)) {
            Toast.makeText(LightConfigurationActivity.this, R.string.poll_number_blank, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(geoLocation) || geoLocation.equalsIgnoreCase("SET")) {
            Toast.makeText(LightConfigurationActivity.this, R.string.geoLocation_blank, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(luminairePower) || luminairePower.equalsIgnoreCase("SET")) {
            Toast.makeText(LightConfigurationActivity.this, R.string.luminour_power_empty, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(projectName)) {
            Toast.makeText(LightConfigurationActivity.this, R.string.project_name_blank, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(battAh)) {
            Toast.makeText(LightConfigurationActivity.this, R.string.battery_ah_blank, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(pannelWatt)) {
            Toast.makeText(LightConfigurationActivity.this, R.string.pannel_watt_blank, Toast.LENGTH_SHORT).show();
            return;
        }
        makeDefaultValues(pollNumber, geoLocation, luminairePower, projectName, battAh, pannelWatt);
        if (battAh.length() == 1) {
            battAh = "00" + battAh;
        } else if (battAh.length() == 2) {
            battAh = "0" + battAh;
        }
        if (pannelWatt.length() == 1) {
            pannelWatt = "00" + pannelWatt;
        } else if (pannelWatt.length() == 2) {
            pannelWatt = "0" + pannelWatt;
        }
        sendCommandsToBleService(battAh,pannelWatt);
    }

在以下方法中,向logcat的打印工作正常:

private void sendCommandsToBleService(String batAhValue,String pannellwattValue) {

        String command = mSolarController.generatePacket("pol " + mPollNumber.getText().toString(), "00");
        String longCmd = mPref.getString(Constants.LAT_COMMAND, "");
        String latCmd = mPref.getString(Constants.LONG_COMMAND, "");
        String firstTimerCmd = mPref.getString(Constants.TIMER_ONE_CMD, "");
        String secondTimerCmd = mPref.getString(Constants.TIMER_TWO_CMD, "");
        String projectCmd = mSolarController.generatePacket("pro " + mProjectName.getText().toString(), "00");
        String batAh = mSolarController.generatePacket("battah " + batAhValue, "00");
        String pannelWatt = mSolarController.generatePacket("panelw " + pannellwattValue, "00");
        String wattCommand = mPref.getString(Constants.BRIGHTNESS_CMD, "");
        String luminaryWatt = mPref.getString(Constants.LUMINOURE_WATT_COMMAND, "");
        mBleService.addCommandToQue(command);
        mBleService.addCommandToQue(longCmd);
        mBleService.addCommandToQue(latCmd);
        mBleService.addCommandToQue(firstTimerCmd);
        mBleService.addCommandToQue(secondTimerCmd);
        mBleService.addCommandToQue(projectCmd);
        mBleService.addCommandToQue(batAh);
        mBleService.addCommandToQue(pannelWatt);
        mBleService.addCommandToQue(wattCommand);
        mBleService.addCommandToQue(luminaryWatt);
        showProgressDialog();
        mBleService.sendCommandToBle();
    }

broadcastreceiver,我没有收到任何回复。我收到ACTION_NO_RESPONSE_FOUND_TERMINATEprogressdialog被解雇了。

private BroadcastReceiver mBroadCastReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equalsIgnoreCase(BleService.SAMPLE_RESPONSE_FOUND)) {
                Toast.makeText(LightConfigurationActivity.this, "Response found is : "+intent.getStringExtra(BleService.COMMAND_RESPONSE), Toast.LENGTH_SHORT).show();
            }
            switch (action) {
                case BleService.ACTION_LAST_RESPONSE_FOUND:
                    if (mProgressDialog != null) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDialog.dismiss();
                                showSuccessPopUp();
                            }
                        },15000);
                    }
                    break;
                case BleService.ACTION_CHECKSUM_MATCH_FAILED:
                    if (mIsActivityRunning) {
                       resendconfig();
                    }
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    break;
                case BleService.ACTION_GAT_FAILED_TO_RECIEVE_ACKNOWLEDGMENT:
                    /**
                     * ToDo : Need to remove Toast notification.
                     */
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    if (mIsActivityRunning) {
                        showAlertDialog(getString(R.string.failed_to_get_response_from_device));
                    }
                    break;
                case BleService.ACTION_NO_RESPONSE_FOUND_TERMINATE:
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    Toast.makeText(context, "Failed to get acknowledgment. Please try again...", Toast.LENGTH_SHORT).show();
                    break;
                case BleService.ACTION_GATT_DISCONNECTED:
                    if (mProgressDialog != null) {
                        mProgressDialog.dismiss();
                    }
                    Toast.makeText(LightConfigurationActivity.this, R.string.disconnected, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

任何人都可以帮助我解决此问题吗?

0 个答案:

没有答案