Android Open Source - BluetoothHidEmu Socket Manager






From Project

Back to project page BluetoothHidEmu.

License

The source code is released under:

Apache License

If you think the Android project BluetoothHidEmu listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package andraus.bluetoothhidemu.sock;
/*w  w w. ja va 2s .co  m*/
import java.io.IOException;

import andraus.bluetoothhidemu.BluetoothHidEmuActivity;
import andraus.bluetoothhidemu.sock.payload.HidPayload;
import andraus.bluetoothhidemu.spoof.BluetoothAdapterSpoofer;
import andraus.bluetoothhidemu.util.DoLog;
import andraus.bluetoothhidemu.view.BluetoothDeviceView;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

/**
 * Singleton
 * 
 */
public class SocketManager {
    
    private String TAG = BluetoothHidEmuActivity.TAG;
    
    private static SocketManager mInstance = null;
    
    public static final int STATE_NONE = BluetoothSocketThread.STATE_NONE;
    public static final int STATE_WAITING = BluetoothSocketThread.STATE_WAITING;
    public static final int STATE_ACCEPTED = BluetoothSocketThread.STATE_ACCEPTED;
    public static final int STATE_DROPPING = BluetoothSocketThread.STATE_DROPPING;
    public static final int STATE_DROPPED = BluetoothSocketThread.STATE_DROPPED;
    
    private BluetoothAdapterSpoofer mSpoofer = null;
    
    private BluetoothSocketThread mCtrlThread = null;
    private BluetoothSocketThread mIntrThread = null;
    
    /**
     * 
     * @param bluetoothAdapter
     * @param connHelper
     */
    private SocketManager(BluetoothAdapterSpoofer connHelper) {
        mSpoofer = connHelper;
    }

    /**
     * 
     * @param connHelper
     * @return
     */
    public static SocketManager getInstance(BluetoothAdapterSpoofer connHelper) {
        if (mInstance == null) {
            mInstance = new SocketManager(connHelper);
        }
        
        return mInstance;
    }


    /**
     * 
     */
    public void destroyThreads() {
        mCtrlThread = null;
        mIntrThread = null;

    }
    
    /**
     * 
     * @param hidPayload
     */
    public void sendPayload(HidPayload hidPayload) {

      if (mIntrThread != null && mIntrThread.isAlive()) {
            mIntrThread.sendBytes(hidPayload.getPayload());
        }
    }

    /**
     * 
     * @param thread
     * @param name
     * @param hostDevice
     * @param socketPort
     * @return
     */
    private BluetoothSocketThread initThread(BluetoothSocketThread thread, 
                    String name, BluetoothDevice hostDevice, int socketPort) {
        
        BluetoothSocket socket;

        try {
            socket = mSpoofer.connectL2capSocket(hostDevice, socketPort, true, true);
        } catch (IOException e) {
            DoLog.e(TAG, String.format("Cannot acquire %sSocket", name), e);
            throw new RuntimeException(e);
        }
        
        if (socket != null) {
            DoLog.d(TAG, String.format("%s socket successfully created: %s", name, socket));
        }
        return new BluetoothSocketThread(socket, name);
    }
    
    
    /**
     * Init sockets and threads
     * 
     * @param adapter
     * @param device
     */
    public void startSockets(BluetoothAdapter adapter, BluetoothDeviceView deviceView) {
        
        BluetoothDevice device = deviceView.getBluetoothDevice();
        
        if (device == null) {
            DoLog.w(TAG, "no hosts not found");
            return;
        } else {
            DoLog.d(TAG, "host selected: " + device);
        }
    
        // discovery is a heavy process. Apps must always cancel it when connecting.
        adapter.cancelDiscovery();
        
        mCtrlThread = initThread(mCtrlThread, "ctrl", device, 0x11);
        mIntrThread = initThread(mIntrThread, "intr", device, 0x13);
        
        mCtrlThread.start();
        mIntrThread.start();
    }
    
    /**
     * Stop L2CAP "control" and "interrupt" channel threads
     */
    public void stopSockets() {

        DoLog.d(TAG, "stop bluetooth connections");
        
        if (mIntrThread != null) {
            mIntrThread.sendBytes(HidPayload.disconnectReq());
            mIntrThread.stopGracefully();
            mIntrThread = null;
        }

        if (mCtrlThread != null) {
            mCtrlThread.stopGracefully();
            mCtrlThread = null;
        }
        
    }
    
    public boolean checkState(int state) {
        return mCtrlThread != null && (mCtrlThread.getConnectionState() == state || mIntrThread.getConnectionState() == state);
    }

}




Java Source Code List

andraus.bluetoothhidemu.BluetoothHidEmuActivity.java
andraus.bluetoothhidemu.ButtonClickListener.java
andraus.bluetoothhidemu.Constants.java
andraus.bluetoothhidemu.KeyboardKeyListener.java
andraus.bluetoothhidemu.KeyboardTextWatcher.java
andraus.bluetoothhidemu.SpecialKeyListener.java
andraus.bluetoothhidemu.TouchpadListener.java
andraus.bluetoothhidemu.settings.BluetoothAdapterStateReceiver.java
andraus.bluetoothhidemu.settings.BluetoothDeviceStateReceiver.java
andraus.bluetoothhidemu.settings.Settings.java
andraus.bluetoothhidemu.sock.BluetoothSocketThread.java
andraus.bluetoothhidemu.sock.SocketManager.java
andraus.bluetoothhidemu.sock.payload.HidConsumerPayload.java
andraus.bluetoothhidemu.sock.payload.HidKeyPair.java
andraus.bluetoothhidemu.sock.payload.HidKeyboardPayload.java
andraus.bluetoothhidemu.sock.payload.HidPayload.java
andraus.bluetoothhidemu.sock.payload.HidPointerPayload.java
andraus.bluetoothhidemu.sock.payload.HidSixaxisPayload.java
andraus.bluetoothhidemu.spoof.BluetoothAdapterSpooferFactory.java
andraus.bluetoothhidemu.spoof.BluetoothAdapterSpooferGeneric.java
andraus.bluetoothhidemu.spoof.BluetoothAdapterSpooferMotoReflect.java
andraus.bluetoothhidemu.spoof.BluetoothAdapterSpooferMoto.java
andraus.bluetoothhidemu.spoof.BluetoothAdapterSpoofer.java
andraus.bluetoothhidemu.spoof.CleanupExceptionHandler.java
andraus.bluetoothhidemu.spoof.Spoof.java
andraus.bluetoothhidemu.spoof.jni.BluetoothSocketJni.java
andraus.bluetoothhidemu.ui.GenericUiControls.java
andraus.bluetoothhidemu.ui.Ps3KeypadUiControls.java
andraus.bluetoothhidemu.ui.UiControls.java
andraus.bluetoothhidemu.util.DoLog.java
andraus.bluetoothhidemu.view.ArrowButton.java
andraus.bluetoothhidemu.view.BluetoothDeviceArrayAdapter.java
andraus.bluetoothhidemu.view.BluetoothDeviceView.java
andraus.bluetoothhidemu.view.EchoEditText.java
andraus.bluetoothhidemu.view.ViewUtils.java