Java Code Examples for android.bluetooth.BluetoothDevice#getUuids()

The following examples show how to use android.bluetooth.BluetoothDevice#getUuids() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSerializer.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
public Bundle serializeBluetoothDevice(BluetoothDevice data) {
    Bundle result = new Bundle();
    result.putString("Address", data.getAddress());
    final String bondState =
            MbsEnums.BLUETOOTH_DEVICE_BOND_STATE.getString(data.getBondState());
    result.putString("BondState", bondState);
    result.putString("Name", data.getName());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        String deviceType = MbsEnums.BLUETOOTH_DEVICE_TYPE.getString(data.getType());
        result.putString("DeviceType", deviceType);
        ParcelUuid[] parcelUuids = data.getUuids();
        if (parcelUuids != null) {
            ArrayList<String> uuidStrings = new ArrayList<>(parcelUuids.length);
            for (ParcelUuid parcelUuid : parcelUuids) {
                uuidStrings.add(parcelUuid.getUuid().toString());
            }
            result.putStringArrayList("UUIDs", uuidStrings);
        }
    }
    return result;
}
 
Example 2
Source File: ListActivity.java    From myo_AndoridEMG with MIT License 6 votes vote down vote up
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// Device Log
    ParcelUuid[] uuids = device.getUuids();
    String uuid = "";
    if (uuids != null) {
        for (ParcelUuid puuid : uuids) {
            uuid += puuid.toString() + " ";
        }
    }

    String msg = "name=" + device.getName() + ", bondStatus="
            + device.getBondState() + ", address="
            + device.getAddress() + ", type" + device.getType()
            + ", uuids=" + uuid;
    Log.d("BLEActivity", msg);

    if (device.getName() != null && !deviceNames.contains(device.getName())) {
        deviceNames.add(device.getName());
    }
}
 
Example 3
Source File: HidDeviceProfile.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a device supports HID Host profile.
 *
 * @param device Device to check.
 * @return {@code true} if the HID Host profile is supported, {@code false} otherwise.
 */
public boolean isProfileSupported(BluetoothDevice device) {
    // If a device reports itself as a HID Device, then it isn't a HID Host.
    ParcelUuid[] uuidArray = device.getUuids();
    if (uuidArray != null) {
        for (ParcelUuid uuid : uuidArray) {
            if (HID_UUID.equals(uuid) || HOGP_UUID.equals(uuid)) {
                return false;
            }
        }
    }
    return true;
}
 
Example 4
Source File: ScanActivity.java    From android with MIT License 5 votes vote down vote up
private String showUUID(BluetoothDevice device) {
    StringBuilder builder = new StringBuilder();

    ParcelUuid[] uuids = device.getUuids();
    if (null == uuids) {
        return "";
    }
    for (int i = 0; i < uuids.length; i++) {
        ParcelUuid uuid = uuids[i];
        builder.append(uuid.getUuid().toString());
    }

    return builder.toString();
}
 
Example 5
Source File: MainActivity.java    From rubik-robot with MIT License 5 votes vote down vote up
public void connectBluetooth() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, "Enable Bluetooth and pair", Toast.LENGTH_LONG).show();
        return;
    }
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("98:D3:31:30:3B:D8");
    for (BluetoothDevice x : mBluetoothAdapter.getBondedDevices()) {
        Log.d("Main", "Address: " + x.getAddress());
        for (ParcelUuid uuid : x.getUuids()) {
            Log.d("Main", "parceluuid:" + uuid.toString());
        }
    }
    try {
        mBluetoothAdapter.cancelDiscovery();

        BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
        socket.connect();

        mRobotScheduler = new RobotScheduler(socket.getInputStream(), socket.getOutputStream(), 10);
        Toast.makeText(this, "Connected to arduino", Toast.LENGTH_SHORT).show();

        mMapper = new SlightlyMoreAdvancedMapper();

    } catch (IOException e) {
        Log.d("Main", "Exception connecting to bluetooth: ", e);
    }
}
 
Example 6
Source File: TaskerFireReceiver.java    From sony-headphones-control with GNU General Public License v3.0 4 votes vote down vote up
static boolean sendData(Context context, byte[] data) throws IOException, InterruptedException {
        BluetoothDevice headset = null;
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter adapter = bluetoothManager.getAdapter();

        Set<BluetoothDevice> devices = adapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
            ParcelUuid[] uuids = device.getUuids();

            if(uuids == null)
                continue;

            for (ParcelUuid u : uuids) {
                if (u.toString().equals(uuid.toString()) || u.toString().equals(uuid_alt.toString())) {
                    headset = device;
                    break;
                }
            }
            if (headset != null)
                break;
        }
        if (headset == null) {
//            Log.e(TAG, "Headset not found");
            return false;
        } else {
//            Log.d(TAG, "Headset found: " + headset.getAddress() + " " + headset.getName());
        }

        BluetoothSocket socket = headset.createRfcommSocketToServiceRecord(uuid);
        try {
//            Log.i(TAG, "Socket connected: " + socket.isConnected());
            socket.connect();
//            Log.i(TAG, "Socket connected: " + socket.isConnected());

            byte[] packet = new byte[data.length + 2];
            packet[0] = 0x0c;
            packet[1] = 0;
            for (int j = 0; j < data.length; j++) {
                packet[j + 2] = data[j];
            }
            sendPacket(socket, packet);
            packet[1] = 1;
            sendPacket(socket, packet);

            return true;
        } finally {
            socket.close();
        }
    }
 
Example 7
Source File: BluetoothConnectionHelper.java    From DataLogger with MIT License 4 votes vote down vote up
/**
 * Start the ConnectThread to initiate a connection to a remote device.
 *
 * @param address The address to connect
 */
public synchronized void connect(String address, int index) {
    Log.i(TAG, "::connect Trying to connect to address " + address + ". ");

    // Cancel any thread attempting to make a connection
    if (mStates[index] == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThreads[index] != null) {
        mConnectedThreads[index].cancel();
        mConnectedThreads[index] = null;
    }

    // Get the BluetoothDevice object
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    switch (device.getBondState()){
        case BluetoothDevice.BOND_NONE:
            Log.i(TAG, "The remote device is not bonded (paired)");
            break;
        case BluetoothDevice.BOND_BONDING:
            Log.i(TAG, "Bonding (pairing) is in progress with the remote device");
            break;
        case BluetoothDevice.BOND_BONDED:
            Log.i(TAG, "The remote device is bonded (paired)");
            boolean remoteUuid = false;
            for (ParcelUuid uuid : device.getUuids()){
                if (mUUIDs[index].equals(uuid.toString())) {
                    remoteUuid = true;
                }
                Log.i(TAG, "::connect "+ uuid.toString() + ". Is target uuid: " + mUUIDs[index].equals(uuid.toString()));
            }
            if (!remoteUuid) {
                Log.e(TAG, "::connect Service UUID (" + mUUIDs[index] + ") not supported in remote device.");
            }
            break;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device, index);
    mConnectThread.start();
    setState(STATE_CONNECTING, index);

}