Java Code Examples for android.bluetooth.BluetoothSocket#close()

The following examples show how to use android.bluetooth.BluetoothSocket#close() . 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: BTService.java    From esc-pos-android with Apache License 2.0 5 votes vote down vote up
public void run() {
  Log.d(TAG, "Main thread started");
  this.setName("BTMainThread");
  BluetoothSocket socket;

  while (BTService.this.getState() != STATE_CONNECTED) {
    try {
      socket = serverSocket.accept();
    } catch (IOException e) {
      break;
    }

    if (socket != null) {
      synchronized (BTService.this) {
        switch (BTService.this.getState()) {
          case STATE_NONE:
          case STATE_CONNECTED:
            try {
              socket.close();
            } catch (IOException ignored) {
            }
            break;
          case STATE_LISTENING:
          case STATE_CONNECTING:
            onDeviceConnected(socket, socket.getRemoteDevice());
        }
      }
    }
  }
}
 
Example 2
Source File: FirechatBTServer.java    From Rumble with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onClientConnected(BluetoothSocket mmConnectedSocket) {
    try {
        Log.d(TAG, "[-] refusing firechat connection");
        mmConnectedSocket.close();
    } catch(IOException e) {
    }
}
 
Example 3
Source File: DatecsSDKWrapper.java    From cordova-plugin-datecs-printer with MIT License 5 votes vote down vote up
/**
 * Finaliza o socket Bluetooth e encerra todas as conexões
 */
private synchronized void closeBluetoothConnection() {
    BluetoothSocket socket = mBluetoothSocket;
    mBluetoothSocket = null;
    if (socket != null) {
        try {
            Thread.sleep(50);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 4
Source File: MultiplexBluetoothTransport.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void timerDelayRemoveDialog(final BluetoothSocket sock){
	timeOutHandler = new Handler();
	socketRunable = new Runnable() {
        public void run() {
        	//Log.e(TAG, "BLUETOOTH SOCKET CONNECT TIMEOUT - ATTEMPT TO CLOSE SOCKET");
        	try {
	sock.close();
} catch (IOException e) {
	e.printStackTrace();
}         
        }
    };
    timeOutHandler.postDelayed(socketRunable, MS_TILL_TIMEOUT);
}
 
Example 5
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();
        }
    }