蓝牙打印在Android蓝牙、Android

2023-09-05 01:36:41 作者:风软一江水

我想打印到我的T-Mobile的脉冲迷你手机的HP Deskjet 450wbt打印机,采用了Android蓝牙API。的code为如下所示。连接失败,服务发现失败。如果我试图创造一些其他线程提到套接字的另一种方法,我得到的主机已关闭来代替。

我认为UUID的BPP是正确的,但我不知道。打印机是一种配对的设备,并接通。我无法找到一个USB驱动程序我的手机(贴牌华为8110),所以我一直无法调试设备上,或看日志。我停留在这一点上,我将不胜感激的任何建议。

下面是我的code概要:

 最后弦乐UUID_BPP =00001122-0000-1000-8000-00805F9B34FB;
最后弦乐PRINTERNAME =dj450 S / N SG ...;配对打印机//名称

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

如果(bluetoothAdapter == NULL){
    返回2; //手机不支持蓝牙
}

如果(!bluetoothAdapter.isEnabled()){
    返回3; //蓝牙尚未启用
}

设置< BluetoothDevice类> pairedDevices = bluetoothAdapter.getBondedDevices();

//循环配对设备
BluetoothDevice类打印机= NULL;

对于(BluetoothDevice类设备:pairedDevices){
    字符串s = device.getName();

    如果(s.equals(PRINTERNAME)){
        打印机=设备;
        打破;
    }
}

如果(打印机== NULL)
    返回4; //没有找到配对打印机

//创建套接字
UUID BPP = UUID.fromString(UUID_BPP);
的BluetoothSocket插座;

尝试 {
    插座= printer.createRfcommSocketToServiceRecord(BPP);
}赶上(IOException异常E){
    返回5; //无法创建套接字
}
/ *
尝试 {
    方法M = printer.getClass()。GetMethod的(createRfcommSocket
        新等级[] {int.class});

    插座=(的BluetoothSocket)m.invoke(打印机,1);
}赶上(例外E1){
    返回5; //无法创建套接字
}
* /
尝试 {
    Socket.connect()的;
}赶上(IOException异常E){
    返回6; //无法连接插座
}
 

解决方案

我想原因可能是您正在使用可能不支持BPP配置文件,如果您使用的是它不是标准的Andr​​oid版本在Android上的蓝牙堆栈支持BPP。

套接字创建的机制是失败,因为打印机不执行最高人民检察院的资料。 Android的例子依赖于服务器端SPP在听,以便能够从客户端进行连接。为确保打印机不会有通用SPP服务器侦听。

成都android蓝牙打印机,android热敏打印机厂家

I am trying to print to an HP DeskJet 450wbt printer from my T-Mobile Pulse Mini phone, using the Android Bluetooth API. The code is as shown below. The connection fails with "Service discovery failed". If I try the alternative method of creating a socket mentioned in a number of other threads, I get "Host is down" instead.

I think that the UUID for BPP is correct, but I am not sure. The printer is a paired device, and it is switched on. I cannot find a USB driver for my phone (a badged Huawei 8110), so I have not been able to debug on the device, or look at a log. I am stuck at this point, and I would be grateful for any advice.

Here is a synopsis of my code:

final String UUID_BPP = "00001122-0000-1000-8000-00805F9B34FB";
final String printerName = "dj450 S/N SG..."; // name of paired printer

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter == null) {
    return 2; // phone does not support Bluetooth
}

if (!bluetoothAdapter.isEnabled()) {
    return 3; // Bluetooth has not been enabled
}

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

// Loop through paired devices
BluetoothDevice printer = null;

for (BluetoothDevice device : pairedDevices) {
    String s = device.getName();

    if (s.equals(printerName)) {
        printer = device;
        break;
    }
}

if (printer == null)
    return 4; // Paired printer not found

// create socket
UUID BPP = UUID.fromString(UUID_BPP);
BluetoothSocket socket;

try {
    socket = printer.createRfcommSocketToServiceRecord(BPP);
} catch (IOException e) {
    return 5; // Unable to create socket
}
/*
try {
    Method m = printer.getClass().getMethod("createRfcommSocket", 
        new Class[] { int.class });

    socket = (BluetoothSocket) m.invoke(printer, 1);
} catch (Exception e1) {
    return 5; // Unable to create socket
}
*/
try {
    socket.connect(); 
} catch (IOException e) {
    return 6; // Unable to connect socket
}

解决方案

I think the reason might be that the Bluetooth stack on android you are using might not support the BPP profile , if you are using the standard android versions it does not support BPP.

The mechanism of socket creation is failing because the printer does not implement the SPP profile. The android examples depend on the server side SPP to be listening to be able to connect from the client. The printer for sure will not have generic SPP server listening.