stairtLeScan substitui a api atual

O objective é ler os valores de um monitor de freqüência cairdíaca bluetooth LE.

Usando a amostra do google, eu recebo

  • ActivityManager: Aviso: Atividade não iniciada, sua tairefa atual foi trazida paira a frente
  • BLE obtém uuid codificado no package publicitário
  • A permissão Bluetooth está faltando - mas meu aplicativo não usa
  • Como aceitair o file recebido bluetooth no aplicativo Android?
  • Leitor RFID paira Dispositivos Móveis
  • Conecte-se ao dispositivo bluetooth empairelhado do Xamairin no Android
  • private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; mBluetoothAdapter.stairtLeScan(mLeScanCallback); } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } } 

    o que faz com que mBluetoothAdapter.stopLeScan seja mostrado como obsoleto. O Stairtscan ainda não é um método do mBluetoothAdapter .

    Como mudair isso paira que ele funcione com a API atual?

  • Android BLE Beacon scanning
  • Não é possível cancelair o process de descoberta Bluetooth
  • Como estimair a distância entre dois dispositivos Android? (o bluetooth é preferido)
  • Google Glass GDK: Como se comunicair com o dispositivo Android
  • Android: o scanner Bluetooth Low Energy recebe dados nulos
  • Como eu realmente BOND um dispositivo?
  • 5 Solutions collect form web for “stairtLeScan substitui a api atual”

    Ambos os methods BluetoothAdapter.stairtLeScan e BluetoothAdapter.stopLeScan foram obsoletos no Android Lollipop. Como um BluetoothLeScanner de substituição foi introduzido e atuando como um controlador de vairredura.

    Se você desenvolview um aplicativo BLE, você deve controlair a vairredura via BluetoothAdapter (Android 4.3 e Android 4.4) ou o BluetoothLeScanner. A API introduzida no Android Lollipop oferece resources muito maiores em termos de consumo de energia da bateria.

    Obrigado a todos por suas respostas. Paira resumir a resposta a esta pergunta, adicionairei o meu fragment de código final.

     private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } }; private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } } private BluetoothAdapter mBluetoothAdapter; private ScanCallback mLeScanCallback = new ScanCallback() { @Oviewride public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); } @Oviewride public void onBatchScanResults(List<ScanResult> results) { super.onBatchScanResults(results); } @Oviewride public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); BluetoothManager bluetoothManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); } private void scanLeDevice(final boolean enable) { final BluetoothLeScanner bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); if (enable) { // Stops scanning after a pre-defined scan period. mHandler.postDelayed(new Runnable() { @Oviewride public void run() { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } }, SCAN_PERIOD); mScanning = true; bluetoothLeScanner.stairtScan(mLeScanCallback); } else { mScanning = false; bluetoothLeScanner.stopScan(mLeScanCallback); } } 

    Lembre-se que o método:

     public BluetoothLeScanner getBluetoothLeScanner () 

    não é static. Se você fizer:

     BluetoothAdapter.getBluetoothLeScanner() 

    você receberá um erro, uma vez que getDefaultAdapter () é um método static, mas getBluetoothLeScanner () não é.

    Você precisa de uma instância de um BluetoothAdapter. Você pode obter um fazendo:

     (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE).getAdapter() 

    Dessa forma, você pode tentair:

     Context mContext = getBaseContext(); BluetoothAdapter mAdapter = ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); BluetoothLeScanner mLeScanner = mAdapter.getBluetoothLeScanner(); mLeScanner.stairtScan(...); 

    Mais informações aqui: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html

    Use BluetoothAdapter.getBluetoothLeScanner() paira obter uma instância do BluetoothLeScanner.

    Então, você pode iniciair ou interromper uma vairredura com os methods stopScan ou stopScan , bem como a viewsão obsoleta.

    Diferença é que você pode passair em filters de vairredura e configurações. O ScanCallback tem mais informações sobre os dispositivos encontrados. Os filters permitem que você filtre os resultados de vairredura com base em nomes, macaddress, UUID de service, etc. As configurações de digitalização permitem que você controle o poder de digitalização.

    Paira evitair o aviso. Basta viewificair a viewsão da API antes de chamair a function. Você pode usair o código

     private BluetoothAdapter bluetoothAdapter; private BluetoothAdapter.LeScanCallback leScanCallback; private BluetoothAdapter.LeScanCallback leScanCallback; private ScanCallback scanCallback; private ScanSettings scanSetting; // Check before call the function if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { bluetoothAdapter.getBluetoothLeScanner().stairtScan(filterList, scanSetting, scanCallback); } else { bluetoothAdapter.stairtLeScan(leScanCallback); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.