コード例 #1
0
 private void scanLeDevice(final boolean enable) {
   if (enable) {
     mHandler.postDelayed(
         new Runnable() {
           @Override
           public void run() {
             if (Build.VERSION.SDK_INT < 21) {
               mBluetoothAdapter.stopLeScan(mLeScanCallback);
             } else {
               mLEScanner.stopScan(mScanCallback);
             }
           }
         },
         SCAN_PERIOD);
     if (Build.VERSION.SDK_INT < 21) {
       mBluetoothAdapter.startLeScan(mLeScanCallback);
     } else {
       mLEScanner.startScan(filters, settings, mScanCallback);
     }
   } else {
     if (Build.VERSION.SDK_INT < 21) {
       mBluetoothAdapter.stopLeScan(mLeScanCallback);
     } else {
       mLEScanner.stopScan(mScanCallback);
     }
   }
 }
コード例 #2
0
 @Override
 protected void onPause() {
   super.onPause();
   if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
     scanLeDevice(false);
   }
 }
コード例 #3
0
 @Override
 protected void onResume() {
   super.onResume();
   if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
   } else {
     if (Build.VERSION.SDK_INT >= 21) {
       mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
       settings =
           new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
       filters = new ArrayList<ScanFilter>();
     }
     scanLeDevice(true);
   }
 }