Search in sources :

Example 1 with BluetoothSocket

use of android.bluetooth.BluetoothSocket in project kdeconnect-android by KDE.

the class BluetoothLink method sendPackageInternal.

private boolean sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) {
    try {
        BluetoothServerSocket serverSocket = null;
        if (np.hasPayload()) {
            UUID transferUuid = UUID.randomUUID();
            serverSocket = BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord("KDE Connect Transfer", transferUuid);
            JSONObject payloadTransferInfo = new JSONObject();
            payloadTransferInfo.put("uuid", transferUuid.toString());
            np.setPayloadTransferInfo(payloadTransferInfo);
        }
        if (key != null) {
            try {
                np = RsaHelper.encrypt(np, key);
            } catch (Exception e) {
                callback.onFailure(e);
                return false;
            }
        }
        sendMessage(np);
        if (serverSocket != null) {
            BluetoothSocket transferSocket = serverSocket.accept();
            try {
                serverSocket.close();
                int idealBufferLength = 4096;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && transferSocket.getMaxReceivePacketSize() > 0) {
                    idealBufferLength = transferSocket.getMaxReceivePacketSize();
                }
                byte[] buffer = new byte[idealBufferLength];
                int bytesRead;
                long progress = 0;
                InputStream stream = np.getPayload();
                while ((bytesRead = stream.read(buffer)) != -1) {
                    progress += bytesRead;
                    transferSocket.getOutputStream().write(buffer, 0, bytesRead);
                    if (np.getPayloadSize() > 0) {
                        callback.onProgressChanged((int) (100 * progress / np.getPayloadSize()));
                    }
                }
                transferSocket.getOutputStream().flush();
                stream.close();
            } catch (Exception e) {
                callback.onFailure(e);
                return false;
            } finally {
                try {
                    transferSocket.close();
                } catch (IOException ignored) {
                }
            }
        }
        callback.onSuccess();
        return true;
    } catch (Exception e) {
        callback.onFailure(e);
        return false;
    }
}
Also used : BluetoothServerSocket(android.bluetooth.BluetoothServerSocket) JSONObject(org.json.JSONObject) UUID(java.util.UUID) BluetoothSocket(android.bluetooth.BluetoothSocket) JSONException(org.json.JSONException)

Example 2 with BluetoothSocket

use of android.bluetooth.BluetoothSocket in project LEGO-MINDSTORMS-MINDdroid by NXT.

the class BTCommunicator method createNXTconnection.

/**
     * Create a bluetooth connection with SerialPortServiceClass_UUID
     * @see <a href=
     *      "http://lejos.sourceforge.net/forum/viewtopic.php?t=1991&highlight=android"
     *      />
     * On error the method either sends a message to it's owner or creates an exception in the
     * case of no message handler.
     */
public void createNXTconnection() throws IOException {
    try {
        BluetoothSocket nxtBTSocketTemporary;
        BluetoothDevice nxtDevice = null;
        nxtDevice = btAdapter.getRemoteDevice(mMACaddress);
        if (nxtDevice == null) {
            if (uiHandler == null)
                throw new IOException();
            else {
                sendToast(mResources.getString(R.string.no_paired_nxt));
                sendState(STATE_CONNECTERROR);
                return;
            }
        }
        nxtBTSocketTemporary = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);
        try {
            nxtBTSocketTemporary.connect();
        } catch (IOException e) {
            if (myOwner.isPairing()) {
                if (uiHandler != null) {
                    sendToast(mResources.getString(R.string.pairing_message));
                    sendState(STATE_CONNECTERROR_PAIRING);
                } else
                    throw e;
                return;
            }
            // try another method for connection, this should work on the HTC desire, credits to Michael Biermann
            try {
                Method mMethod = nxtDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
                nxtBTSocketTemporary = (BluetoothSocket) mMethod.invoke(nxtDevice, Integer.valueOf(1));
                nxtBTSocketTemporary.connect();
            } catch (Exception e1) {
                if (uiHandler == null)
                    throw new IOException();
                else
                    sendState(STATE_CONNECTERROR);
                return;
            }
        }
        nxtBTsocket = nxtBTSocketTemporary;
        nxtInputStream = nxtBTsocket.getInputStream();
        nxtOutputStream = nxtBTsocket.getOutputStream();
        connected = true;
    } catch (IOException e) {
        if (uiHandler == null)
            throw e;
        else {
            if (myOwner.isPairing())
                sendToast(mResources.getString(R.string.pairing_message));
            sendState(STATE_CONNECTERROR);
            return;
        }
    }
    // everything was OK
    if (uiHandler != null)
        sendState(STATE_CONNECTED);
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) IOException(java.io.IOException) Method(java.lang.reflect.Method) BluetoothSocket(android.bluetooth.BluetoothSocket) IOException(java.io.IOException)

Aggregations

BluetoothSocket (android.bluetooth.BluetoothSocket)2 BluetoothDevice (android.bluetooth.BluetoothDevice)1 BluetoothServerSocket (android.bluetooth.BluetoothServerSocket)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 UUID (java.util.UUID)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1