2015-08-19 116 views
-3

我在Android上通过蓝牙发送字节时遇到问题。 我得到这个代码#1:如何通过蓝牙在android上发送几个字节?

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

字节变为一个接一个......但我需要向他们发送一个数据包。我需要使用什么方法?这个怎么做?

+1

楞** **个不楞** ** HT – akalikin

+0

谢谢\ 0 /(迷茫) –

+0

字节变为一个接一个......但我需要将它们一包发送。我需要使用什么方法? –

回答

0

这项工作对我来说:

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