【问题标题】:How to sent few Bytes on android via bluetooth?如何通过蓝牙在 android 上发送几个字节?
【发布时间】:2015-11-12 14:46:15
【问题描述】:

我在 Android 上通过蓝牙发送字节时遇到问题。 我从 Stackoverflow 得到这段代码:

private boolean connected = false;
private BluetoothSocket sock;
private InputStream in;
private OutputStream out;

zee = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
        "XX:XX:XX:XX:XX:XX");
    m = zee.getClass().getMethod("createRfcommSocket",
        new Class[] { int.class });
    sock = (BluetoothSocket) m.invoke(zee, Integer.valueOf(1));
    sock.connect();
    in = sock.getInputStream();
    out = sock.getOutputStream();

char[] test = { 0x55, 0x0, 0x0, 0x0, 0x0, 0x50, 0x2, 0x0,
        0x30, 0xD7 };

for(int k=0; k < test.lenght; k++){
new DataOutputStream(sock.getOutputStream()).writeByte(test[k]);
}




for(int k=0; k < test.lenght; k++){
new DataOutputStream(sock.getOutputStream()).writeByte(test[k]);
}

字节一个接一个...但我需要将它们发送到一个数据包中。我需要使用什么方法?如何做到这一点?

【问题讨论】:

  • 长度th 不长度ht
  • 谢谢\0/(困惑)
  • 字节一个接一个...但我需要一包发送。我需要使用什么方法?

标签: android bluetooth hex


【解决方案1】:

这对我有用:

OutputStream outStream = socket.getOutputStream();
byte[] test = {0x03, 0x40, 0x00, (byte) ch2, (byte) 0xC1, (byte) 0xC0};
outStream.write(test);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2017-12-15
    • 2016-05-08
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多