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

The following examples show how to use android.bluetooth.BluetoothDevice#createRfcommSocketToServiceRecord() . 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: BluetoothService.java    From AndroidSmoothBluetooth with Apache License 2.0 6 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (mIsSecure) {
            if (isAndroid) {
                tmp = device.createRfcommSocketToServiceRecord(UUID_ANDROID_DEVICE);
            } else {
                tmp = device.createRfcommSocketToServiceRecord(UUID_OTHER_DEVICE);
            }
        } else {
            if (isAndroid) {
                tmp = device.createInsecureRfcommSocketToServiceRecord(UUID_ANDROID_DEVICE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(UUID_OTHER_DEVICE);
            }
        }
    } catch (IOException e) { }
    mmSocket = tmp;
}
 
Example 2
Source File: BluetoothService.java    From Android-HC05-App with MIT License 6 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        UUID uuid = Constants.myUUID;
        tmp = device.createRfcommSocketToServiceRecord(uuid);
    } catch (IOException e) {
        Log.e(Constants.TAG, "Create RFcomm socket failed", e);
    }
    mmSocket = tmp;
}
 
Example 3
Source File: BluetoothConnectionHelper.java    From DataLogger with MIT License 6 votes vote down vote up
public ConnectThread(BluetoothDevice device, int index) {
    mmDevice = device;
    mIndex = index;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice

    Log.i(TAG, "::ConnectThread Get a BluetoothSocket for a connection at index " + mIndex);
    try {
        tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(mUUIDs[mIndex]));
    } catch (IOException e) {
        Log.e(TAG, "::ConnectThread Socket create() failed at index "+mIndex, e);
    }
    mmSocket = tmp;
}
 
Example 4
Source File: BluetoothClient.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
public BluetoothClient(BluetoothAdapter bluetoothAdapter, BluetoothDevice device, UUID serviceUUID) {
    this.bluetoothAdapter = bluetoothAdapter;

    // Use a temporary object that is later assigned to mSocket,
    // because mSocket is final
    BluetoothSocket tmp = null;
    mDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(serviceUUID);
    } catch (IOException ignored) {
    }
    mSocket = tmp;
}
 
Example 5
Source File: BluetoothClientService.java    From tilt-game-android with MIT License 6 votes vote down vote up
public ConnectThread(BluetoothDevice device, boolean secure) {
    _device = device;
    _socketType = secure ? "Secure" : "Insecure";

    BluetoothSocket socket = null;
    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        if (secure) {
            socket = device.createRfcommSocketToServiceRecord(_secureUuid);
        } else {
            socket = device.createInsecureRfcommSocketToServiceRecord(_insecureUuid);
        }
    } catch (IOException e) {
        Log.e(TAG, "ConnectThread: create failed");
        e.printStackTrace();
    }
    _socket = socket;
}
 
Example 6
Source File: BluetoothUtility.java    From SimpleBluetoothLibrary with Apache License 2.0 6 votes vote down vote up
public ConnectDeviceThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    bluetoothDevice = device;
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(NORMAL_UUID);
    } catch (IOException e) {

    }
    mmSocket = tmp;
}
 
Example 7
Source File: BluetoothChatService.java    From AndroidDemo with MIT License 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
            bluetoothDevice = device;
            BluetoothSocket tmp;
            try {
                tmp = device.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
//                e.printStackTrace();
                tmp = null;
            }
            bluetoothSocket = tmp;
        }
 
Example 8
Source File: BluetoothChatService.java    From Car_remote_control with GNU General Public License v3.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device)
{
	mmDevice = device;
	BluetoothSocket tmp = null;

	try
	{
		tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
	}
	catch (IOException e)
	{
		Log.e(TAG, "create() failed", e);
	}
	mmSocket = tmp;
}
 
Example 9
Source File: BCLServiceClient.java    From unity-bluetooth with MIT License 5 votes vote down vote up
protected ConnectThread(BluetoothDevice device) {
    BluetoothSocket tmp = null;
    mmDevice = device;

    try {
        tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(CHARACTERISTIC_UUID));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    mmSocket = tmp;
}
 
Example 10
Source File: BluetoothManager.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ConnectingThread用のBluetoothSocketを生成するためのヘルパーメソッド
 * @param device
 * @param secure セキュア接続用のBluetoothSocketを生成するならtrue
 * @return
 */
private BluetoothSocket createBluetoothSocket(final BluetoothDevice device,
	final boolean secure) throws IOException {

	// 接続を行うためのBluetoothSocketを生成
	return secure
		? device.createRfcommSocketToServiceRecord(mSecureProfileUUID)				// セキュア接続
		: device.createInsecureRfcommSocketToServiceRecord(mInSecureProfileUUID);	// インセキュア接続
}
 
