简体   繁体   中英

Print using Bluetooth printer in android not working

  • I am working on application which allows user to create PDF and also allows to print it using Bluetooth printer.
    • I created PDF but whenever i'm going to integrate print functionality using Bluetooth printer, it occurs error over there.
    • I couldn't get list of Bluetooth device.
    • If you have any sample code for it then please provide it to me.It is too important to me.

Thank You....

This is my code

PrintActivity.java

public class PrintActivity extends Activity implements Runnable {

int idd;

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

Button btnPrinr;
BluetoothAdapter mBluetoothAdapter;

private UUID applicationUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private ProgressDialog mBluetoothConnectProgressDialog;
private BluetoothSocket mBluetoothSocket;
BluetoothDevice mBluetoothDevice;

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

    idd = getIntent().getIntExtra("ID", 0);
    Log.e("ID", "" + idd);

    btnPrinr = (Button) findViewById(R.id.btnPrinr);

    btnPrinr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                Toast.makeText(PrintActivity.this, "Message1", Toast.LENGTH_SHORT).show();
            } else {
                if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                } else {
                    ListPairedDevices();
                    Intent connectIntent = new Intent(PrintActivity.this, DeviceListActivity.class);
                    startActivityForResult(connectIntent, REQUEST_CONNECT_DEVICE);
                }
            }
        }
    });

}

public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent) {
    super.onActivityResult(mRequestCode, mResultCode, mDataIntent);

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

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

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

public void run() {
    try {
        mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
        mBluetoothAdapter.cancelDiscovery();
        mBluetoothSocket.connect();
        mHandler.sendEmptyMessage(0);
    } catch (IOException eConnectException) {
        Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
        closeSocket(mBluetoothSocket);
        return;
    }
}

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

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        mBluetoothConnectProgressDialog.dismiss();
        Toast.makeText(PrintActivity.this, "DeviceConnected", Toast.LENGTH_SHORT).show();
    }
  };
}

DeviceListActivity.java

public class DeviceListActivity extends Activity {

protected static final String TAG = "TAG";
private BluetoothAdapter mBluetoothAdapter;
private ArrayAdapter<String> mPairedDevicesArrayAdapter;
TextView txtName;

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

    setResult(Activity.RESULT_CANCELED);
    mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_raw,R.id.txtName);

    ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
    mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
    mPairedListView.setOnItemClickListener(mDeviceClickListener);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();

    if (mPairedDevices.size() > 0) {
        findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
        for (BluetoothDevice mDevice : mPairedDevices) {
            mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
        }
    } else {
        String mNoDevices = getResources().getText(R.string.none_paired).toString();
        mPairedDevicesArrayAdapter.add(mNoDevices);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mBluetoothAdapter != null) {
        mBluetoothAdapter.cancelDiscovery();
    }
}

private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> mAdapterView, View mView, int mPosition, long mLong) {
        mBluetoothAdapter.cancelDiscovery();
        String mDeviceInfo = ((TextView) mView).getText().toString();
        String mDeviceAddress = mDeviceInfo.substring(mDeviceInfo.length() - 15);
        Log.v(TAG, "Device_Address " + mDeviceAddress);

        Bundle mBundle = new Bundle();
        mBundle.putString("DeviceAddress", mDeviceAddress);
        Intent mBackIntent = new Intent();
        mBackIntent.putExtras(mBundle);
        setResult(Activity.RESULT_OK, mBackIntent);
        finish();
    }
   };
 }

In the code I'm using after this line, there is another line that yours is missing

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();

I hope this helps, I'm only starting with android studio but since no one has answered this question I hope mine helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM