|
@@ -1,8 +1,5 @@
|
|
package cn.minbb.producttester.views;
|
|
package cn.minbb.producttester.views;
|
|
|
|
|
|
-import android.animation.ObjectAnimator;
|
|
|
|
-import android.animation.ValueAnimator;
|
|
|
|
-import android.annotation.SuppressLint;
|
|
|
|
import android.app.ProgressDialog;
|
|
import android.app.ProgressDialog;
|
|
import android.bluetooth.BluetoothAdapter;
|
|
import android.bluetooth.BluetoothAdapter;
|
|
import android.bluetooth.BluetoothDevice;
|
|
import android.bluetooth.BluetoothDevice;
|
|
@@ -17,66 +14,42 @@ import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Handler;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.NonNull;
|
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.support.v7.app.AppCompatDelegate;
|
|
import android.support.v7.app.AppCompatDelegate;
|
|
import android.view.Menu;
|
|
import android.view.Menu;
|
|
import android.view.MenuItem;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.View;
|
|
-import android.view.animation.AccelerateInterpolator;
|
|
|
|
import android.widget.AdapterView;
|
|
import android.widget.AdapterView;
|
|
-import android.widget.LinearLayout;
|
|
|
|
import android.widget.ListView;
|
|
import android.widget.ListView;
|
|
import android.widget.Toast;
|
|
import android.widget.Toast;
|
|
|
|
|
|
-import com.joanzapata.iconify.widget.IconTextView;
|
|
|
|
-
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
-import java.util.Timer;
|
|
|
|
-import java.util.TimerTask;
|
|
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
import butterknife.BindView;
|
|
import butterknife.BindView;
|
|
import butterknife.ButterKnife;
|
|
import butterknife.ButterKnife;
|
|
-import butterknife.OnClick;
|
|
|
|
import cn.minbb.producttester.R;
|
|
import cn.minbb.producttester.R;
|
|
import cn.minbb.producttester.adapter.DeviceListAdapter;
|
|
import cn.minbb.producttester.adapter.DeviceListAdapter;
|
|
-import cn.minbb.producttester.controls.FoldingLayout;
|
|
|
|
-import cn.minbb.producttester.controls.OnFoldListener;
|
|
|
|
import cn.minbb.producttester.ctrl.App;
|
|
import cn.minbb.producttester.ctrl.App;
|
|
|
|
|
|
public class ScanDeviceActivity extends AppCompatActivity {
|
|
public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
|
|
|
- @BindView(R.id.matchListLayout)
|
|
|
|
- LinearLayout matchListLayout;
|
|
|
|
- @BindView(R.id.searchListLayout)
|
|
|
|
- LinearLayout searchListLayout;
|
|
|
|
- @BindView(R.id.matchListArrow)
|
|
|
|
- IconTextView matchListArrow;
|
|
|
|
- @BindView(R.id.searchListArrow)
|
|
|
|
- IconTextView searchListArrow;
|
|
|
|
- @BindView(R.id.matchListFolding)
|
|
|
|
- FoldingLayout matchListFolding;
|
|
|
|
- @BindView(R.id.searchListFolding)
|
|
|
|
- FoldingLayout searchListFolding;
|
|
|
|
- @BindView(R.id.matchList)
|
|
|
|
- ListView matchList;
|
|
|
|
- @BindView(R.id.searchList)
|
|
|
|
- ListView searchList;
|
|
|
|
- @BindView(R.id.bottomLayout)
|
|
|
|
- View bottomLayout;
|
|
|
|
|
|
+ @BindView(R.id.deviceList)
|
|
|
|
+ ListView deviceList;
|
|
|
|
|
|
private ProgressDialog dialog;
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
private BluetoothAdapter adapter = App.getApp().getBluetoothAdapter();
|
|
private BluetoothAdapter adapter = App.getApp().getBluetoothAdapter();
|
|
- List<DeviceListAdapter.ItemBean> matchItemBeanList = new ArrayList<>();
|
|
|
|
- List<DeviceListAdapter.ItemBean> searchItemBeanList = new ArrayList<>();
|
|
|
|
- private DeviceListAdapter matchDeviceListAdapter;
|
|
|
|
- private DeviceListAdapter searchDeviceListAdapter;
|
|
|
|
|
|
+ private List<DeviceListAdapter.ItemBean> deviceItemBeanList = new ArrayList<>();
|
|
|
|
+ private DeviceListAdapter deviceListAdapter;
|
|
|
|
+ private BluetoothDevice selectDevice;
|
|
|
|
+ private int selectIndex;
|
|
|
|
|
|
static {
|
|
static {
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
@@ -111,12 +84,10 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
}
|
|
}
|
|
Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
for (BluetoothDevice device : devices) {
|
|
for (BluetoothDevice device : devices) {
|
|
- matchItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
|
|
|
+ deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
}
|
|
}
|
|
- matchDeviceListAdapter = new DeviceListAdapter(this, matchItemBeanList);
|
|
|
|
- matchList.setAdapter(matchDeviceListAdapter);
|
|
|
|
- searchDeviceListAdapter = new DeviceListAdapter(this, searchItemBeanList);
|
|
|
|
- searchList.setAdapter(searchDeviceListAdapter);
|
|
|
|
|
|
+ deviceListAdapter = new DeviceListAdapter(this, deviceItemBeanList);
|
|
|
|
+ deviceList.setAdapter(deviceListAdapter);
|
|
}
|
|
}
|
|
|
|
|
|
initView();
|
|
initView();
|
|
@@ -130,30 +101,46 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
this.unregisterReceiver(receiver);
|
|
this.unregisterReceiver(receiver);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void start(Context context) {
|
|
|
|
+ Intent starter = new Intent(context, ScanDeviceActivity.class);
|
|
|
|
+ context.startActivity(starter);
|
|
|
|
+ }
|
|
|
|
+
|
|
private void initView() {
|
|
private void initView() {
|
|
App.setupActionBar(this);
|
|
App.setupActionBar(this);
|
|
}
|
|
}
|
|
|
|
|
|
private void setListener() {
|
|
private void setListener() {
|
|
- matchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
|
|
+ deviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
@Override
|
|
@Override
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
- // 尝试连接
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
- @Override
|
|
|
|
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
- // 尝试配对
|
|
|
|
- try {
|
|
|
|
- Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
|
- createBondMethod.invoke(searchItemBeanList.get(position).device);
|
|
|
|
- } catch (NoSuchMethodException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } catch (InvocationTargetException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ selectIndex = i;
|
|
|
|
+ selectDevice = deviceItemBeanList.get(i).device;
|
|
|
|
+ switch (selectDevice.getBondState()) {
|
|
|
|
+ case BluetoothDevice.BOND_NONE:
|
|
|
|
+ // 未配对 - 尝试配对
|
|
|
|
+ try {
|
|
|
|
+ Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
|
+ createBondMethod.invoke(selectDevice);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case BluetoothDevice.BOND_BONDING:
|
|
|
|
+ // 配对中
|
|
|
|
+ break;
|
|
|
|
+ case BluetoothDevice.BOND_BONDED:
|
|
|
|
+ // 已配对 - 尝试连接
|
|
|
|
+ // 固定的 UUID
|
|
|
|
+ String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
|
|
|
|
+ UUID uuid = UUID.fromString(SPP_UUID);
|
|
|
|
+ try {
|
|
|
|
+ BluetoothSocket socket = selectDevice.createRfcommSocketToServiceRecord(uuid);
|
|
|
|
+ socket.connect();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -168,108 +155,53 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static void start(Context context) {
|
|
|
|
- Intent starter = new Intent(context, ScanDeviceActivity.class);
|
|
|
|
- context.startActivity(starter);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void showProgress(boolean show) {
|
|
|
|
|
|
+ private void showSearchDeviceProgress(boolean show) {
|
|
if (dialog == null) {
|
|
if (dialog == null) {
|
|
dialog = new ProgressDialog(this);
|
|
dialog = new ProgressDialog(this);
|
|
dialog.setTitle("蓝牙串口");
|
|
dialog.setTitle("蓝牙串口");
|
|
dialog.setMessage("搜索设备中······");
|
|
dialog.setMessage("搜索设备中······");
|
|
dialog.setCancelable(false);
|
|
dialog.setCancelable(false);
|
|
- dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
|
|
|
|
- @Override
|
|
|
|
- public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
|
- adapter.cancelDiscovery();
|
|
|
|
- dialog.dismiss();
|
|
|
|
|
|
+ dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialogInterface, i) -> {
|
|
|
|
+ adapter.cancelDiscovery();
|
|
|
|
+ dialog.dismiss();
|
|
|
|
+ });
|
|
|
|
+ dialog.setOnDismissListener(dialog -> {
|
|
|
|
+ int count = 0;
|
|
|
|
+ for (DeviceListAdapter.ItemBean itemBean : deviceItemBeanList) {
|
|
|
|
+ if (itemBean.device.getBondState() == BluetoothDevice.BOND_NONE) {
|
|
|
|
+ count++;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ Toast.makeText(ScanDeviceActivity.this, "查找到 " + count + " 个蓝牙设备", Toast.LENGTH_SHORT).show();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
if (show) {
|
|
if (show) {
|
|
dialog.show();
|
|
dialog.show();
|
|
} else {
|
|
} else {
|
|
dialog.dismiss();
|
|
dialog.dismiss();
|
|
|
|
+ dialog = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void handleAnimation(final View bar, final FoldingLayout foldingLayout, View parent, final View nextParent) {
|
|
|
|
- foldingLayout.setFoldListener(new OnFoldListener() {
|
|
|
|
- @Override
|
|
|
|
- public void onStartFold(float foldFactor) {
|
|
|
|
- bar.setClickable(true);
|
|
|
|
- matchListArrow.setText("{fa-chevron-up}");
|
|
|
|
- resetMarginToTop(foldingLayout, foldFactor, nextParent);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void onFoldingState(float foldFactor, float foldDrawHeight) {
|
|
|
|
- bar.setClickable(false);
|
|
|
|
- resetMarginToTop(foldingLayout, foldFactor, nextParent);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void onEndFold(float foldFactor) {
|
|
|
|
- bar.setClickable(true);
|
|
|
|
- matchListArrow.setText("{fa-chevron-up}");
|
|
|
|
- resetMarginToTop(foldingLayout, foldFactor, nextParent);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- animateFold(foldingLayout, 500);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void resetMarginToTop(View view, float foldFactor, View nextParent) {
|
|
|
|
- LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) nextParent.getLayoutParams();
|
|
|
|
- lp.topMargin = (int) (-view.getMeasuredHeight() * foldFactor) + dp2px(ScanDeviceActivity.this, 10);
|
|
|
|
- nextParent.setLayoutParams(lp);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void setMarginToTop(float foldFactor, View nextParent) {
|
|
|
|
- LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) nextParent.getLayoutParams();
|
|
|
|
- lp.topMargin = (int) (-dp2px(ScanDeviceActivity.this, 135) * foldFactor) + dp2px(ScanDeviceActivity.this, 10);
|
|
|
|
- nextParent.setLayoutParams(lp);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @SuppressLint("NewApi")
|
|
|
|
- public void animateFold(FoldingLayout foldLayout, int duration) {
|
|
|
|
- float foldFactor = foldLayout.getFoldFactor();
|
|
|
|
- ObjectAnimator animator = ObjectAnimator.ofFloat(foldLayout, "foldFactor", foldFactor, foldFactor > 0 ? 0 : 1);
|
|
|
|
- animator.setRepeatMode(ValueAnimator.REVERSE);
|
|
|
|
- animator.setRepeatCount(0);
|
|
|
|
- animator.setDuration(duration);
|
|
|
|
- animator.setInterpolator(new AccelerateInterpolator());
|
|
|
|
- animator.start();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static int dp2px(Context context, float dpValue) {
|
|
|
|
- float density = context.getResources().getDisplayMetrics().density;
|
|
|
|
- return (int) (dpValue * density + 0.5f);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void connect(BluetoothDevice device) throws IOException {
|
|
|
|
- // 固定的 UUID
|
|
|
|
- String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
|
|
|
|
- UUID uuid = UUID.fromString(SPP_UUID);
|
|
|
|
- BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
|
|
|
|
- socket.connect();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @OnClick({R.id.matchListToggle, R.id.searchListToggle})
|
|
|
|
- public void onViewClicked(View view) {
|
|
|
|
- switch (view.getId()) {
|
|
|
|
- case R.id.matchListToggle:
|
|
|
|
- handleAnimation(view, matchListFolding, matchListLayout, searchListLayout);
|
|
|
|
- break;
|
|
|
|
- case R.id.searchListToggle:
|
|
|
|
- handleAnimation(view, searchListFolding, searchListLayout, bottomLayout);
|
|
|
|
- break;
|
|
|
|
|
|
+ private void showMatchDeviceProgress(boolean show) {
|
|
|
|
+ if (dialog == null) {
|
|
|
|
+ dialog = new ProgressDialog(this);
|
|
|
|
+ dialog.setTitle("配对");
|
|
|
|
+ dialog.setMessage("设备配对中······");
|
|
|
|
+ dialog.setCancelable(false);
|
|
|
|
+ }
|
|
|
|
+ if (show) {
|
|
|
|
+ dialog.show();
|
|
|
|
+ } else {
|
|
|
|
+ dialog.dismiss();
|
|
|
|
+ dialog = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
- menu.add(0, 0, 0, "搜索设备").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
|
|
|
|
+ menu.add(0, 0, 0, "提示").setIcon(R.drawable.ic_info_outline_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
|
|
+ menu.add(0, 1, 0, "搜索设备").setIcon(R.drawable.ic_search_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
return super.onCreateOptionsMenu(menu);
|
|
return super.onCreateOptionsMenu(menu);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -280,8 +212,23 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
ScanDeviceActivity.this.finish();
|
|
ScanDeviceActivity.this.finish();
|
|
break;
|
|
break;
|
|
case 0:
|
|
case 0:
|
|
- showProgress(true);
|
|
|
|
- matchItemBeanList.clear();
|
|
|
|
|
|
+ AlertDialog.Builder builder = new AlertDialog.Builder(ScanDeviceActivity.this);
|
|
|
|
+ builder.setIcon(R.drawable.ic_info_outline_black_24dp);
|
|
|
|
+ builder.setTitle("提示");
|
|
|
|
+ builder.setMessage("点击已配对的设备进行连接\n点击搜索到的设备进行配对");
|
|
|
|
+ builder.setPositiveButton("好的", null);
|
|
|
|
+ builder.create().show();
|
|
|
|
+ break;
|
|
|
|
+ case 1:
|
|
|
|
+ showSearchDeviceProgress(true);
|
|
|
|
+ List<DeviceListAdapter.ItemBean> unboundItemBeanList = new ArrayList<>();
|
|
|
|
+ for (DeviceListAdapter.ItemBean itemBean : deviceItemBeanList) {
|
|
|
|
+ if (itemBean.device.getBondState() == BluetoothDevice.BOND_NONE) {
|
|
|
|
+ unboundItemBeanList.add(itemBean);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ deviceItemBeanList.removeAll(unboundItemBeanList);
|
|
|
|
+ deviceListAdapter.notifyDataSetChanged();
|
|
// 打开蓝牙
|
|
// 打开蓝牙
|
|
if (!adapter.isEnabled()) {
|
|
if (!adapter.isEnabled()) {
|
|
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
@@ -297,7 +244,7 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
|
// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
|
// startActivityForResult(intent, 1);
|
|
// startActivityForResult(intent, 1);
|
|
new Handler().postDelayed(() -> runOnUiThread(() -> {
|
|
new Handler().postDelayed(() -> runOnUiThread(() -> {
|
|
- showProgress(false);
|
|
|
|
|
|
+ showSearchDeviceProgress(false);
|
|
adapter.cancelDiscovery();
|
|
adapter.cancelDiscovery();
|
|
}), 20000);
|
|
}), 20000);
|
|
break;
|
|
break;
|
|
@@ -335,7 +282,9 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
switch (requestCode) {
|
|
switch (requestCode) {
|
|
case 1:
|
|
case 1:
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
- System.out.println("权限请求成功");
|
|
|
|
|
|
+ Toast.makeText(ScanDeviceActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
|
|
|
|
+ } else {
|
|
|
|
+ Toast.makeText(ScanDeviceActivity.this, "未获得授权,将无法进行扫描", Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
@@ -350,31 +299,33 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
// 获取查找到的蓝牙设备
|
|
// 获取查找到的蓝牙设备
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
|
|
- matchItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
|
- searchDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
|
+ if (!deviceItemBeanList.contains(new DeviceListAdapter.ItemBean(device)) && device.getName() != null && device.getAddress() != null) {
|
|
|
|
+ deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
|
+ deviceListAdapter.notifyDataSetChanged();
|
|
|
|
+ }
|
|
|
|
|
|
// 如果查找到的设备符合要连接的设备,处理
|
|
// 如果查找到的设备符合要连接的设备,处理
|
|
- if (device.getName().equalsIgnoreCase(device.getName())) {
|
|
|
|
- // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
|
|
|
|
- App.getApp().getBluetoothAdapter().cancelDiscovery();
|
|
|
|
- // 获取蓝牙设备的连接状态
|
|
|
|
- switch (device.getBondState()) {
|
|
|
|
- // 未配对
|
|
|
|
- case BluetoothDevice.BOND_NONE:
|
|
|
|
- // 配对
|
|
|
|
- try {
|
|
|
|
- Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
|
- createBondMethod.invoke(device);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
- // 已配对
|
|
|
|
- case BluetoothDevice.BOND_BONDED:
|
|
|
|
- // 连接
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// if (device.getName().equalsIgnoreCase(device.getName())) {
|
|
|
|
+// // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
|
|
|
|
+// App.getApp().getBluetoothAdapter().cancelDiscovery();
|
|
|
|
+// // 获取蓝牙设备的连接状态
|
|
|
|
+// switch (device.getBondState()) {
|
|
|
|
+// // 未配对
|
|
|
|
+// case BluetoothDevice.BOND_NONE:
|
|
|
|
+// // 配对
|
|
|
|
+// try {
|
|
|
|
+// Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
|
+// createBondMethod.invoke(device);
|
|
|
|
+// } catch (Exception e) {
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+// }
|
|
|
|
+// break;
|
|
|
|
+// // 已配对
|
|
|
|
+// case BluetoothDevice.BOND_BONDED:
|
|
|
|
+// // 连接
|
|
|
|
+// break;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
|
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
|
// 状态改变的广播
|
|
// 状态改变的广播
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
@@ -382,12 +333,21 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
switch (device.getBondState()) {
|
|
switch (device.getBondState()) {
|
|
case BluetoothDevice.BOND_NONE:
|
|
case BluetoothDevice.BOND_NONE:
|
|
// 取消配对
|
|
// 取消配对
|
|
|
|
+ System.out.println("取消配对");
|
|
|
|
+ showMatchDeviceProgress(false);
|
|
break;
|
|
break;
|
|
case BluetoothDevice.BOND_BONDING:
|
|
case BluetoothDevice.BOND_BONDING:
|
|
// 正在配对
|
|
// 正在配对
|
|
|
|
+ System.out.println("正在配对");
|
|
|
|
+ showMatchDeviceProgress(true);
|
|
break;
|
|
break;
|
|
case BluetoothDevice.BOND_BONDED:
|
|
case BluetoothDevice.BOND_BONDED:
|
|
// 完成配对
|
|
// 完成配对
|
|
|
|
+ System.out.println("完成配对");
|
|
|
|
+ showMatchDeviceProgress(false);
|
|
|
|
+ deviceItemBeanList.set(selectIndex, new DeviceListAdapter.ItemBean(device));
|
|
|
|
+ deviceListAdapter.notifyDataSetChanged();
|
|
|
|
+ Toast.makeText(ScanDeviceActivity.this, "和设备 " + device.getName() + " 配对成功", Toast.LENGTH_SHORT).show();
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -397,12 +357,12 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
Toast.makeText(ScanDeviceActivity.this, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
|
|
Toast.makeText(ScanDeviceActivity.this, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
|
|
ScanDeviceActivity.this.finish();
|
|
ScanDeviceActivity.this.finish();
|
|
} else {
|
|
} else {
|
|
- matchItemBeanList.clear();
|
|
|
|
|
|
+ deviceItemBeanList.clear();
|
|
Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
for (BluetoothDevice device : devices) {
|
|
for (BluetoothDevice device : devices) {
|
|
- matchItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
|
|
|
+ deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
}
|
|
}
|
|
- matchDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
|
+ deviceListAdapter.notifyDataSetChanged();
|
|
}
|
|
}
|
|
}), 500);
|
|
}), 500);
|
|
}
|
|
}
|