⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servicediscoverylist.java

📁 手机蓝牙功能编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    boolean cancelPendingSearches()
    {
        LogScreen.log("Cancel pending inquiries and searches\n");

        boolean everythingCancelled = true;

        if (inquiryInProgress)
        {
            // Note: The BT API (v1.0) isn't completely clear
            // whether cancelInquiry is blocking or non-blocking.

            if (discoveryAgent.cancelInquiry(this))
            {
                setInquiryInProgress(false);
            }
            else
            {
                everythingCancelled = false;
            }
        }

        for (int i=0; i < transIds.size(); i++)
        {
            // Note: The BT API (v1.0) isn't completely clear
            // whether cancelServiceSearch is blocking or
            // non-blocking?

            Integer pendingId =
                (Integer) transIds.elementAt(i);
            if (discoveryAgent.cancelServiceSearch(
                                   pendingId.intValue()))
            {
                transIds.removeElement(pendingId);
            }
            else
            {
                everythingCancelled = false;
            }
        }
        return everythingCancelled;
    }


    // DiscoveryListener callbacks

    public void deviceDiscovered(RemoteDevice remoteDevice,
                                 DeviceClass deviceClass)
    {
        LogScreen.log("deviceDiscovered: " +
                      remoteDevice.getBluetoothAddress() +
                      " major device class=" +
                      deviceClass.getMajorDeviceClass() +
                      " minor device class=" +
                      deviceClass.getMinorDeviceClass() + "\n");

        // Note: The following check has the effect of only
        // performing later service searches on phones.
        // If you intend to run the MIDlet on some other device (e.g.
        // handheld computer, PDA, etc. you have to add a check
        // for them as well.) You might also refine the check using
        // the minor device class check only cellular phones.

        boolean isPhone =
            (deviceClass.getMajorDeviceClass() == 0x200);

        // Setting the following line to 'true' is a workaround
        // for some early beta SDK device emulators. Set it
        // to false when compiling the MIDlet for download to
        // real MIDP phones!
        boolean isEmulator = false;

        if (isPhone || isEmulator)
        {
            unsearchedRemoteDevices.addElement(remoteDevice);
        }
    }


    public void inquiryCompleted(int discoveryType)
    {
        LogScreen.log("inquiryCompleted: " +
                      discoveryType + "\n");

        setInquiryInProgress(false);

        if (unsearchedRemoteDevices.size() == 0)
        {
            setTitle(TITLE);

            addCommand(searchCommand);
            removeCommand(stopCommand);

            midlet.serviceDiscoveryListError(
                       "No matching Bluetooth devices were found " +
                       "that run the desired service");
        }
    }


    public void servicesDiscovered(int transId,
                                   ServiceRecord[] serviceRecords)
    {
        LogScreen.log("servicesDiscovered: transId=" +
                      transId +
                      " # serviceRecords=" +
                      serviceRecords.length + "\n");

        // Remove+Add: ensure there is at most one open command
        removeCommand(openCommand);
        addCommand(openCommand);

        // Use the bluetooth address
        // to identify the matching Device + ServiceRecord

        // there should only be one record
        if (serviceRecords.length == 1)
        {
            RemoteDevice device = serviceRecords[0].getHostDevice();
            String name = device.getBluetoothAddress();

            // This MIDlet only uses the first matching service
            // record found for a particular device.
            if (!matchingServiceRecords.containsKey(name))
            {
                matchingServiceRecords.put(name, serviceRecords[0]);
                append(name, null);

                // The List should have at least one entry,
                // before we add an open command.
                if (size() == 1)
                {
                    addCommand(openCommand);
                }
            }
        }
        else
        {
            midlet.serviceDiscoveryListError(
                       "Error: Unexpected number (" +
                       serviceRecords.length + ") of service records " +
                       "in servicesDiscovered callback, transId=" +
                       transId);
        }
    }


    public void serviceSearchCompleted(int transId, int responseCode)
    {
        setTitle(TITLE);

        String responseCodeString =
            LogScreen.responseCodeString(responseCode);
        LogScreen.log("serviceSearchCompleted: transId=" +
                    transId +
                    " (" +
                    responseCodeString + ")\n");

        // For any responseCode, decrement the counter
        numServiceSearchesInProgress--;

        // remove the transaction id from the pending list
        for (int i=0; i < transIds.size(); i++)
        {
            Integer pendingId = (Integer) transIds.elementAt(i);
            if (pendingId.intValue() == transId)
            {
                transIds.removeElement(pendingId);
                break;
            }
        }

        // all the searches have completed
        if (!inquiryInProgress && (transIds.size() == 0))
        {
           removeCommand(stopCommand);
           addCommand(searchCommand);

           if (matchingServiceRecords.size() == 0)
           {
                midlet.serviceDiscoveryListError(
                           "No matching services found");
           }
        }
    }


    // Interface Runnable
    public void run()
    {
        Thread currentThread = Thread.currentThread();
        int i = 0;

    running:
        while (thread == currentThread)
        {
            synchronized (this)
            {
                if (thread != currentThread)
                {
                    break running;
                }
                else if (!inquiryInProgress)
                {
                    doServiceSearch();
                }

                if (inquiryInProgress ||
                    numServiceSearchesInProgress > 0)
                {
                    setTitle("Searching " + ACTIVITY[i]);
                    if (++i >= ACTIVITY.length)
                    {
                        i = 0;
                    }
                }


                try
                {
                    wait(WAIT_MILLIS);
                }
                catch (InterruptedException e)
                {
                    // we can safely ignore this
                }
            }
        }
    }


    public void doServiceSearch()
    {
        if ((unsearchedRemoteDevices.size() > 0) &&
            (numServiceSearchesInProgress < sdTransMax))
        {
            synchronized(this)
            {
                RemoteDevice device =
                    (RemoteDevice) unsearchedRemoteDevices
                                       .elementAt(0);

                UUID[] uuids = new UUID[1];
                uuids[0] = uuid;
                try
                {
                    int[] attrSet = null; // default attrSet

                    numServiceSearchesInProgress++;

                    int transId = discoveryAgent.searchServices(
                                                     attrSet,
                                                     uuids,
                                                     device,
                                                     this);

                    LogScreen.log("starting service search on device=" +
                                  device.getBluetoothAddress() +
                                  " transId=" + transId + "\n");

                    transIds.addElement(new Integer(transId));
                    unsearchedRemoteDevices.removeElementAt(0);
                }
                catch (BluetoothStateException e)
                {
                    numServiceSearchesInProgress--;

                    midlet.serviceDiscoveryListError(
                               "Error, could not perform " +
                               "service search: '" +
                               e.getMessage());
                }
            }
        }
    }


    public void remove(Vector alreadyOpen)
    {
        if (size() > 0)
        {
            for (int i=0; i < alreadyOpen.size(); i++)
            {
                // Bluetooth address of slave device that
                // we already have a connection open to
                String btAddress = (String) alreadyOpen.elementAt(i);

                boolean found = false;
                for (int j = 0; j < size(); j++)
                {
                    if (getString(j).equals(btAddress))
                    {
                        found = true;

                        // if the element we are about to
                        // remove is selected, select something else
                        if (getSelectedIndex() == j)
                        {
                            setSelectedIndex(j, false);
                        }
                        delete(j);
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -