Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 从其他活动打印_Android Studio_Android Activity_Bluetooth - Fatal编程技术网

Android studio 从其他活动打印

Android studio 从其他活动打印,android-studio,android-activity,bluetooth,Android Studio,Android Activity,Bluetooth,我设法从我的主要活动建立了蓝牙连接,但我想从另一个活动打印 我该怎么做 启动蓝牙打印机连接的主要活动 protected static final String TAG = "TAG"; private static final int REQUEST_ENABLE_BT = 2; private static final int REQUEST_CONNECT_DEVICE = 1; private ProgressDialog BluetoothConnec

我设法从我的主要活动建立了蓝牙连接,但我想从另一个活动打印

我该怎么做

启动蓝牙打印机连接的主要活动

    protected static final String TAG = "TAG";
    private static final int REQUEST_ENABLE_BT = 2;
    private static final int REQUEST_CONNECT_DEVICE = 1;

    private ProgressDialog BluetoothConnectProgressDialog;

    Button button_connect_to_bluetooth_printer;

    private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothSocket bluetoothSocket;
    private BluetoothDevice bluetoothDevice;

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

        button_connect_to_bluetooth_printer = findViewById(R.id.btn_connect_bluetooth);
        button_connect_to_bluetooth_printer.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (!bluetoothAdapter.isEnabled())
                {
                    Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
                }
                else
                {
                    ListPairedDevices();
                    Intent connectIntent = new Intent(MainActivity.this, BluetoothDeviceListActivity.class);
                    startActivityForResult(connectIntent,
                            REQUEST_CONNECT_DEVICE);
                }
            }
        });
     }

    private void ListPairedDevices()
    {
        Set<BluetoothDevice> PairedDevices = bluetoothAdapter.getBondedDevices();
        if (PairedDevices.size() > 0)
        {
            for (BluetoothDevice mDevice : PairedDevices)
            {
                Log.v(TAG, "PairedDevices: " + mDevice.getName() + mDevice.getAddress());
            }
        }
    }

    public void onActivityResult(int RequestCode, int ResultCode, Intent DataIntent)
    {
        super.onActivityResult(RequestCode, ResultCode, DataIntent);

        switch (RequestCode)
        {
            case REQUEST_CONNECT_DEVICE:
                if (ResultCode == Activity.RESULT_OK)
                {
                    Bundle mExtra = DataIntent.getExtras();
                    String mDeviceAddress = mExtra.getString("DeviceAddress");
                    Log.v(TAG, "Coming incoming address " + mDeviceAddress);
                    bluetoothDevice = bluetoothAdapter
                            .getRemoteDevice(mDeviceAddress);
                    BluetoothConnectProgressDialog = ProgressDialog.show(this, "Connecting...", bluetoothDevice.getName() + " : " + bluetoothDevice.getAddress(), true, true);
                    Thread mBlutoothConnectThread = new Thread(this);
                    mBlutoothConnectThread.start();
                    // pairToDevice(mBluetoothDevice); This method is replaced by
                    // progress dialog with thread
                }
                break;

            case REQUEST_ENABLE_BT:
                if (ResultCode == Activity.RESULT_OK) {
                    ListPairedDevices();
                    Intent connectIntent = new Intent(MainActivity.this,
                            BluetoothDeviceListActivity.class);
                    startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                } else {
                    Toast.makeText(MainActivity.this, "Message", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    public void run()
    {
        try
        {
            bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
            bluetoothAdapter.cancelDiscovery();
            bluetoothSocket.connect();
            handler.sendEmptyMessage(0);
        }
        catch (IOException eConnectException)
        {
            Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
            closeSocket(bluetoothSocket);
            return;
        }
    }
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg)
        {
            BluetoothConnectProgressDialog.dismiss();
        }
    };

    private void closeSocket(BluetoothSocket nOpenSocket)
    {
        try
        {
            nOpenSocket.close();
            Log.d(TAG, "SocketClosed");
        }
        catch (IOException ex)
        {
            Log.d(TAG, "CouldNotCloseSocket");
        }
    }

我怎样才能解决这个问题??应用程序没有崩溃,但也不会打印。

尝试检查您的printbill.java行1635,它显示null指针异常,这意味着该行上有null内容。可能是您未初始化时的ur bluetoothsocket.getoutputstream。
OutputStream os=bluetoothsocket.getoutputstream()是的,这是这一行,但我如何修复它?在像这样使用之前初始化您的bluetoothsocket,您已经完成了上面的操作<代码>bluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(ApplicationUID)如果它不工作,可能是因为
bluetoothDevice.createRfcommSocketToServiceRecord(ApplicationUID)返回null,因此您的套接字也为null
    protected static final String TAG = "TAG";
    private BluetoothAdapter bluetoothAdapter;
    private ArrayAdapter<String> paired_devices_list;

    @Override
    protected void onCreate(Bundle mSavedInstanceState)
    {
        super.onCreate(mSavedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_bluetooth_device_list);

        setResult(Activity.RESULT_CANCELED);
        paired_devices_list = new ArrayAdapter<>(this, R.layout.bluetooth_device_name);

        ListView PairedListDevices = findViewById(R.id.paired_devices);
        PairedListDevices.setAdapter(paired_devices_list);
        PairedListDevices.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                try
                {
                    bluetoothAdapter.cancelDiscovery();
                    String mdeviceInfo = ((TextView) view).getText().toString();
                    String deviceAddress = mdeviceInfo.substring(mdeviceInfo.length() - 17);
                    Log.v(TAG, "Device_Address " + deviceAddress);


                    Bundle bundle = new Bundle();
                    bundle.putString("DeviceAddress", deviceAddress);
                    Intent mBackIntent = new Intent();
                    mBackIntent.putExtras(bundle);
                    setResult(Activity.RESULT_OK, mBackIntent);
                    finish();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> PairedDevices = bluetoothAdapter.getBondedDevices();

        if (PairedDevices.size() > 0)
        {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice mDevice : PairedDevices)
            {
                paired_devices_list.add(mDevice.getName() + "\n" + mDevice.getAddress());
            }
        }
        else
        {
            String NoDevices = "None Paired";
            paired_devices_list.add(NoDevices);
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (bluetoothAdapter != null)
        {
            bluetoothAdapter.cancelDiscovery();
        }
    }
public void print()
    {
        Thread t = new Thread()
        {
            public void run()
            {
                try
                {
                    OutputStream os = bluetoothSocket.getOutputStream();

                    String account_no;

                    account_no_value = ""+ Account_No.getText().toString()+"\n";

                    os.write(account_no.getBytes());

                }
                catch (Exception e)
                {
                    Log.e("PrintActivity", "Exe ", e);
                }
            }
        };
        t.start();
    }
E/PrintActivity: Exe 
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream()' on a null object reference
        at com.vicjames.qiimeterreader.PrintBill$6.run(PrintBill.java:1635)