获取附近蓝牙设备列表后如何(首次连接)配对特定的蓝牙设备(蓝牙扬声器)?

时间:2018-06-14 07:00:51

标签: java bluetooth bluecove

**here is mY code for scanning Nearby devices **
BluetoothExample listener = new BluetoothExample();

    try {
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        DiscoveryAgent agent = localDevice.getDiscoveryAgent();
        agent.startInquiry(DiscoveryAgent.GIAC, listener);

        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("Device Inquiry Completed. ");

**现在我获得了附近可用的蓝牙设备列表。所以我想要的是连接到附近的设备之一。这就是我所做的 1.搜索服务**

agent.searchServices(attrIDs, uuidSet, mainDevice, listener);

        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("Service search finished.");

    } catch (Exception e) {
        e.printStackTrace();
    }
}
  

发现服务

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    for (int i = 0; i < servRecord.length; i++) {
        String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
        if (url == null) {
            continue;
        }
        DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
        if (serviceName != null) {
            System.out.println("service " + serviceName.getValue() + " found " + url);

            if (serviceName.getValue().equals(serviceName.getValue())) {
                try {

                    ClientSession clientSession = (ClientSession) Connector.open(url);
                    HeaderSet hsConnectReply = clientSession.connect(null);
                    System.out.println(hsConnectReply.getResponseCode());
                    System.out.println(ResponseCodes.OBEX_HTTP_OK);
                    if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
                        System.out.println("Failed to connect");
                        return;
                    } else {
                        System.out.println("Connected");
                        HeaderSet hsOperation = clientSession.createHeaderSet();
                        hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
                        hsOperation.setHeader(HeaderSet.TYPE, "text");
                        Operation putOperation = clientSession.put(hsOperation);
                        byte data[] = "Hello World !!!".getBytes("iso-8859-1");
                        OutputStream os = putOperation.openOutputStream();
                        os.write(data);
                        os.close();

                        putOperation.close();
                        clientSession.disconnect(null);
                        clientSession.close();
                        System.out.println("BluetoothExample.servicesDiscovered()");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        } else {
            System.out.println("service found " + url);
        }

    }
}
  

此代码可帮助我连接到附近的设备,如移动设备,但这无法连接到蓝牙扬声器(我有JBL flip 4)。   因此,如果有人能够帮助我第一次从附近发现的蓝牙设备(如蓝牙音箱等)配对特定设备。   代码示例将受到高度赞赏。

0 个答案:

没有答案