Android Open Source - Bluebit Uuid Matcher






From Project

Back to project page Bluebit.

License

The source code is released under:

Apache License

If you think the Android project Bluebit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// vim: et sw=4 sts=4 tabstop=4
/*/*from ww w .j ava 2 s . c  om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.issc.util;

import com.issc.Bluebit;
import com.issc.R;

import android.content.Intent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

public class UuidMatcher {

    Set<UUID> mRules;

    private String mPkgName;
    private String mActName;

    private CharSequence mTitle;
    private CharSequence mDesc;

    private final static String sPREFIX = "0000";
    private final static String sPOSTFIX = "-0000-1000-8000-00805f9b34fb";

    public UuidMatcher() {
        mRules = new TreeSet<UUID>();
    }

    public void setTarget(String pkg, String act) {
        mPkgName = pkg;
        mActName = act;
    }

    public Intent createIntent() {
        Intent i = new Intent();
        i.setClassName(mPkgName, mActName);
        return i;
    }

    public void setInfo(CharSequence title, CharSequence desc) {
        mTitle = title;
        mDesc  = desc;
    }

    public CharSequence getTitle() {
        return mTitle;
    }

    public CharSequence getDesc() {
        return mDesc;
    }

    /**
     * Add rule to this matcher.
     *
     * @param str short Uuid in string. i.e: "1800" for GAP.
     *        It will be converted to 00001800-0000-1000-8000-00805f9b34fb
     */
    public boolean addShortRule(String str) {
        if (!str.matches(".{4}")) {
            return false;
        } else {
            return addRule(fromUuid16(str));
        }
    }

    /**
     * Add rule to this matcher.
     *
     * @param str Uuid in string. It should be 128-bit UUID such as
     *        00001800-0000-1000-8000-00805f9b34fb
     */
    public boolean addRule(String uuidStr) {
        // regulare expression to match something like
        // 00001800-0000-1000-8000-00805f9b34fb
        if (!uuidStr.matches(".{8}-.{4}-.{4}-.{4}-.{12}")) {
            return false;
        } else {
            return addRule(UUID.fromString(uuidStr));
        }
    }

    /**
     * Add rule to this matcher.
     */
    public boolean addRule(UUID newRule) {
        if (mRules.contains(newRule)) {
            return false;
        } else {
            mRules.add(newRule);
            return true;
        }
    }

    public boolean containsShort(String str) {
        if (!str.matches(".{4}")) {
            return false;
        } else {
            return contains(fromUuid16(str));
        }
    }

    public boolean contains(String uuidStr) {
        // regulare expression to match something like
        // 00001800-0000-1000-8000-00805f9b34fb
        if (!uuidStr.matches(".{8}-.{4}-.{4}-.{4}-.{12}")) {
            return false;
        } else {
            return mRules.contains(UUID.fromString(uuidStr));
        }
    }

    public boolean contains(UUID target) {
        return mRules.contains(target);
    }

    /**
     * To determine the relationship between the List and this Rule.
     *
     * @return True if the List is exactly match this Rule.
     */
    public boolean equals(List<UUID> uuids) {
        if (uuids.size() != mRules.size()) {
            return false;
        }

        return (encloses(uuids) && isEnclosedBy(uuids));
    }

    /**
     * To determine the relationship between the List and this Rule.
     *
     * @return True if each uuids of the List is enclosed by this UuidMatcher.
     */
    public boolean encloses(List<UUID> uuids) {
        return mRules.containsAll(uuids);
    }

    /**
     * To determine the relationship between the List and this Rule.
     *
     * @return True if each uuids of this UuidMatcher is enclosed by List.
     *         the List might has more Uuids than this rule.
     */
    public boolean isEnclosedBy(List<UUID> uuids) {
        return uuids.containsAll(mRules);
    }

    /**
     * Expand String from uuid16 to 128-bit UUID.
     *
     * SIG defines BASE_UUID as 00000000-0000-1000-8000-00805F9B34FB.
     * This function helps you to convert 16-bit "0001" to 128-bti UUID.
     */
    public final static String fromUuid16(String shortStr) {
        StringBuilder sb = new StringBuilder();
        sb.append(sPREFIX);
        sb.append(shortStr);
        sb.append(sPOSTFIX);
        return sb.toString();
    }
}




Java Source Code List

com.issc.Bluebit.java
com.issc.data.BLEDevice.java
com.issc.data.UuidsHandler.java
com.issc.gatt.GattAdapter.java
com.issc.gatt.GattCharacteristic.java
com.issc.gatt.GattDescriptor.java
com.issc.gatt.GattServer.java
com.issc.gatt.GattService.java
com.issc.gatt.Gatt.java
com.issc.impl.AlgorithmAIO.java
com.issc.impl.FunctionAdapter.java
com.issc.impl.GattTransaction.java
com.issc.impl.LeService.java
com.issc.impl.aosp.AospGattAdapter.java
com.issc.impl.aosp.AospGattCharacteristic.java
com.issc.impl.aosp.AospGattDescriptor.java
com.issc.impl.aosp.AospGattServer.java
com.issc.impl.aosp.AospGattService.java
com.issc.impl.aosp.AospGatt.java
com.issc.ui.ActivityAIO.java
com.issc.ui.ActivityDeviceChooser.java
com.issc.ui.ActivityDeviceDetail.java
com.issc.ui.ActivityFileChooser.java
com.issc.ui.ActivityFunctionPicker.java
com.issc.ui.ActivityGattServer.java
com.issc.ui.ActivityKeyboard.java
com.issc.ui.ActivityMain.java
com.issc.ui.ActivityServerList.java
com.issc.ui.ActivityTransparent.java
com.issc.ui.ActivityWeight.java
com.issc.ui.FragGattServer.java
com.issc.util.Log.java
com.issc.util.TransactionQueue.java
com.issc.util.Util.java
com.issc.util.UuidMatcher.java
com.issc.widget.LoadingFragment.java