List of usage examples for android.bluetooth BluetoothDevice createRfcommSocketToServiceRecord
@RequiresPermission(Manifest.permission.BLUETOOTH) public BluetoothSocket createRfcommSocketToServiceRecord(UUID uuid) throws IOException
From source file:Main.java
public static BluetoothSocket getSocket(BluetoothDevice device) throws IOException { BluetoothSocket socket = device.createRfcommSocketToServiceRecord(PRINTER_UUID); socket.connect();/*from ww w .j a v a2 s . c om*/ return socket; }
From source file:Main.java
static synchronized BluetoothSocket getRfcommSocket(BluetoothDevice device, int numberOfRetries) { BluetoothSocket socket = null;/* w ww .j av a2 s . c o m*/ numberOfRetries = numberOfRetries % 3; switch (numberOfRetries) { case 0: try { if (Build.VERSION.SDK_INT < 10) { socket = getInsecureRfcommSocketByReflection(device); } else { socket = device.createInsecureRfcommSocketToServiceRecord(BLUETOOTH_SPP_PROFILE); } break; } catch (Exception e) { } case 1: try { socket = device.createRfcommSocketToServiceRecord(BLUETOOTH_SPP_PROFILE); break; } catch (Exception e) { } case 2: try { socket = getRfcommSocketByReflection(device); } catch (Exception e) { } break; } return socket; }
From source file:com.authorwjf.bounce.BluetoothChat.java
private void setupChat() { // Initialize the array adapter for the conversation thread mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message); // Initialize the BluetoothChatService to perform bluetooth connections mChatService = new BluetoothChatService(this, mHandler); // Initialize the buffer for outgoing messages mOutStringBuffer = new StringBuffer(""); BluetoothAdapter myAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice remoteDevice = myAdapter.getRemoteDevice(Main.macAddress); try {/*from www . j a va 2s . co m*/ BluetoothSocket btSocket = remoteDevice .createRfcommSocketToServiceRecord(BluetoothChatService.MY_UUID_INSECURE); btSocket.connect(); } catch (IOException exc) { System.out.println("Error connecting"); stopSelf(); } new AutomaticQueryThread(mSendButton, this).start(); }
From source file:com.duy.pascal.interperter.libraries.android.connection.bluetooth.AndroidBluetoothLib.java
@SuppressWarnings("unused") @PascalMethod(description = "Connect to a device over Bluetooth. Blocks until the connection is established or fails.", returns = "True if the connection was established successfully.") public String bluetoothConnect( @PascalParameter(name = "uuid", description = "The UUID passed here must match the UUID used by the server device.") @RpcDefault(DEFAULT_UUID) String uuid, @PascalParameter(name = "address", description = "The user will be presented with a list of discovered devices to choose from if an address is not provided.") @RpcOptional String address) throws IOException { if (address == null) { Intent deviceChooserIntent = new Intent(); deviceChooserIntent.setComponent(Constants.BLUETOOTH_DEVICE_LIST_COMPONENT_NAME); Intent result = mAndroidFacade.startActivityForResult(deviceChooserIntent); if (result != null && result.hasExtra(Constants.EXTRA_DEVICE_ADDRESS)) { address = result.getStringExtra(Constants.EXTRA_DEVICE_ADDRESS); } else {/*from w ww .j a v a 2 s. com*/ return null; } } BluetoothDevice mDevice; BluetoothSocket mSocket; BluetoothConnection conn; mDevice = mBluetoothAdapter.getRemoteDevice(address); mSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid)); // Always cancel discovery because it will slow down a connection. mBluetoothAdapter.cancelDiscovery(); mSocket.connect(); conn = new BluetoothConnection(mSocket); return addConnection(conn); }
From source file:org.mobisocial.corral.CorralClient.java
private Uri getFileOverBluetooth(DbUser user, SignedObj obj) throws IOException { String macStr = DbContactAttributes.getAttribute(mContext, user.getLocalId(), Contact.ATTR_BT_MAC); if (macStr == null) { throw new IOException("No bluetooth mac address for user"); }/*from w ww .j av a2 s. co m*/ String uuidStr = DbContactAttributes.getAttribute(mContext, user.getLocalId(), Contact.ATTR_BT_CORRAL_UUID); if (uuidStr == null) { throw new IOException("No corral uuid for user"); } UUID uuid = UUID.fromString(uuidStr); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = adapter.getRemoteDevice(macStr); BluetoothSocket socket; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { socket = device.createInsecureRfcommSocketToServiceRecord(uuid); } else { socket = device.createRfcommSocketToServiceRecord(uuid); } // TODO: // Custom wire protocol, look for header bits to map to protocol handler. Log.d(TAG, "BJD BLUETOOTH CORRAL NOT READY: can't pull file over bluetooth."); return null; }
From source file:com.googlecode.android_scripting.facade.BluetoothFacade.java
@Rpc(description = "Connect to a device over Bluetooth. Blocks until the connection is established or fails.", returns = "True if the connection was established successfully.") public String bluetoothConnect( @RpcParameter(name = "uuid", description = "The UUID passed here must match the UUID used by the server device.") @RpcDefault(DEFAULT_UUID) String uuid, @RpcParameter(name = "address", description = "The user will be presented with a list of discovered devices to choose from if an address is not provided.") @RpcOptional String address) throws IOException { if (address == null) { Intent deviceChooserIntent = new Intent(); deviceChooserIntent.setComponent(Constants.BLUETOOTH_DEVICE_LIST_COMPONENT_NAME); Intent result = mAndroidFacade.startActivityForResult(deviceChooserIntent); if (result != null && result.hasExtra(Constants.EXTRA_DEVICE_ADDRESS)) { address = result.getStringExtra(Constants.EXTRA_DEVICE_ADDRESS); } else {/* w w w . java 2 s . c o m*/ return null; } } BluetoothDevice mDevice; BluetoothSocket mSocket; BluetoothConnection conn; mDevice = mBluetoothAdapter.getRemoteDevice(address); mSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(uuid)); // Always cancel discovery because it will slow down a connection. mBluetoothAdapter.cancelDiscovery(); mSocket.connect(); conn = new BluetoothConnection(mSocket); return addConnection(conn); }
From source file:org.apache.cordova.plugin.BluetoothPlugin.java
/** * helper function//from ww w. ja v a 2 s .co m * @param device * @param uuid * @return * @throws IOException */ private BluetoothSocket connectSecureHelper(BluetoothDevice device, UUID uuid) throws IOException { BluetoothSocket bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid); bluetoothSocket.connect(); return bluetoothSocket; }
From source file:org.mobisocial.corral.CorralDownloadClient.java
private Uri getFileOverBluetooth(DbIdentity user, SignedObj obj, CorralDownloadFuture future, DownloadProgressCallback callback) throws IOException { callback.onProgress(DownloadState.PREPARING_CONNECTION, DownloadChannel.BLUETOOTH, 0); String macStr = DbContactAttributes.getAttribute(mContext, user.getLocalId(), DbContactAttributes.ATTR_BT_MAC); if (macStr == null) { throw new IOException("No bluetooth mac address for user"); }/*from w ww . j a v a 2s . com*/ String uuidStr = DbContactAttributes.getAttribute(mContext, user.getLocalId(), DbContactAttributes.ATTR_BT_CORRAL_UUID); if (uuidStr == null) { throw new IOException("No corral uuid for user"); } UUID uuid = UUID.fromString(uuidStr); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = adapter.getRemoteDevice(macStr); BluetoothSocket socket; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { socket = device.createInsecureRfcommSocketToServiceRecord(uuid); } else { socket = device.createRfcommSocketToServiceRecord(uuid); } // TODO: // Custom wire protocol, look for header bits to map to protocol handler. Log.d(TAG, "BJD BLUETOOTH CORRAL NOT READY: can't pull file over bluetooth."); return null; }
From source file:org.lsc.hellocordova.BluetoothPlugin.java
private void openSocket(String addressDevice) throws IOException { BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard //SerialPortService ID mmSocket = device.createRfcommSocketToServiceRecord(uuid); mmSocket.connect();/*from w w w . java2 s. c om*/ mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); beginListenForData(); Log.i("BluetoothPlugin", "Bluetooth Sockets Opened"); }
From source file:com.android.server.MigratorService.java
private void connect(BluetoothDevice device) { BluetoothSocket clientSocket;/* w ww. ja va 2 s . c o m*/ try { clientSocket = device.createRfcommSocketToServiceRecord(MIGRATE_UID); } catch (IOException e) { return; } if (clientSocket != null) { if (mAdapter.isDiscovering()) { mAdapter.cancelDiscovery(); } try { clientSocket.connect(); if (debug) Log.w(TAG, "connect"); } catch (IOException e) { try { clientSocket.close(); } catch (IOException e1) { return; } } long base = System.nanoTime(); write(clientSocket); } else { if (debug) Log.w(TAG, "socket is null"); } }