/** * 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 public void run() { cancelScan(); } }, mScanTime);
@Override public void onClick(View v) { mBLEController.disconnect(); } });
private void init(){ mBLEController = BluetoothLEController.getInstance().build(this); mBLEController.setBluetoothListener(mBluetoothLEListener); mBLEController.setReadCharacteristic(READ_CHARACTERISTIC_ID); mBLEController.setWriteCharacteristic(WRITE_CHARACTERISTIC_ID); if (!mBLEController.isSupportBLE()){ Toast.makeText(BleActivity.this, "Unsupport BLE!", Toast.LENGTH_SHORT).show(); finish();
/** * start scanning for possible devices who matches the service id * @param serviceUUIDs the list of possible UUIDs to search * @return boolean */ @Override public boolean startScanByService(List<UUID> serviceUUIDs) { if (!isAvailable() && !isEnabled()){ return false; } if (Build.VERSION.SDK_INT >= 21){ mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); mLeSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); mLeFilters = scanFilters(serviceUUIDs); } scanLeDevice(); return true; }
/** * Start the scan of devices * @return boolean */ @Override public boolean startScan() { if (!isAvailable() && !isEnabled()){ return false; } if (Build.VERSION.SDK_INT >= 21){ mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); mLeSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); mLeFilters = new ArrayList<ScanFilter>(); } scanLeDevice(); return true; }
@Override public boolean cancelScan() { if (!isAvailable() && !isEnabled()){ return false; } if (Build.VERSION.SDK_INT < 21) { mBluetoothAdapter.stopLeScan(mLeScanCallback); }else { mLEScanner.stopScan(mCbtScanCallback); } if (mBluetoothListener != null){ mBluetoothListener.onActionDiscoveryStateChanged(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); } return true; }
@Override public void onClick(View v) { mBLEController.reConnect(); } });
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String itemStr = mList.get(position); mBLEController.connect(itemStr.substring(itemStr.length() - 17)); } });