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

查看:45
本文介绍了如何从 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 菜鸟可能想出的所有方法来解决编码问题,例如:

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

然后借助转换方法填写

radioArray = HexStringToByteArray(radioAddr1);

可以在这里找到:HexStringToByteArray@stackoverflow

这篇关于如何从 Android 蓝牙向串行设备发送扩展的 ascii AT 命令 (CCh)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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