1

我必须将消息从我的(Ubuntu-12.04,64 位)笔记本电脑发送到蓝牙电话,其友好名称作为参数提供。此手机之前可能已与笔记本电脑连接/配对,也可能未配对。我正在使用此处所示的 Java 代码将消息从我的笔记本电脑发送到蓝牙设备。

package com.test.bt;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;

public class BluetoothTest {

    /**
     * @param args
     * @throws InterruptedException
     * @throws IOException
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) throws InterruptedException,
            BluetoothStateException, UnsupportedEncodingException, IOException {
        BluetoothDiscoveryListener bdl = new BluetoothDiscoveryListener();

        LocalDevice ld = LocalDevice.getLocalDevice();
        DiscoveryAgent da = ld.getDiscoveryAgent();
        da.startInquiry(DiscoveryAgent.GIAC, bdl);
        bdl.doLock();

        UUID[] uuidSet = new UUID[1];
        uuidSet[0] = new UUID(0x1105); // OBEX Object Push service
        int[] attrSet = new int[] { 0x0100 }; // Service name

        boolean deviceFound = false;
        for (RemoteDevice rd : bdl.remoteDevices) {
            if (rd.getFriendlyName(false).matches(args[0])) {
                deviceFound = true;
                da.searchServices(attrSet, uuidSet, rd, bdl);
                bdl.doLock();

                bdl.sendMessage("Bluetooth.txt", "text", args[1]);
            }
        }
        if (!deviceFound) {
            System.err.println("Requested device was not found.");
        }
    }
}

它仅在设备之前已配对时才有效。否则,它会失败,如下所示。实际上,如果 PIN 码算术并且我说是,我会在手机上收到提示。然后,下面的这个失败。

BlueCove version 2.1.0 on bluez
Device Phone found at address 101DC0F72211
    MAJOR device class 512
    MINOR device class 4
    MAJOR service classes 5898240
Inquiry completed successfully.
Following services are found ... 
     Service name : OPP
      URL is : btgoep://101DC0F72211:3;authenticate=false;encrypt=false;master=false
Services search completed successfully.
Connecting to btgoep://101DC0F72211:3;authenticate=false;encrypt=false;master=false
Exception in thread "main" java.io.IOException: Failed to connect. [115] Operation now in progress
    at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl(Native Method)
    at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnection(BluetoothStackBlueZ.java:560)
    at com.intel.bluetooth.BluetoothRFCommClientConnection.<init>(BluetoothRFCommClientConnection.java:37)
    at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:387)
    at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:162)
    at javax.microedition.io.Connector.open(Connector.java:83)
    at com.test.bt.BluetoothDiscoveryListener.sendMessage(BluetoothDiscoveryListener.java:106)
    at com.test.bt.BluetoothTest.main(BluetoothTest.java:52)
BlueCove stack shutdown completed

我尝试了各种身份验证和加密选项;但是,没有运气。

即使设备已经配对,我也注意到了另一种我不理解的行为。如果设备配对成功,我想我可以使用该retrieveDevices(DiscoveryAgent.CACHED)方法或该retrieveDevices(DiscoveryAgent.PREKNOWN)方法。两者都返回 null。

那么,如何向之前未与笔记本电脑配对的手机发送消息?

4

0 回答 0