0

how to convert 35 bytes of hex values to decimal coming from bluetooth serial port communnication.. i am using bluetooth chat application, the device sending data in hex format of 35 bytes.... my code giving output like: 10 oa ff 00 3c................11 my code.. Bluetoothservice.java

public void run() {

            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[35];
            int bytes;            
            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer, 0, buffer.length); 
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothService.this.start();
                    break;
                }
            }

MainActivity.java

byte[] readBuf = (byte[]) msg.obj; 

String readMessage = BytesTrans.bytes2HexString(readBuf,msg.arg1);

mArrayAdapter.addAll(mConnectedDeviceName+":  "+ readMessage );

public static class BytesTrans {

        public static String bytes2HexString(byte[] b, int count) {
            String ret = "";
            //String str ="";
            for (int i = 0; i < count; i++) {
                String hex = Integer.toHexString(b[i] & 0xFF); 
                if (hex.length() ==1) {
                   hex = '0' + hex;               
                    }
                ret += hex.toUpperCase() + " ";                              
            }
            return ret;
        }`
4

1 回答 1

0

Try the following:

public static String bytes2String(byte[] b, int count) {
        String ret = "";
        //String str ="";
        for (int i = 0; i < count; i++) {
            String myInt = Integer.toString((int)(b[i] & 0xFF)); 

            ret.append(myInt + " ");                             
        }
        return ret;
    }

I think this may be what you are looking for.

于 2013-04-18T17:26:41.060 回答