[android] BluetoothLeScanner

The method startLeScan(BluetoothAdapter.LeScanCallback) from the type BluetoothAdapter is deprecated

 

롤리팝에서는 startLeScan 대신 bluetoothLeScanner를 쓰라고 나온다.

따라해도 안돼서 삽질했는데, 블루투스가 꺼져있어서 그런거였다 ㅋㅋㅋ

 

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 

public class MainActivity extends Activity
{
  private BluetoothLeScanner leScanner;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!bluetoothAdapter.isEnabled())
      bluetoothAdapter.enable();
    leScanner = bluetoothAdapter.getBluetoothLeScanner();
  }
  
  
  private void scan()
  {
    leScanner.startScan(scanCallback);
  }
  
  private ScanCallback scanCallback = new ScanCallback()
  {
    @Override
    public void onScanResult(int callbackType, ScanResult result)
    {
      super.onScanResult(callbackType, result);
      Log.i("MainActivity.java | onScanResult", "|" + "111111111111111" + "|" + result.getDevice().getName() + "|" + result.getDevice().getAddress());
    }
    
    
    @Override
    public void onScanFailed(int errorCode)
    {
      super.onScanFailed(errorCode);
      Log.i("MainActivity.java | onScanFailed", "|" + "2222222222222" + "|" + errorCode);
    }
    
    
    @Override
    public void onBatchScanResults(List<ScanResult> results)
    {
      super.onBatchScanResults(results);
      for (ScanResult result : results)
      {
        Log.i("MainActivity.java | onBatchScanResults", "33333333333333|" + result.getDevice().getName() + "|" + result.getDevice().getAddress() + "|");
      }
    }
  };
}