Skip to content

Commit

Permalink
update commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyaoyou1212 committed Oct 31, 2017
1 parent 234d226 commit 1e6516f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
import android.widget.ListView;
import android.widget.TextView;

import com.vise.baseble.ViseBle;
import com.vise.baseble.callback.scan.IScanCallback;
import com.vise.baseble.callback.scan.ScanCallback;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;
import com.vise.bledemo.R;
import com.vise.bledemo.adapter.DeviceAdapter;
import com.vise.bledemo.common.BluetoothDeviceManager;
import com.vise.bledemo.event.ScanEvent;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.event.Subscribe;
import com.vise.log.ViseLog;

import java.util.List;
import java.util.ArrayList;

/**
* 设备扫描展示界面
Expand All @@ -30,16 +31,42 @@ public class DeviceScanActivity extends AppCompatActivity {
private ListView deviceLv;
private TextView scanCountTv;

//设备扫描结果集合
private volatile List<BluetoothLeDevice> bluetoothLeDeviceList;
//设备扫描结果展示适配器
private DeviceAdapter adapter;

/**
* 扫描回调
*/
private ScanCallback periodScanCallback = new ScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(final BluetoothLeDeviceStore bluetoothLeDeviceStore) {
ViseLog.i("Founded Scan Device:" + bluetoothLeDeviceStore);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (adapter != null && bluetoothLeDeviceStore != null) {
adapter.setDeviceList(bluetoothLeDeviceStore.getDeviceList());
updateItemCount(adapter.getCount());
}
}
});
}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
ViseLog.i("scan finish " + bluetoothLeDeviceStore);
}

@Override
public void onScanTimeout() {
ViseLog.i("scan timeout");
}
});

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_scan);
BusManager.getBus().register(this);
init();
}

Expand All @@ -63,17 +90,6 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
});
}

@Subscribe
public void showScanDevice(ScanEvent event) {
if (event != null) {
if (event.getBluetoothLeDeviceStore() != null) {
bluetoothLeDeviceList = event.getBluetoothLeDeviceStore().getDeviceList();
adapter.setDeviceList(bluetoothLeDeviceList);
updateItemCount(adapter.getCount());
}
}
}

@Override
protected void onResume() {
super.onResume();
Expand All @@ -90,7 +106,6 @@ protected void onPause() {

@Override
protected void onDestroy() {
BusManager.getBus().unregister(this);
super.onDestroy();
}

Expand All @@ -103,7 +118,7 @@ protected void onDestroy() {
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.scan, menu);
if (!BluetoothDeviceManager.getInstance().isScaning()) {
if (periodScanCallback != null && !periodScanCallback.isScanning()) {
menu.findItem(R.id.menu_stop).setVisible(false);
menu.findItem(R.id.menu_scan).setVisible(true);
menu.findItem(R.id.menu_refresh).setActionView(null);
Expand Down Expand Up @@ -139,19 +154,18 @@ public boolean onOptionsItemSelected(final MenuItem item) {
*/
private void startScan() {
updateItemCount(0);
if (adapter != null && bluetoothLeDeviceList != null) {
bluetoothLeDeviceList.clear();
adapter.setDeviceList(bluetoothLeDeviceList);
if (adapter != null) {
adapter.setDeviceList(new ArrayList<BluetoothLeDevice>());
}
BluetoothDeviceManager.getInstance().startScan();
ViseBle.getInstance().startScan(periodScanCallback);
invalidateOptionsMenu();
}

/**
* 停止扫描
*/
private void stopScan() {
BluetoothDeviceManager.getInstance().stopScan();
ViseBle.getInstance().stopScan(periodScanCallback);
invalidateOptionsMenu();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import com.vise.baseble.ViseBle;
import com.vise.baseble.callback.IBleCallback;
import com.vise.baseble.callback.IConnectCallback;
import com.vise.baseble.callback.scan.IScanCallback;
import com.vise.baseble.callback.scan.ScanCallback;
import com.vise.baseble.common.BleConstant;
import com.vise.baseble.common.PropertyType;
import com.vise.baseble.core.BluetoothGattChannel;
import com.vise.baseble.core.DeviceMirror;
import com.vise.baseble.core.DeviceMirrorPool;
import com.vise.baseble.exception.BleException;
import com.vise.baseble.model.BluetoothLeDevice;
import com.vise.baseble.model.BluetoothLeDeviceStore;
import com.vise.baseble.utils.HexUtil;
import com.vise.bledemo.event.CallbackDataEvent;
import com.vise.bledemo.event.ConnectEvent;
Expand All @@ -43,29 +39,6 @@ public class BluetoothDeviceManager {
private CallbackDataEvent callbackDataEvent = new CallbackDataEvent();
private NotifyDataEvent notifyDataEvent = new NotifyDataEvent();

/**
* 扫描回调
*/
private ScanCallback periodScanCallback = new ScanCallback(new IScanCallback() {
@Override
public void onDeviceFound(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
ViseLog.i("Founded Scan Device:" + bluetoothLeDeviceStore);
BusManager.getBus().post(scanEvent.setBluetoothLeDeviceStore(bluetoothLeDeviceStore));
}

@Override
public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
ViseLog.i("scan finish " + bluetoothLeDeviceStore);
BusManager.getBus().post(scanEvent.setBluetoothLeDeviceStore(bluetoothLeDeviceStore).setScanFinish(true));
}

@Override
public void onScanTimeout() {
ViseLog.i("scan timeout");
BusManager.getBus().post(scanEvent.setScanTimeout(true));
}
});

/**
* 连接回调
*/
Expand Down Expand Up @@ -180,18 +153,6 @@ public void init(Context context) {
mDeviceMirrorPool = ViseBle.getInstance().getDeviceMirrorPool();
}

public void startScan() {
ViseBle.getInstance().startScan(periodScanCallback);
}

public void stopScan() {
ViseBle.getInstance().stopScan(periodScanCallback);
}

public boolean isScaning() {
return periodScanCallback.isScanning();
}

public void connect(BluetoothLeDevice bluetoothLeDevice) {
ViseBle.getInstance().connect(bluetoothLeDevice, connectCallback);
}
Expand Down

0 comments on commit 1e6516f

Please sign in to comment.