How do you send an extended-ascii AT-command (CCh) from Android bluetooth to a serial device?

↘锁芯ラ 提交于 2020-01-14 05:57:07

问题


This one really has me banging my head. I'm sending alphanumeric data from an Android app, through the BluetoothChatService, to a serial bluetooth adaptor connected to the serial input of a radio transceiver.

Everything works fine except when I try to configure the radio on-the-fly with its AT-commands. The AT+++ (enter command mode) is received OK, but the problem comes with the extended-ascii characters in the next two commands: Changing the radio destination address (which is what I'm trying to do) requires CCh 10h (plus 3 hex radio address bytes), and exiting the command mode requires CCh ATO.

I know the radio can be configured OK because I've done it on an earlier prototype with the serial commands from PIC basic, and it also can be configured by entering the commands directly from hyperterm. Both these methods somehow convert that pesky CCh into a form the radio understands.

I've have tried just about everything an Android noob could possibly come up with to finagle the encoding such as:

private void command_address() {
    byte[] addrArray = {(byte) 0xCC, 16, 36, 65, 21, 13};                   
    CharSequence addrvalues = EncodingUtils.getString(addrArray, "UTF-8");  
    sendMessage((String) addrvalues);
}

but no matter what, I can't seem to get that high-order byte (CCh/204/-52) to behave as it should. All other (< 127) bytes, command or data, transmit with no problem. Any help here would be greatly appreciated.

-Dave


回答1:


Welll ... turns out the BluetoothChat code re-creates the byte array with message.getBytes() before sending to the service. (after all, being chat code it would normally source only regular ascii strings) As others on this site have pointed out, getBytes() can create encoding issues in some cases. So, for purposes of sending these extended-ascii commands, I don't mess with strings and just send the byte array to the service with

private void sendCommand(byte[] cmd) {
    mChatService.write(cmd);
}

The so-called command array is first initialized with placeholders for the hex radio address elements

byte[] addrArray = {(byte) 0xCC, 16, 0, 0, 0, 13};

and then filled in with the help of the conversion method

radioArray = HexStringToByteArray(radioAddr1);

which can be found here: HexStringToByteArray@stackoverflow



来源:https://stackoverflow.com/questions/4650831/how-do-you-send-an-extended-ascii-at-command-cch-from-android-bluetooth-to-a-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!