2016-10-05 37 views
0

我想使用RxAndroidBle lib(https://github.com/Polidea/RxAndroidBle)。我想让应用程序开始并扫描BLE设备。我想在LogCat中打印找到的设备。我怎样才能做到这一点?RxAndroidBle扫描

RxBleClient rxBleClient; 
RxBleScanResult rxBleScanResult; 
private Subscription scanSubscription; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rxBleClient = RxBleClient.create(this); 
    Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
     rxBleScanResult.getBleDevice().getMacAddress()); 
} 

回答

0

http://polidea.github.io/RxAndroidBle/

Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
     rxBleScanResult -> { 
      // Process scan result here. 
      Log.e("MainActivity","FOUND :"+ rxBleScanResult.getBleDevice().getName()); 
     }, 
     throwable -> { 
      // Handle an error here. 
     } 
    ); 

// When done, just unsubscribe. 
scanSubscription.unsubscribe(); 

编辑: 我已经注意到,这打破了扫描。即使是比较如果BleScanResult.getBleDevice().getName().equals("BleName") 打破扫描。它只是像3或5个设备一样返回,然后没有其他东西来。

编辑2: 我将离开以前的编辑。有人可能会有同样的问题。某些手机​​(LG G4 Android 6)对于某些蓝牙设备返回null。但其他一些(三星J5 Android 6)不返回空值。这就是我在不同的地方寻找bug的原因。但它的简单,只需添加

if(BleScanResult.getBleDevice().getName()!=null) 

现在它不会中断扫描了。