在java中如何将十六进制字符串分离为单独的位值?

kzipqqlq  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(398)

使用以下命令成功将字节数组转换为十六进制字符串后:

private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            ScanRecord Scanned = result.getScanRecord();
            String address = btAdapter.getAddress();

            byte[] packetData = Scanned.getBytes();
            StringBuilder R = new StringBuilder();
                for(byte hex : packetData){
                    R.append(String.format("%02X", hex));
                }

我想现在有能力读取十六进制流的每一位分别作为数值,以允许对我的ble扫描仪应用程序的值执行基本逻辑。
我读过很多网站,我最初的想法是在for循环中使用数组并读入十六进制字符串的每个字符。但我是java新手,还没有成功做到这一点!
任何关于我如何能有效地做到这一点的想法都会很好。
编辑:
初始输出使用getbytes()并给出一个字节数组输出,如“[b@”,后跟一些数字和字母。这个值不是我需要的格式,我需要62位十六进制流,由于for循环,它现在已经成功打印了!
现在我想将62位数据包长度的十六进制数据分离为单独的当前值。不将它们转换回字节数组。
希望这能进一步澄清这些要求
编辑:到目前为止完整的代码

package com.example.joelwasserman.androidbletutorial;

import android.Manifest;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.ParcelUuid;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import java.util.List;
import java.util.UUID;
import java.util.Vector;

public class MainActivity extends AppCompatActivity {

    BluetoothManager btManager;
    BluetoothAdapter btAdapter;
    BluetoothLeScanner btScanner;
    Button startScanningButton;
    Button stopScanningButton;
    TextView peripheralTextView;
    private final static int REQUEST_ENABLE_BT = 1;
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        peripheralTextView = (TextView) findViewById(R.id.PeripheralTextView);
        peripheralTextView.setMovementMethod(new ScrollingMovementMethod());

        startScanningButton = (Button) findViewById(R.id.StartScanButton);
        startScanningButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startScanning();
            }
        });

        stopScanningButton = (Button) findViewById(R.id.StopScanButton);
        stopScanningButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                stopScanning();
            }
        });
        stopScanningButton.setVisibility(View.INVISIBLE);

        btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        btAdapter = btManager.getAdapter();
        btScanner = btAdapter.getBluetoothLeScanner();

        if (btAdapter != null && !btAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
        }

        // Make sure we have access coarse location enabled, if not, prompt the user to enable it
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect peripherals.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            });
            builder.show();
        }
    }

    // Device scan callback.
    private ScanCallback leScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            ScanRecord Scanned = result.getScanRecord();
            String address = btAdapter.getAddress();

            byte[] packetData = Scanned.getBytes();

            /*StringBuilder R = new StringBuilder();
                for(byte hex : packetData){
                    R.append(String.format("%02X", hex));
                }*/
            int PacketLength = Scanned.getBytes().length;
//            int x = Integer.parseInt(String.valueOf(packetData));
//            String int_val = Integer.toString(x);

            if(new String("SimpleBLEBroadcaster").equals(result.getDevice().getName()))
                peripheralTextView.append("Device Name: " + result.getDevice().getName() + " rssi: " + result.getRssi() +" Packet length: " + PacketLength + " Packet Data: " + "0x" + R  +  "\n");
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_COARSE_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    System.out.println("coarse location permission granted");
                } else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Functionality limited");
                    builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                        }

                    });
                    builder.show();
                }
                return;
            }
        }
    }

    public void startScanning() {
        System.out.println("start scanning");
        peripheralTextView.setText("");
        startScanningButton.setVisibility(View.INVISIBLE);
        stopScanningButton.setVisibility(View.VISIBLE);
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                btScanner.startScan(leScanCallback);
            }
        });
    }

    public void stopScanning() {
        System.out.println("stopping scanning");
        peripheralTextView.append("Stopped Scanning");
        startScanningButton.setVisibility(View.VISIBLE);
        stopScanningButton.setVisibility(View.INVISIBLE);
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                btScanner.stopScan(leScanCallback);
            }
        });
    }

    public static byte[] hexStringToByteArray(String s) throws IOException {
        char[] buf = new char[2];
        int numRead = -1;
        StringReader in = new StringReader(s);
        byte[] bytes = new byte[s.length() / 2];
        int byteIndex = 0;
        while ((numRead = in.read(buf)) > -1) {
            byte b = (byte) Integer.parseInt(String.valueOf(buf), 16);
            bytes[byteIndex++] = b;
        }
        return bytes;
    }
}
roejwanj

roejwanj1#

记住我所说的交通与可视化,你所得到的可以通过以下方式处理:

public static byte[] hexStringToByteArray(String s) throws IOException {
        char[] buf = new char[2];
        int numRead = -1;
        StringReader in = new StringReader(s);
        byte[] bytes = new byte[s.length() / 2];
        int byteIndex = 0;
        while ((numRead = in.read(buf)) > -1) {
            byte b = (byte) Integer.parseInt(String.valueOf(buf), 16);
            bytes[byteIndex++] = b;
        }
        return bytes;
    }

相关问题