Example 11
Source File: BluetoothManager.java    From BTChat with GNU General Public License v3.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}
 
Example 12
Source File: BluetoothService.java    From UnityBluetoothPlugin with Apache License 2.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    this.mmDevice = device;
    BluetoothSocket tmp = null;

    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}
 
Example 13
Source File: ConnectThread.java    From PolarHeartRateApplication with MIT License 5 votes vote down vote up
public ConnectThread(BluetoothDevice device, MainActivity ac) {
	// Use a temporary object that is later assigned to mmSocket,
	// because mmSocket is final
	Log.i("ConnectThread", "Starting connectThread");
	this.ac=ac;
	BluetoothSocket tmp = null;
	mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

	// Get a BluetoothSocket to connect with the given BluetoothDevice
	try {
		// MY_UUID is the app's UUID string, also used by the server code
		UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
		tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
	} catch (IOException ignored) {
		Log.e("ConnectThread", "Error on getting the device");
	}
	mmSocket = tmp;

}
 
Example 14
Source File: blueToothDemo.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        mkmsg("Client connection failed: "+e.getMessage().toString()+"\n");
    }
    socket = tmp;
 
}
 
Example 15
Source File: BluetoothService.java    From Android-Bluetooth-Fingerprint with Apache License 2.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device) {
    mmDevice = device;
    BluetoothSocket tmp = null;

    // Get a BluetoothSocket for a connection with the
    // given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        Log.e(TAG, "create() failed", e);
    }
    mmSocket = tmp;
}
 
Example 16
Source File: BluetoothManager.java    From sensorhub with Mozilla Public License 2.0 5 votes vote down vote up
public BluetoothSocket connectToSerialDevice(String deviceNameRegex) throws IOException
{
    BluetoothDevice dev = findDevice(deviceNameRegex);
    UUID spp = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    BluetoothSocket socket = dev.createRfcommSocketToServiceRecord(spp);
    return socket;
}
 
Example 17
Source File: SerialInterface_BT.java    From PodEmu with GNU General Public License v3.0 5 votes vote down vote up
public ConnectThread(BluetoothDevice device)
        {
            mmDevice = device;
            BluetoothSocket tmp = null;

            if(!btAdapter.isEnabled())
            {
                mmSocket = null;
                this.cancel();
//                mConnectedThread.cancel();
                PodEmuService.stopService(baseContext);
                return;
            }

            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try
            {
                PodEmuLog.debug("SIBT: ConnectThread connecting with btdev_uuid_idx=" + btdev_uuid_idx);
                setState(STATE_CONNECTING);
                tmp = device.createRfcommSocketToServiceRecord(BTDEV_UUID[btdev_uuid_idx]);
            }
            catch (IOException e)
            {
                PodEmuLog.debug("SIBT: ConnectThread createRfcomm() failed");
                PodEmuLog.printStackTrace(e);
            }
            mmSocket = tmp;
        }
 
Example 18
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 19
Source File: ReadThread.java    From brailleback with Apache License 2.0 4 votes vote down vote up
private void tryToConnect(List<DeviceFinder.DeviceInfo> bonded) {
    mSocket = null;
    try {
        for (DeviceFinder.DeviceInfo dev : bonded) {
            if (mDisconnecting) {
                return;
            }
            BluetoothDevice bthDev = dev.getBluetoothDevice();
            if (mBluetoothAddressToConnectTo != null
                    && !mBluetoothAddressToConnectTo.equals(
                            bthDev.getAddress())) {
                continue;
            }
            mDisplayService.setConnectionProgress(
                    mResources.getString(R.string.connprog_trying,
                            bthDev.getName()));
            Log.d(LOG_TAG, "Trying to connect to braille device: "
                    + bthDev.getName());
            try {
                BluetoothSocket socket;
                if (dev.getConnectSecurely()) {
                    socket = bthDev
                            .createRfcommSocketToServiceRecord(
                                    dev.getSdpUuid());
                } else {
                    socket = bthDev
                            .createInsecureRfcommSocketToServiceRecord(
                                    dev.getSdpUuid());
                }
                if (socket != null) {
                    socket.connect();
                    mSocket = socket;
                    mConnectedDeviceInfo = dev;
                }
                return;
            } catch (IOException ex) {
                Log.e(LOG_TAG, "Error opening a socket: " + ex.toString());
            }
        }
    } finally {
        if (mSocket == null) {
            mDisplayService.setConnectionProgress(null);
        }
    }
}
 
Example 20
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();
        }
    }