/** * Get current instance as singleton. * @return BluetoothController instance */ public static BluetoothController getInstance(){ if (sBluetoothController == null){ synchronized (BluetoothController.class){ if (sBluetoothController == null){ sBluetoothController = new BluetoothController(); } } } return sBluetoothController; }
@Override public void onClick(View v) { mBLEController.disconnect(); } });
@Override public void onClick(View v) { mBluetoothController.setDiscoverable(60); } });
@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)); } }
private void initBT(){ mBluetoothController = BluetoothController.getInstance().build(this); mBluetoothController.setAppUuid(UUID.fromString("fa87c0d0-afac-12de-8a39-0450200c9a66")); mBluetoothController.setBluetoothListener(mListener); tvBTState.setText("Bluetooth state: " + Utils.transBtStateAsString(mBluetoothController.getBluetoothState())); }
@Override public boolean startScan() { if (!isAvailable() && !isEnabled()){ return false; } return mBluetoothAdapter.startDiscovery(); }
@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)); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 4){ if (mBluetoothController != null){ mBluetoothController.release(); } mBluetoothController.build(this); mBluetoothController.setBluetoothListener(mListener); } }
@Override public void onClick(View v) { if (!mBluetoothController.isEnabled()) { mBluetoothController.openBluetooth(); } else { Toast.makeText(ClassicBluetoothActivity.this, "Bluetooth has opened!", Toast.LENGTH_SHORT).show(); } } });
/** * 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); } }
@Override public void run() { cancelScan(); } }, mScanTime);
@Override public void onClick(View v) { mBLEController.reConnect(); } });
/** * Get current instance as singleton. * @return BluetoothLEController instance */ public static BluetoothLEController getInstance(){ if (sBluetoothLEController == null){ synchronized (BluetoothLEController.class){ if (sBluetoothLEController == null){ sBluetoothLEController = new BluetoothLEController(); } } } return sBluetoothLEController; }
@Override protected void onDestroy() { super.onDestroy(); mBLEController.release(); }
@Override protected void onDestroy() { super.onDestroy(); mBluetoothController.release(); } }
@Override public void onClick(View v) { mBluetoothController.startAsServer(); Toast.makeText(ClassicBluetoothActivity.this, "Start as a server!", Toast.LENGTH_SHORT).show(); } });
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String itemStr = mList.get(position); mBLEController.connect(itemStr.substring(itemStr.length() - 17)); } });
@Override public void onClick(View v) { if (mConnectState == State.STATE_CONNECTED) { mBluetoothController.disconnect(); } finish(); } });
@Override public boolean cancelScan() { if (!isAvailable() && !isEnabled()){ return false; } return mBluetoothAdapter.cancelDiscovery(); }
@Override public boolean setDiscoverable(int time){ if (!isAvailable() && !isEnabled()){ return false; } Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, time); mContext.startActivity(intent); return true; }