getConnectionURL始终返回null

时间:2019-06-07 13:01:00

标签: java bluetooth

我需要编写一个Java应用程序以通过Windows中的spp连接连接到蓝牙设备。 直接使用Windows,我可以关联设备并打开串行端口而没有任何问题。 我将Java(jdk 8)与bluecove-2.1.1堆栈一起使用。 我编写了以下简单应用程序,以编写蓝牙设备的名称及其服务的网址。

public class JavaApplication4 {

    /**
     * @param args the command line arguments
     */

   public static final Vector/*<RemoteDevice>*/ devicesDiscovered = new Vector();

    public static void main(String[] args) throws IOException, InterruptedException {

        final Object inquiryCompletedEvent = new Object();
        final Object serviceSearchCompletedEvent = new Object();

        devicesDiscovered.clear();

        DiscoveryListener listener = new DiscoveryListener() {

            @Override
            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
                devicesDiscovered.addElement(btDevice);
                try {
                    System.out.println("  name " + btDevice.getFriendlyName(false));
                } catch (IOException cantGetDeviceName) {
                }
            }

            @Override
            public void inquiryCompleted(int discType) {
                System.out.println("Device Inquiry completed!");
                synchronized(inquiryCompletedEvent){
                    inquiryCompletedEvent.notifyAll();
                }
            }

            @Override
            public void serviceSearchCompleted(int transID, int respCode) {
                synchronized(serviceSearchCompletedEvent){
                    serviceSearchCompletedEvent.notifyAll();
                }
            }

            @Override
            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                System.out.println("    Service Discovered");
                for (int i = 0; i < servRecord.length; i++) {
                    String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    if (url==null)
                    {
                        url = servRecord[i].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, false);

                    }
                    if (url==null)
                        url = servRecord[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);
                    DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
                    if (serviceName != null) {

                        System.out.println("      service " + serviceName.getValue() + " found " + url);
                    } else if (url != null) {
                        System.out.println("      service found " + url);
                    } else
                    {
                        System.out.println("      service found no name no url" );
                    }

                }
            }
        };

        synchronized(inquiryCompletedEvent) {
            boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            if (started) {
                System.out.println("wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
                System.out.println(devicesDiscovered.size() +  " device(s) found");
                UUID[] searchUuidSet = new UUID[] { 
 //                   new UUID(0x1)    , // SDP 
                    new UUID(0x3)    , // RFCOMM
/*                    new UUID(0x8)    , // OBEX
                    new UUID(0xC)    , // HTTP
                    new UUID(0x100)  , // L2CAP
                    new UUID(0xF)    , // BNEP
                    new UUID(0x1101) , // Serial Port
                    new UUID(0x1000) , // ServiceDiscoveryServerServiceClassID
                    new UUID(0x1001) , // BrowseGroupDescriptorServiceClassID
                    new UUID(0x1002) , // PublicBrowseGroup
                    new UUID(0x1105) , // OBEX Object Push Profile
                    new UUID(0x1106) , // OBEX File Transfer Profile
                    new UUID(0x1115) , // Personal Area Networking User
                    new UUID(0x1116) , // Network Access Point
                    new UUID(0x1117) , // Group Network*/
                };
                int[] attrIDs =  new int[] {
                    0x0100 // Service name
                };
                for(Enumeration en = devicesDiscovered.elements(); en.hasMoreElements(); ) {
                    RemoteDevice btDevice = (RemoteDevice)en.nextElement();
                    synchronized(serviceSearchCompletedEvent) {
                        System.out.println("search services on " + btDevice.getBluetoothAddress() + " " + btDevice.getFriendlyName(false));
                        LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet, btDevice, listener);

                        serviceSearchCompletedEvent.wait();
                    }
                }
            }
        }

    } 

}

上面的代码是通过查看示例代码here制成的。

我要连接的设备名为BT_Device_0166,它始终返回一个空URL。

发现时发现另一台设备的url不为null,但我要使用的设备始终为null。

以下是我得到的输出:

BlueCove version 2.1.1-SNAPSHOT on winsock
wait for device inquiry to complete...
Device 0080E1BA01D7 found
  name ST BTC3.0 Module
Device 886B0F978165 found
  name BT_Device_0166
Device Inquiry completed!
2 device(s) found
search services on 0080E1BA01D7 ST BTC3.0 Module
    Service Discovered
      service Serial Port found btspp://0080E1BA01D7:1;authenticate=false;encrypt=false;master=false
      service iAP found btspp://0080E1BA01D7:2;authenticate=false;encrypt=false;master=false
search services on 886B0F978165 BT_Device_0166
    Service Discovered
      service Bluetooth Serial Port found null
      service found no name no url
      service found no name no url
BlueCove stack shutdown completed

我做错了吗? 是否可以在没有URL的情况下连接到BT串行端口?

0 个答案:

没有答案