2016-08-03 3 views
0

BLE를 사용하여 내 안드로이드 기기를 다른 블루투스 기기와 연결 중입니다.Android BLE 이미 연결된 기기의 startLeScan 스캔

문제는 이미 연결된 장치를 검색하는 것입니다.

내 첫 번째 접근 방식에서는 연결 방법 전에 stopLeScan을 호출하지 않았습니다.

위의 문제에는 아무런 문제가 없었지만 ui가 너무 짧아서 (너무 짧은 간격 ui 업데이트) 연결 시간이 매우 느려지기도했습니다.

연결하기 전에 내 앱에서 stopLeDevice를 호출 한 후에는 모든 issuses가 해결되었지만 새로운 문제가 발생했습니다. 새로운 문제는 내 scanResult에서 연결된 장치를 더 이상 볼 수 없다는 것입니다. 단절된 장치 만 표시합니다. 나는 아직도 나의 연결된 장치를 감시하고 싶다. 어떻게 이것을 달성 할 수 있습니까?

답변

0

이 클래스를 사용하여 자동으로 새 BLE 장치를 시작합니다.

BLEScanner 서비스

public class BLEScanner extends Service { 
    private BluetoothAdapter mBluetoothAdapter; 
    private ArrayList<BluetoothDevice> mLeDevices; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     mLeDevices = new ArrayList<BluetoothDevice>(); 
     if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 
      BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
      mBluetoothAdapter = bluetoothManager.getAdapter(); 
     } 
     startLeScan(); 
     return Service.START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    private void startLeScan() { 
     scanLeDevice(true); 
    } 

    private void stopLeScan() { 
     scanLeDevice(false); 
    } 

    private void scanLeDevice(boolean enable) { 
     if (enable) { 
      mBluetoothAdapter.startLeScan(mLeScanCallback); 
     } else { 
      mBluetoothAdapter.stopLeScan(mLeScanCallback); 
     } 
    } 

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { 

     @Override 
     public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { 
      if (!mLeDevices.contains(device)) { 
       mLeDevices.add(device); 
       connectToDevice(device); 
      } 
     } 
    }; 

    private void connectToDevice(final BluetoothDevice device) { 
     if (device != null) { 
      Log.i("Tag", "Name: " + device.getAddress() + " Connecting"); 
      if (device.getName() != null) 
       device.connectGatt(this.getApplicationContext(), false, new BluetoothCallBack(this.getApplicationContext(), BLEScanner.this)); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     stopLeScan(); 
     mLeDevices.clear(); 
    } 

    public void removeDevice(BluetoothDevice mDevice) { 
     try { 
      if (mLeDevices != null && mLeDevices.size() > 0) 
       mLeDevices.remove(mDevice); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

이제 콜백 클래스

public class BluetoothCallBack extends BluetoothGattCallback { 
    private BLEScanner mServiceObject; 

    public BluetoothCallBack(Context mContext, BLEScanner mServiceObject) { 
     this.mServiceObject = mServiceObject; 
    } 

    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.i("Tag", "CONNECTED DEVICE: " + gatt.getDevice().getAddress()); 
      gatt.discoverServices(); 
     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      Log.e("Tag", "DISCONNECTED DEVICE: " + gatt.getDevice().getAddress()); 
      gatt.disconnect(); 
      gatt.close(); 
      mServiceObject.removeDevice(gatt.getDevice()); 
     } 
    } 
} 
BLE 장치 연결 여부를 확인하는