/**
* 搜索Ble设备
* @param enable
*/
public void scanBle(boolean enable) {
// TODO Auto-generated method stub
if (!isBleAvailable) {
Intent intent = new Intent();
intent.setAction(ActionBleNotify.ACTION_BLE_NOT_AVAILABLE);
mContext.sendBroadcast(intent);
return;
}
if (isBleEnabled()) {
if (enable) {
// Stops scanning after a pre-defined scan period.
if(mScanning){
scanBle(false);
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if(mScanning){
if (mOnBleScanListener!=null) {
mOnBleScanListener.stop();
}
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
}
}, SCAN_PERIOD);
if (mOnBleScanListener!=null) {
mOnBleScanListener.start();
}
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
// mBluetoothAdapter.startLeScan(BTTempBLEService.uuids, mLeScanCallback);
} else {
if (mOnBleScanListener!=null) {
mOnBleScanListener.stop();
}
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
} else {
Intent intent = new Intent();
intent.setAction(ActionBleNotify.ACTION_BLE_NOT_ENABLED);
mContext.sendBroadcast(intent);
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback;
很多人说用stopScan()、startScan()方法替代,如果是,请告诉如何替代,谢谢!
BLE扫描架构变了,不是这样简单直接替代。我的代码这会看不到,有空了帖出来给你
嗯,方法上面有说明过时的替代方案为 BluetoothLeScanner#stopScan(ScanCallback)}。
具体代码示例:
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner()
if(scanner != null){
scanner.canner.stopScan(scanCallback);
}
其实该方法的内部就是间接的调用了Scanner的方法
if (DBG) Log.d(TAG, "stopLeScan()");
BluetoothLeScanner scanner = getBluetoothLeScanner();
if (scanner == null) {
return;
}
synchronized (mLeScanClients) {
ScanCallback scanCallback = mLeScanClients.remove(callback);
if (scanCallback == null) {
if (DBG) Log.d(TAG, "scan not started yet");
return;
}
scanner.stopScan(scanCallback);
}
过时也是可以用的 ,