Example usage for android.bluetooth BluetoothSocket getRemoteDevice

List of usage examples for android.bluetooth BluetoothSocket getRemoteDevice

Introduction

In this page you can find the example usage for android.bluetooth BluetoothSocket getRemoteDevice.

Prototype

public BluetoothDevice getRemoteDevice() 

Source Link

Document

Get the remote device this socket is connecting, or connected, to.

Usage

From source file:Main.java

public static String getDisplayString(BluetoothSocket socket) {
    return getDisplayString(socket.getRemoteDevice());
}

From source file:org.thaliproject.p2p.btconnectorlib.internal.bluetooth.BluetoothUtils.java

/**
 * Extracts the Bluetooth MAC address of the peer from the given Bluetooth socket instance.
 * @param bluetoothSocket The Bluetooth socket.
 * @return The Bluetooth MAC address or null in case of an error.
 *//*  w ww.  j  av a  2  s .  com*/
public static String getBluetoothMacAddressFromSocket(final BluetoothSocket bluetoothSocket) {
    String bluetoothMacAddress = null;

    if (bluetoothSocket != null && bluetoothSocket.getRemoteDevice() != null) {
        bluetoothMacAddress = bluetoothSocket.getRemoteDevice().getAddress();
    } else {
        // The whole purpose of this method is to have exception free, quick way to get the
        // Bluetooth MAC address. Thus, do not throw an exception here.
        Log.e(TAG, "getBluetoothMacAddressFromSocket: Either the socket or its remote device is null");
    }

    return bluetoothMacAddress;
}

From source file:org.thaliproject.p2p.btconnectorlib.internal.bluetooth.BluetoothUtils.java

/**
 * Creates a new Bluetooth socket based on the given one using the given channel.
 * @param originalBluetoothSocket The original Bluetooth socket.
 * @param channelOrPort The RFCOMM channel or L2CAP psm to use.
 * @param secure If true, will try to create a secure RFCOMM socket. If false, will try to create an insecure one.
 * @return A new Bluetooth socket with the specified channel/port or null in case of a failure.
 *//*from  w w  w . ja  va 2 s.co m*/
public static BluetoothSocket createBluetoothSocket(BluetoothSocket originalBluetoothSocket, int channelOrPort,
        boolean secure) {
    Log.d(TAG, "createBluetoothSocketWithSpecifiedChannel: Channel/port: " + channelOrPort + ", secure: "
            + secure);
    Class<?> bluetoothDeviceClass = originalBluetoothSocket.getRemoteDevice().getClass();
    Class<?>[] parameterTypes = new Class<?>[] { Integer.TYPE };

    String methodName = secure ? METHOD_NAME_FOR_CREATING_SECURE_RFCOMM_SOCKET
            : METHOD_NAME_FOR_CREATING_INSECURE_RFCOMM_SOCKET;

    BluetoothSocket newSocket = null;

    try {
        Method createSocketMethod = bluetoothDeviceClass.getMethod(methodName, parameterTypes);
        Object[] parameters = new Object[] { Integer.valueOf(channelOrPort) };
        newSocket = (BluetoothSocket) createSocketMethod.invoke(originalBluetoothSocket.getRemoteDevice(),
                parameters);
    } catch (Exception e) {
        Log.e(TAG, "createBluetoothSocketWithSpecifiedChannel: Failed to create a new Bluetooth socket: "
                + e.getMessage(), e);
    }

    return newSocket;
}

From source file:com.googlecode.android_scripting.facade.BluetoothFacade.java

public BluetoothConnection(BluetoothSocket mSocket, BluetoothServerSocket mServerSocket) throws IOException {
    this.mSocket = mSocket;
    mOutputStream = mSocket.getOutputStream();
    mInputStream = mSocket.getInputStream();
    mDevice = mSocket.getRemoteDevice();
    mReader = new BufferedReader(new InputStreamReader(mInputStream, "ASCII"));
    this.mServerSocket = mServerSocket;
}