Android 上的蓝牙打印

发布于 2024-10-21 07:53:26 字数 1623 浏览 2 评论 0原文

我正在尝试使用 Android 蓝牙 API 从 T-Mobile Pulse Mini 手机打印到 HP DeskJet 450wbt 打印机。代码如下所示。连接失败并显示“服务发现失败”。如果我尝试创建许多其他线程中提到的套接字的替代方法,我会得到“主机已关闭”。

我认为 BPP 的 UUID 是正确的,但我不确定。打印机是已配对的设备,并且已打开。我找不到适用于我的手机(带有标记的华为 8110)的 USB 驱动程序,因此我无法在设备上进行调试或查看日志。我被困在这一点上,我将不胜感激任何建议。

这是我的代码的概要:

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 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌伤浅笑 2024-10-28 07:53:26

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

由于打印机未实现 SPP 配置文件,套接字创建机制失败。 android 示例依赖于服务器端 SPP 进行监听,以便能够从客户端进行连接。打印机肯定不会有通用 SPP 服务器侦听。

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.

旧时模样 2024-10-28 07:53:26

试试这个:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);

Try this:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
川水往事 2024-10-28 07:53:26

有多种蓝牙配置文件可用于打印,即 HCRP、BPP、SPP、OPP、DirectPrinting 等。(参考:http://www.alanjmcf.me.uk/comms/bluetooth/Bluetooth%20Profiles%20and%2032feet.NET.html#_Toc266869878)

尝试使用这些其他配置文件 (UUID) 来尝试与打印机进行“socket.connect”...
也许您的 Palm OS 设备会使用 BPP 之外的其他配置文件?

顺便问一下,Philip,对于您的打印机 HP DeskJet 450wbt,您使用的是通用蓝牙适配器还是专为您的打印机从 HP 购买的产品?

或者您的打印机是否支持开箱即用的蓝牙,无需插入蓝牙适配器?

也许你可以在这里回答我的问题:
可以使用通用蓝牙适配器支持通过蓝牙打印的打印机?

让我知道它对您来说效果如何。

There are various bluetooth profiles that maybe used for printing namely, HCRP, BPP, SPP, OPP, DirectPrinting etc. (Ref: http://www.alanjmcf.me.uk/comms/bluetooth/Bluetooth%20Profiles%20and%2032feet.NET.html#_Toc266869878)

Try these other profiles (UUIDs) to try a 'socket.connect'with the printer...
Perhaps your Palm OS device would be using another profile than BPP?

By the way Philip, for your printer - HP DeskJet 450wbt, are you using a generic Bluetooth dongle or something you purchased specifically for your printer from HP?

Or does your printer support Bluetooth out of the box, without need to plugin a Bluetooth adapter?

Perhaps you can answer my question here:
can generic Bluetooth dongle be used for printers that support printing over Bluetooth?

Let me know how it works out for you.

我们只是彼此的过ke 2024-10-28 07:53:26

如果您的手机型号(此处为 Android)内置了对蓝牙打印配置文件的支持,则该手机支持打印到蓝牙打印机。如果您在手机上没有看到任何用于打印(例如,从内置照片库应用程序)到蓝牙打印机的设置/选项,则手机很可能不支持蓝牙打印。

如果支持不是内置的,那么您几乎没有机会添加支持;它必须来自设备制造商(此处为 Android)。

(参考:http://discussion。 forum.nokia.com/forum/showthread.php?76295-Bluetooth-printing-option-in-application)


为了在 Android 上实现蓝牙配置文件支持,有一个名为“Sybase-iAnywhere-Blue- SDK-for-Android”,它取代了 Android 的版本,并提供了底层蓝牙配置文件和协议的所有接口。使用此 SDK 提供的 BPP 配置文件,您可以使用 Android 手机通过蓝牙进行打印。

请参阅以下链接了解更多详细信息:
链接1:
http://www.sybase.com/detail?id=1064424
链接2:
http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks

If your phone model (here Android) has built-in support for the Bluetooth Printing Profile, then the phone supports printing to a Bluetooth printer. If you do not see any settings/options on your phone to print (from, for example, the built-in photo gallery applications) to a Bluetooth printer, then in all likelihood the phone does not support Bluetooth printing.

If the support is not built-in, there is little or no chance for you to add the support; it has to come from the device manufacturer(here Android).

(Ref: http://discussion.forum.nokia.com/forum/showthread.php?76295-Bluetooth-printing-option-in-application)


For Bluetooth profile support to be implemented on Android, there is a project called “Sybase-iAnywhere-Blue-SDK-for-Android”, which replaces Android's version, and provides all interfaces into the underlying Bluetooth profiles and protocols. Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK.

See links below for more details:
link 1:
http://www.sybase.com/detail?id=1064424
Link 2:
http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文