android - 在 android 中使用蓝牙打印机打印不工作

标签 android printing bluetooth

  • 我正在开发允许用户创建 PDF 并允许使用蓝牙打印机打印的应用程序。
    • 我创建了 PDF,但每当我要使用蓝牙打印机集成打印功能时,就会出现错误。
    • 我无法获取蓝牙设备列表。
    • 如果您有任何示例代码,请提供给我。这对我来说太重要了。

谢谢你....

This is my code

打印 Activity .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();
    }
   };
 }

最佳答案

在我在这一行之后使用的代码中,还有另一行你的缺失

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

我希望这有帮助,我只是从 android studio 开始,但由于没有人回答这个问题,我希望我的帮助...

关于android - 在 android 中使用蓝牙打印机打印不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791982/

相关文章:

android - 打开上下文菜单 "under"ListView 中的项目

android - 如何在onPause和onResume上对其静音和取消静音

java - 将 Paper 设置为 PageFormatter 不会更新 PageFormatter

java - 如何只打印 h :datatable? 的内容

Android:捕获 BLE 连接失败/断开连接?

安卓和谷歌地图 v2

Android 地理围栏不能作为服务工作

Windows:如何告诉打印机在打印过程中发出 FormFeed?

android - 如何切换电话、电话扬声器、耳机或蓝牙设备的音频输出

android - 是否有任何蓝牙设备唯一标识