/** * Build this instance. * @param context the current context you use * @return BluetoothController instance */ public BluetoothController build(Context context){ mContext = context; mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothService = new BluetoothService(); return this; }
/** * Get current SDP recorded UUID. * @return an UUID */ public UUID getAppUuid(){ if (mBluetoothService != null){ return mBluetoothService.getAppUuid(); } return null; }
@Override public BluetoothDevice getConnectedDevice() { if (mBluetoothService != null){ return mBluetoothService.getConnectedDevice(); } return null; } }
public void run() { mAdapter.cancelDiscovery(); try { mmSocket.connect(); } catch (IOException e) { setState(co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN); try { mmSocket.close(); } catch (IOException e2) {} BluetoothService.this.start(); return; } synchronized (BluetoothService.this) { mConnectThread = null; } connected(mmSocket); }
/** * Stop all threads. */ public synchronized void stop() { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } mBluetoothDevice = null; setState(State.STATE_NONE); }
@Override public void connect(String mac){ if (!isAvailable() || !isEnabled()){ throw new RuntimeException("Bluetooth is not avaliable!"); } if (mBluetoothAdapter.isDiscovering()){ mBluetoothAdapter.cancelDiscovery(); } if (mBluetoothService != null){ mBluetoothService.connect(mBluetoothAdapter.getRemoteDevice(mac)); } }
public int getConnectionState(){ if (mBluetoothService != null){ return mBluetoothService.getState(); } return State.STATE_UNKNOWN; }
@Override public void startAsServer() { if (mBluetoothService != null){ mBluetoothService.start(); } }
/** * Set an UUID for SDP record. * @param uuid an UUID */ public void setAppUuid(UUID uuid){ if (mBluetoothService != null){ mBluetoothService.setAppUuid(uuid); } }
/** * Set bluetooth listener, you can check all bluetooth status and read data with this listener's callback. * @param listener a BluetoothListener */ public void setBluetoothListener(BluetoothListener listener){ this.mBluetoothListener = listener; registerReceiver(); if (mBluetoothService != null) { mBluetoothService.setBluetoothListener(mBluetoothListener); } }
public void run() { BluetoothSocket socket = null; while (mState != co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED) { try { socket = mmServerSocket.accept(); } catch (IOException e) { break; } if (socket != null) { synchronized (BluetoothService.this) { switch (mState) { case co.lujun.lmbluetoothsdk.base.State.STATE_LISTEN: case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTING: case co.lujun.lmbluetoothsdk.base.State.STATE_DISCONNECTED: connected(socket); break; case co.lujun.lmbluetoothsdk.base.State.STATE_NONE: case co.lujun.lmbluetoothsdk.base.State.STATE_CONNECTED: try { socket.close(); } catch (IOException e) {} break; } } } } }
/** * Start the ConnectThread to initiate a connection to a remote device. * @param device The BluetoothDevice to connect */ public synchronized void connect(BluetoothDevice device) { if (mState == State.STATE_CONNECTING && mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } mConnectThread = new ConnectThread(device); mConnectThread.start(); setState(State.STATE_CONNECTING); }
@Override public void reConnect(String mac) { if (!isAvailable() || !isEnabled()){ throw new RuntimeException("Bluetooth is not avaliable!"); } if (mBluetoothAdapter.isDiscovering()){ mBluetoothAdapter.cancelDiscovery(); } if (getConnectionState() == State.STATE_DISCONNECTED && mBluetoothService != null && mac != null){ mBluetoothService.connect(mBluetoothAdapter.getRemoteDevice(mac)); } }
/** * Start AcceptThread to begin a session in listening (server) mode. */ public synchronized void start() { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } if (mAcceptThread == null) { mAcceptThread = new AcceptThread(); mAcceptThread.start(); } setState(State.STATE_LISTEN); }
/** * Start the ConnectedThread to begin managing a Bluetooth connection. * @param socket The BluetoothSocket on which the connection was made */ public synchronized void connected(BluetoothSocket socket) { if (mConnectThread != null) { mConnectThread.cancel(); mConnectThread = null; } if (mConnectedThread != null) { mConnectedThread.cancel(); mConnectedThread = null; } if (mAcceptThread != null) { mAcceptThread.cancel(); mAcceptThread = null; } mConnectedThread = new ConnectedThread(socket); mConnectedThread.start(); mBluetoothDevice = socket.getRemoteDevice(); setState(State.STATE_CONNECTED); }