我正在编写一个应用程序,它可以将数据发送到蓝牙打印机。有人能帮帮我吗?我该如何使用Android蓝牙堆栈进行打印?还是有任何外部API或SDK可供使用?
这里是我的搜索蓝牙设备的代码...
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
registerReceiver(ActionFoundReceiver,
new IntentFilter(BluetoothDevice.ACTION_FOUND));
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName() + "\n"
+ device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};
这是我发送数据到打印机的代码...
BluetoothDevice mDevice = bluetoothAdapter.getRemoteDevice("00:15:FF:F2:56:A4");
Method m = mDevice.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
mBTsocket = (BluetoothSocket) m.invoke(mDevice, 1);
System.out.println("Connecting.....");
mBTsocket.connect();
System.out.println("Connected");
OutputStream os = mBTsocket.getOutputStream();
os.flush();
os.write(Receipt.getBytes());
// mBTsocket.close();
当我写 socket.close() 时,数据无法打印到打印机上,因为在打印数据之前套接字连接已关闭。如果我不写 socket.close() ,则只能打印一次数据。如果不重新启动手机的蓝牙,则无法第二次打印数据。有没有人能为此提供解决方案?或者有没有其他方法来解决这个问题?