在Android Studio中将字符串从getString转换为getBytes

时间:2018-06-26 00:01:12

标签: android bluetooth qr-code

我想创建一个可以将二维码扫描到文本并通过蓝牙发送的应用程序,因此我必须创建一个可以将我的文本(字符串)传递到另一个活动(蓝牙活动)的程序。问题出在我的第二次活动中,我不得不使用ouputStream(getBytes),这可能导致java.lang.NullPointerException。你能给我一些想法如何转换从getString获得的字符串,以便可以使用getBytes吗?

这是我第二个活动(Bluetooth.java)中的程序

@Override
public void onResume() {
    super.onResume();

    out.append("\n...In onResume...\n...Attempting client connect...");

    // Set up a pointer to the remote node using it's address.
    BluetoothDevice device = btAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    //   A MAC address, which we got above.
    //   A Service ID or UUID.  In this case we are using the
    //     UUID for SPP.
    try {
        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }

    // Discovery is resource intensive.  Make sure it isn't going on
    // when you attempt to connect and pass your message.
    btAdapter.cancelDiscovery();

    // Establish the connection.  This will block until it connects.
    try {
        btSocket.connect();
        out.append("\n...Connection established and data link opened...");
    } catch (IOException e) {
        try {
            btSocket.close();
        } catch (IOException e2) {
            AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
        }
    }

    // Create a data stream so we can talk to server.
    out.append("\n...Sending message to server...");

    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }

    Intent intent = getIntent();
    String message = intent.getExtras().getString("scanContent");
    byte[] msgBuffer = message.getBytes();
    try {
        outStream.write(msgBuffer);
    } catch (IOException e) {
        String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
        if (address.equals("00:00:00:00:00:00"))
            msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
        msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

        AlertBox("Fatal Error", msg);
    }
}

这是我主要活动中的程序:

 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult =   IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

    if (scanningResult != null) {
        String scanContent = scanningResult.getContents();
        contentTxt.setText(scanContent);

        Intent pass_data = new Intent(this,Bluetooth.class);

        pass_data.putExtra("scanContent",scanContent);
        startActivity(pass_data);

    } else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }
}

0 个答案:

没有答案