2727import android .bluetooth .BluetoothGattService ;
2828import android .bluetooth .BluetoothManager ;
2929import android .bluetooth .BluetoothProfile ;
30+ import android .bluetooth .le .ScanCallback ;
31+ import android .bluetooth .le .ScanResult ;
3032import android .content .Context ;
3133import android .content .Intent ;
3234import android .content .pm .PackageManager ;
@@ -173,28 +175,31 @@ public void scanLeDevice(final boolean enable, long scanPeriod) {
173175 @ Override
174176 public void run () {
175177 isScanning = false ;
176- mBluetoothAdapter .stopLeScan (mScanCallback );
177- broadcastUpdate (ACTION_SCAN_FINISHED );
178- if (mScanLeDeviceList != null ) {
179- mScanLeDeviceList .clear ();
180- mScanLeDeviceList = null ;
178+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
179+ mBluetoothAdapter .getBluetoothLeScanner ().stopScan (mScanCallback );
180+ } else {
181+ mBluetoothAdapter .stopLeScan (mLeScanCallback );
181182 }
182- // mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
183+ broadcastUpdate (ACTION_SCAN_FINISHED );
184+ mScanLeDeviceList .clear ();
183185 }
184186 }, scanPeriod );
185187 mScanLeDeviceList .clear ();
186188 isScanning = true ;
187- mBluetoothAdapter .startLeScan (mScanCallback );
188- // mBluetoothAdapter.getBluetoothLeScanner().startScan(mLeScanCallback);
189+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
190+ mBluetoothAdapter .getBluetoothLeScanner ().startScan (mScanCallback );
191+ } else {
192+ mBluetoothAdapter .startLeScan (mLeScanCallback );
193+ }
189194 } else {
190195 isScanning = false ;
191- mBluetoothAdapter .stopLeScan (mScanCallback );
192- broadcastUpdate (ACTION_SCAN_FINISHED );
193- if (mScanLeDeviceList != null ) {
194- mScanLeDeviceList .clear ();
195- mScanLeDeviceList = null ;
196+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
197+ mBluetoothAdapter .getBluetoothLeScanner ().stopScan (mScanCallback );
198+ } else {
199+ mBluetoothAdapter .stopLeScan (mLeScanCallback );
196200 }
197- // mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback);
201+ broadcastUpdate (ACTION_SCAN_FINISHED );
202+ mScanLeDeviceList .clear ();
198203 }
199204 }
200205
@@ -216,15 +221,6 @@ public boolean isScanning() {
216221 return isScanning ;
217222 }
218223
219- /**
220- * Get scan ble devices
221- *
222- * @return scan le device list
223- */
224- public List <BluetoothDevice > getScanLeDevice () {
225- return mScanLeDeviceList ;
226- }
227-
228224 /**
229225 * Connects to the GATT server hosted on the Bluetooth LE device.
230226 *
@@ -503,6 +499,7 @@ public boolean readRemoteRssi() {
503499 * whether this operation was successful.
504500 *
505501 * <p>Requires {@link Manifest.permission#BLUETOOTH} permission.
502+ *
506503 * @param mtu mtu
507504 * @return true, if the new MTU value has been requested successfully
508505 */
@@ -545,26 +542,47 @@ public List<BluetoothDevice> getConnectDevices() {
545542 }
546543
547544 /**
548- * Device scan callback
545+ * Device scan callback.
546+ * <p>
547+ * Use mScanCallback if Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP,
548+ * else use mLeScanCallback.
549549 */
550- private final BluetoothAdapter .LeScanCallback mScanCallback = new BluetoothAdapter .LeScanCallback () {
551- @ Override
552- public void onLeScan (BluetoothDevice device , int rssi , byte [] scanRecord ) {
553- if (device == null || mScanLeDeviceList .contains (device )) return ;
554- mScanLeDeviceList .add (device );
555- if (mOnLeScanListener != null ) {
556- mOnLeScanListener .onLeScan (device , rssi , scanRecord );
557- }
558- broadcastUpdate (ACTION_BLUETOOTH_DEVICE , device );
559- }
560- };
550+ private ScanCallback mScanCallback ;
551+ private BluetoothAdapter .LeScanCallback mLeScanCallback ;
561552
562- /*private final ScanCallback mLeScanCallback = new ScanCallback() {
563- @Override
564- public void onScanResult(int callbackType, ScanResult result) {
565- super.onScanResult(callbackType, result);
553+ {
554+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
555+ mScanCallback = new ScanCallback () {
556+ @ Override
557+ public void onScanResult (int callbackType , ScanResult result ) {
558+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
559+ if (mScanLeDeviceList .contains (result .getDevice ())) return ;
560+ mScanLeDeviceList .add (result .getDevice ());
561+ if (mOnLeScanListener != null ) {
562+ mOnLeScanListener .onLeScan (result .getDevice (), result .getRssi (), result .getScanRecord ().getBytes ());
563+ }
564+ broadcastUpdate (ACTION_BLUETOOTH_DEVICE , result .getDevice ());
565+ Log .i (TAG , "onScanResult: name: " + result .getDevice ().getName () +
566+ ", address: " + result .getDevice ().getAddress () +
567+ ", rssi: " + result .getRssi () + ", scanRecord: " + result .getScanRecord ());
568+ }
569+ }
570+ };
571+ } else {
572+ mLeScanCallback = new BluetoothAdapter .LeScanCallback () {
573+ @ Override
574+ public void onLeScan (BluetoothDevice device , int rssi , byte [] scanRecord ) {
575+ Log .i (TAG , "device name: " + device .getName () + ", address: " + device .getAddress ());
576+ if (device == null || mScanLeDeviceList .contains (device )) return ;
577+ mScanLeDeviceList .add (device );
578+ if (mOnLeScanListener != null ) {
579+ mOnLeScanListener .onLeScan (device , rssi , scanRecord );
580+ }
581+ broadcastUpdate (ACTION_BLUETOOTH_DEVICE , device );
582+ }
583+ };
566584 }
567- };*/
585+ }
568586
569587 /**
570588 * Implements callback methods for GATT events that the app cares about. For example,
0 commit comments