如何在Android中通过蓝牙发送十六进制值

hnz*_*zjo 8 android bluetooth

我想通过bluetooth/SPP在android中发送一个十六进制字符串我试过这个:

out = sock.getOutputStream();

String myHexString = Integer.toHexString(80) + " "
        + Integer.toHexString(2) + " " + Integer.toHexString(0)
        + " " + Integer.toHexString(48);
Run Code Online (Sandbox Code Playgroud)

发送此字符串:

out.write(myHexString.getBytes());
Run Code Online (Sandbox Code Playgroud)

但没有奏效......

任何帮助?

hnz*_*zjo 11

问题解决了这个问题:

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]);
}
Run Code Online (Sandbox Code Playgroud)