return socket.getInputStream(); return socket.getOutputStream(); return socket.getRemoteDevice().getName(); socket.connect(); return socket.getRemoteDevice().getAddress(); socket.close(); return fallbackSocket.getInputStream(); return fallbackSocket.getOutputStream(); fallbackSocket.connect(); fallbackSocket.close();
@Test public void connect() throws Exception { assertThat(bluetoothSocket.isConnected()).isFalse(); bluetoothSocket.connect(); assertThat(bluetoothSocket.isConnected()).isTrue(); bluetoothSocket.close(); assertThat(bluetoothSocket.isConnected()).isFalse(); } }
private void onConnected(BluetoothSocket socket) throws IOException { binding.connected.setEnabled(true); binding.entry.setEnabled(true); this.socket=socket; out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); responseSub=Bytes.from(socket.getInputStream()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(bytes -> post(new String(bytes)), throwable -> out.close()); }
synchronized private static void zebraPrint() throws IOException { BluetoothAdapter blueTooth = BluetoothAdapter.getDefaultAdapter(); blueTooth.cancelDiscovery(); UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); if (!blueTooth.isEnabled()) blueTooth.enable(); BluetoothDevice blueDevice = blueTooth.getRemoteDevice(printerMac); BluetoothSocket bSocket = blueDevice.createInsecureRfcommSocketToServiceRecord(SERIAL_UUID); if (!bSocket.isConnected()) bSocket.connect(); OutputStream out = bSocket.getOutputStream(); String data = "Your cpcl data"; out.write(text.getBytes()); out.flush(); bSocket.close(); }
/** * Accept a connection to the BluetoothServerSocket we've set up. * @throws IOException */ private void acceptConnection() throws IOException { if (mServerSocket == null) { throw new IOException("ServerSocket is null, not trying to accept()."); } else if (!mBluetoothAdapter.isEnabled()) { throw new IOException("Bluetooth adapter is disabled, not trying to accept()."); } log.info( "Calling mServerSocket.accept()"); mSocket = mServerSocket.accept(); log.info("Accepted socket from " + mSocket.getRemoteDevice()); log.info( "Accepted socket connected? " + mSocket.isConnected()); MurmurService.direction = -1; MurmurService.remoteAddress = mSocket.getRemoteDevice().getAddress(); mExchange = new CryptographicExchange( mContext, mSocket.getRemoteDevice().getAddress(), mSocket.getInputStream(), mSocket.getOutputStream(), false, FriendStore.getInstance(mContext), MessageStore.getInstance(mContext), mContext.mExchangeCallback); //mExchange.execute((Boolean) null); // Start the exchange. (new Thread(mExchange)).start(); }
BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID); socket.connect(); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeChar('x'); // for example socket.close();
@Override public void run() { // always return a remote device BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(mac); mBluetoothAdapter.cancelDiscovery(); mCurrStatus = STATUS.FREE; try { Log.d(TAG, "prepare to connect: " + remoteDevice.getAddress() + " " + remoteDevice.getName()); // BluetoothSocket socket = remoteDevice.createRfcommSocketToServiceRecord(UUID.fromString(STR_UUID)); // BluetoothSocket socket = (BluetoothSocket) remoteDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(remoteDevice,1); mSocket = remoteDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(Constants.STR_UUID)); // if(!socket.isConnected()) mSocket.connect(); mInputStream = mSocket.getInputStream(); mOutputStream = mSocket.getOutputStream(); mCurrStatus = STATUS.CONNECTED; } catch (Exception e) { if (listener != null) listener.onError(e); try { mInputStream.close(); mOutputStream.close(); } catch (IOException closeException) { closeException.printStackTrace(); } mCurrStatus = STATUS.FREE; } } }
private void disconnect() { if (socket!=null) { try { socket.close(); } catch (IOException e) { Log.e(TAG, "Exception from Bluetooth", e); } } }
public void run() { try { nome = socket.getRemoteDevice().getName(); is = new DataInputStream(socket.getInputStream()); os =new DataOutputStream(socket.getOutputStream()); String string; while (true) { string = is.readUTF(); mTelaHandler.obtainMessage(MSG_TEXTO, nome + ": " + string).sendToTarget(); } } catch (IOException e) { e.printStackTrace(); mTelaHandler.obtainMessage(MSG_DESCONECTOU, e.getMessage() + "[3]").sendToTarget(); } } public void iniciar(BluetoothSocket socket) {
/** * May block the current thread and wait until {@link BluetoothDevice} is offered via * {@link #deviceConnected(BluetoothDevice)} method or timeout occurred. * * @return socket of the connected bluetooth device * @throws IOException if socket has been closed, thread interrupted while waiting or timeout has * occurred. */ @Implementation protected BluetoothSocket accept(int timeout) throws IOException { if (closed) { throw new IOException("Socket closed"); } BluetoothSocket socket; try { socket = timeout == -1 ? sockets.take() : sockets.poll(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new IOException(e); } if (socket == null) { throw new IOException("Timeout occurred"); } socket.connect(); return socket; }
@Test public void getOutputStreamSink() throws Exception { bluetoothSocket.getOutputStream().write(DATA); byte[] b = new byte[1024]; int len = shadowOf(bluetoothSocket).getOutputStreamSink().read(b); assertThat(Arrays.copyOf(b, len)).isEqualTo(DATA); }
Log.d(TAG, "Job state is NEW. Run it.."); job.setState(ObdCommandJobState.RUNNING); if (sock.isConnected()) { job.getCommand().run(sock.getInputStream(), sock.getOutputStream()); } else { job.setState(ObdCommandJobState.EXECUTION_ERROR);
@Test public void getInputStreamFeeder() throws Exception { shadowOf(bluetoothSocket).getInputStreamFeeder().write(DATA); InputStream inputStream = bluetoothSocket.getInputStream(); byte[] b = new byte[1024]; int len = inputStream.read(b); assertThat(Arrays.copyOf(b, len)).isEqualTo(DATA); }
devs+=sock.getRemoteDevice().getName()+"\n"; mmSocket.close(); Message msg = handle.obtainMessage(READY_TO_CONN); handle.sendMessage(msg);
try { socket = serverSocket.accept(); if (socket.isConnected()) { Log.d(TAG, "run: connection successfull"); Log.d(TAG, "run: " + socket.getRemoteDevice().getName() + " " + socket.getRemoteDevice().getAddress()); socket.close(); } catch (Exception eee) { Log.d(TAG, "run: " + eee.getMessage());
/** Will cancel an in-progress connection, and close the socket */ public void cancel() { Log.i("ConnectThread","Closing BT connection"); try { if(mmSocket!=null && mmSocket.isConnected()) mmSocket.close(); } catch (IOException e) { Log.e("ConnectThread", "Error on closing bluetooth"); } } }
@Override public void run() { StringBuilder sb = new StringBuilder(); try { Reader reader = new InputStreamReader(socket.getInputStream(), "UTF-8"); char[] buf = new char[512]; while (continueAccepting) { while (sb.indexOf("\n") == -1 && continueAccepting) { int charsRead; if ((charsRead = reader.read(buf)) > 0) { sb.append(buf, 0, charsRead); } } int endIndex = sb.indexOf("\n"); if (endIndex != -1) { String message = sb.substring(0, endIndex + 1); sb.delete(0, endIndex + 1); processMessage(message); } } } catch (IOException e) { Log.e("BluetoothLink/receiving", "Connection to " + socket.getRemoteDevice().getAddress() + " likely broken.", e); disconnect(); } }
@Test public void accept() throws Exception { BluetoothDevice btDevice = ShadowBluetoothDevice.newInstance("DE:AD:BE:EE:EE:EF"); shadowOf(serverSocket).deviceConnected(btDevice); BluetoothSocket clientSocket = serverSocket.accept(); assertThat(clientSocket.getRemoteDevice()).isSameAs(btDevice); }
try { sock = dev.createRfcommSocketToServiceRecord(MY_UUID); sock.connect(); } catch (Exception e1) { Log.e(TAG, "There was an error while establishing Bluetooth connection. Falling back..", e1); Class<?> clazz = sock.getRemoteDevice().getClass(); Class<?>[] paramTypes = new Class<?>[]{Integer.TYPE}; try { Method m = clazz.getMethod("createRfcommSocket", paramTypes); Object[] params = new Object[]{Integer.valueOf(1)}; sockFallback = (BluetoothSocket) m.invoke(sock.getRemoteDevice(), params); sockFallback.connect(); sock = sockFallback; } catch (Exception e2) {