|
@@ -4,23 +4,34 @@ import android.animation.ObjectAnimator;
|
|
|
import android.animation.ValueAnimator;
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.app.ProgressDialog;
|
|
|
+import android.bluetooth.BluetoothAdapter;
|
|
|
+import android.bluetooth.BluetoothDevice;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.PersistableBundle;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
import android.view.Menu;
|
|
|
import android.view.MenuItem;
|
|
|
import android.view.View;
|
|
|
import android.view.animation.AccelerateInterpolator;
|
|
|
+import android.widget.AdapterView;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.ListView;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
import com.joanzapata.iconify.widget.IconTextView;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
import cn.minbb.producttester.R;
|
|
|
+import cn.minbb.producttester.adapter.DeviceListAdapter;
|
|
|
import cn.minbb.producttester.controls.FoldingLayout;
|
|
|
import cn.minbb.producttester.controls.OnFoldListener;
|
|
|
import cn.minbb.producttester.ctrl.App;
|
|
@@ -48,15 +59,48 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
+ private BluetoothAdapter adapter;
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_scan_device);
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
|
+ initView();
|
|
|
+ setListener();
|
|
|
+ loadData();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
App.setupActionBar(this);
|
|
|
}
|
|
|
|
|
|
+ private void setListener() {
|
|
|
+ matchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadData() {
|
|
|
+ // 检查设备是否支持蓝牙
|
|
|
+ adapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
+ if (adapter == null) {
|
|
|
+ // 设备不支持蓝牙
|
|
|
+ Toast.makeText(ScanDeviceActivity.this, "设备不支持蓝牙", Toast.LENGTH_LONG).show();
|
|
|
+ ScanDeviceActivity.this.finish();
|
|
|
+ }
|
|
|
+ List<DeviceListAdapter.ItemBean> matchItemBeanList = new ArrayList<>();
|
|
|
+ Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
|
+ for (BluetoothDevice device : devices) {
|
|
|
+ matchItemBeanList.add(new DeviceListAdapter.ItemBean(device.getName(), device.getAddress()));
|
|
|
+ System.out.println("name = " + device.getName());
|
|
|
+ }
|
|
|
+ matchList.setAdapter(new DeviceListAdapter(this, matchItemBeanList));
|
|
|
+ }
|
|
|
+
|
|
|
public static void start(Context context) {
|
|
|
Intent starter = new Intent(context, ScanDeviceActivity.class);
|
|
|
context.startActivity(starter);
|
|
@@ -143,7 +187,7 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
|
|
|
@Override
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
- menu.add(0, 0, 0, "扫描").setIcon(R.drawable.ic_search_black_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
|
+ menu.add(0, 0, 0, "扫描").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
}
|
|
|
|
|
@@ -154,6 +198,13 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
ScanDeviceActivity.this.finish();
|
|
|
break;
|
|
|
case 0:
|
|
|
+ // 打开蓝牙
|
|
|
+ if (!adapter.isEnabled()) {
|
|
|
+ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
+ // 设置蓝牙可见性,最多 300 秒
|
|
|
+ intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
|
|
|
+ ScanDeviceActivity.this.startActivity(intent);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
return super.onOptionsItemSelected(item);
|