如何从Android蓝牙向串行设备发送扩展ASCII AT命令(CCh)? [英] How do you send an extended-ascii AT-command (CCh) from Android bluetooth to a serial device?

查看:71
本文介绍了如何从Android蓝牙向串行设备发送扩展ASCII AT命令(CCh)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的让我头.我正在通过BluetoothChatService将来自Android应用的字母数字数据发送到与无线收发器的串行输入相连的串行蓝牙适配器.

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.

一切正常,除非我尝试使用其AT命令即时配置无线电.可以很好地收到AT +++(输入命令模式),但是问题出在接下来的两个命令中:扩展的ASCII字符:更改无线电目标地址(这是我要执行的操作)需要CCh 10h(加上3个十六进制)无线电地址字节),并且退出命令模式需要CCh ATO.

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.

我知道可以对无线电进行配置,因为我已经使用PIC basic的串行命令在较早的原型上完成了它,并且还可以通过直接从hyperterm输入命令来进行配置.这两种方法都以某种方式将讨厌的CCh转换成无线电可以理解的形式.

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.

我已经尝试了几乎所有Android noob可能想出的所有编码,例如:

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);
}

但是无论如何,我似乎都无法使该高位字节(CCh/204/-52)发挥应有的作用.所有其他(小于127)字节,命令或数据都可以毫无问题地传输.任何帮助,我们将不胜感激.

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.

-戴夫

推荐答案

Welll ...在发送给服务之前,BluetoothChat代码使用 message.getBytes()重新创建了字节数组. . (毕竟,作为聊天代码,它通常仅会获取常规的ascii字符串)正如该站点上的其他人所指出的,在某些情况下,getBytes()可能会造成编码问题.因此,出于发送这些扩展的ascii命令的目的,我不会弄乱字符串,而只是使用以下方式将字节数组发送给服务

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);

可以在这里找到: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