|
|
|
|
@ -1,19 +1,27 @@
|
|
|
|
|
package com.rehome.dywoa.bleUtil;
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
|
|
import android.bluetooth.BluetoothManager;
|
|
|
|
|
import android.bluetooth.le.BluetoothLeScanner;
|
|
|
|
|
import android.bluetooth.le.ScanCallback;
|
|
|
|
|
import android.bluetooth.le.ScanResult;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import com.bjzc.blemanager.BluetoothLeService;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
|
import android.widget.ListView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import androidx.activity.EdgeToEdge;
|
|
|
|
|
@ -23,14 +31,20 @@ import androidx.core.app.ActivityCompat;
|
|
|
|
|
import androidx.core.graphics.Insets;
|
|
|
|
|
import androidx.core.view.ViewCompat;
|
|
|
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
|
|
import com.clj.fastble.BleManager;
|
|
|
|
|
//import com.clj.fastble.BleManager;
|
|
|
|
|
import com.bjzc.blemanager.BleManager;
|
|
|
|
|
import com.clj.fastble.callback.BleScanCallback;
|
|
|
|
|
import com.clj.fastble.data.BleDevice;
|
|
|
|
|
import com.clj.fastble.data.BleScanState;
|
|
|
|
|
import com.clj.fastble.scan.BleScanRuleConfig;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.rehome.dywoa.Contans;
|
|
|
|
|
import com.rehome.dywoa.R;
|
|
|
|
|
import com.rehome.dywoa.adapter.XzjhAdapter;
|
|
|
|
|
import com.rehome.dywoa.base.BaseActivity;
|
|
|
|
|
import com.rehome.dywoa.utils.SPUtils;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
@ -42,6 +56,7 @@ import java.util.List;
|
|
|
|
|
public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
ListView lv;
|
|
|
|
|
TextView tx_data;
|
|
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
|
|
private ArrayList<String> mPermissionList = new ArrayList();
|
|
|
|
|
private BluetoothLeScanner bluetoothLeScanner;
|
|
|
|
|
@ -54,6 +69,10 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
//private LeDeviceListAdapter leDeviceListAdapter = new LeDeviceListAdapter();
|
|
|
|
|
private LeDeviceListAdapter mLeDeviceListAdapter;
|
|
|
|
|
|
|
|
|
|
private BleManager ble;
|
|
|
|
|
private int selectItem = -1;
|
|
|
|
|
private String cmd = "";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getContentViewID() {
|
|
|
|
|
return R.layout.activity_blue_tooth_device;
|
|
|
|
|
@ -61,6 +80,84 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initView() {
|
|
|
|
|
lv = findViewById(R.id.lv);
|
|
|
|
|
tx_data = findViewById(R.id.tx_data);
|
|
|
|
|
ble= BleManager.getInstance();
|
|
|
|
|
String str=ble.init(BlueToothDeviceActivity.this);
|
|
|
|
|
showLog(str);
|
|
|
|
|
|
|
|
|
|
if("BLE is not supported".equals(str)){
|
|
|
|
|
showToast("当前设备不支持低功耗蓝牙,无法连接传感器采集数据");
|
|
|
|
|
}
|
|
|
|
|
if("Bluetooth not supported".equals(str)){
|
|
|
|
|
showToast("当前设备不支持蓝牙功能");
|
|
|
|
|
}
|
|
|
|
|
if("Bluetooth not enabled".equals(str)){
|
|
|
|
|
showToast("蓝牙功能没有开启");
|
|
|
|
|
}
|
|
|
|
|
if("ACCESS_COARSE_LOCATION permission denied".equals(str)){
|
|
|
|
|
showToast("您未授权开启蓝牙扫描附近的设备,请先开启蓝牙权限");
|
|
|
|
|
}
|
|
|
|
|
if("context is no Activity".equals(str)){
|
|
|
|
|
showToast("Init(context)方法的参数必须是 Activity");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ble.setOnReceiveListener(new BleManager.OnReceiveListener() {
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
|
@Override
|
|
|
|
|
public void onShowMessage(Context context, Intent intent) {
|
|
|
|
|
final String action = intent.getAction();
|
|
|
|
|
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
|
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(BlueToothDeviceActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
showLog("扫描发现设备" + device.getName() + "," + device.getAddress());
|
|
|
|
|
String deviceName = device.getName();
|
|
|
|
|
if (deviceName != null && !deviceName.isEmpty()){
|
|
|
|
|
//showLog(new Gson().toJson(device));
|
|
|
|
|
mLeDeviceListAdapter.addDevice(device);
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
} else if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
|
|
|
|
|
showLog("连接");
|
|
|
|
|
mLeDeviceListAdapter.setSelectItem(selectItem);
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
|
|
BluetoothDevice device = mLeDeviceListAdapter.getDevice(selectItem);
|
|
|
|
|
String deviceName = device.getName();
|
|
|
|
|
if (deviceName != null && !deviceName.isEmpty()){
|
|
|
|
|
SPUtils.put(context, Contans.KEY_BLUE_TOOTH,device.getAddress());
|
|
|
|
|
}
|
|
|
|
|
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
|
|
|
|
|
showLog("断开");
|
|
|
|
|
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
|
|
|
|
|
showLog("数据传输通道准备就绪,可以发送数据了。");
|
|
|
|
|
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
|
|
|
|
|
byte[] data= intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
|
|
String resultReceive = new String(data);
|
|
|
|
|
showLog("收到传感器数据" + resultReceive);
|
|
|
|
|
if("VR".equals(cmd)){
|
|
|
|
|
showLog(resultReceive);
|
|
|
|
|
}else{
|
|
|
|
|
tx_data.setText("接收数值:"+resultReceive);
|
|
|
|
|
}
|
|
|
|
|
if("0F3101".equals(cmd)){
|
|
|
|
|
ble.write("VR", false);
|
|
|
|
|
cmd="VR";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
initToolbar("蓝牙数据管理", "扫描并确认", new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
@ -68,14 +165,69 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
mLeDeviceListAdapter.clear();
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
//initBlueTooth();
|
|
|
|
|
if(BleManager.getInstance().isSupportBle()){
|
|
|
|
|
initBlueToothFastBle();
|
|
|
|
|
// if(BleManager.getInstance().isSupportBle()){
|
|
|
|
|
// initBlueToothFastBle();
|
|
|
|
|
// }else{
|
|
|
|
|
// showToast("当前设备不支持低功耗蓝牙,无法连接传感器采集数据");
|
|
|
|
|
// }
|
|
|
|
|
if("OK".equals(str)){
|
|
|
|
|
ble.scan();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
BleManager.getInstance().init(getApplication());
|
|
|
|
|
lv = findViewById(R.id.lv);
|
|
|
|
|
mLeDeviceListAdapter = new LeDeviceListAdapter(BlueToothDeviceActivity.this);
|
|
|
|
|
//BleManager.getInstance().init(getApplication());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mLeDeviceListAdapter = new LeDeviceListAdapter(new LeDeviceListAdapter.CallBackBlueToothBind() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void Click(int position) {
|
|
|
|
|
showLog(String.valueOf(position));
|
|
|
|
|
BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(BlueToothDeviceActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
selectItem=position;
|
|
|
|
|
ble.connect(device.getAddress(),device.getName());
|
|
|
|
|
}
|
|
|
|
|
}, new LeDeviceListAdapter.CallBackBlueToothUnBind() {
|
|
|
|
|
@Override
|
|
|
|
|
public void Click(int position) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, new LeDeviceListAdapter.CallBackBlueToothSendData() {
|
|
|
|
|
@Override
|
|
|
|
|
public void Click(int position) {
|
|
|
|
|
//ble.write(String cmd, Boolean ishex)
|
|
|
|
|
//读固件版本号
|
|
|
|
|
//ble.write("VR", false);
|
|
|
|
|
//读电量
|
|
|
|
|
//ble.write("BV", false);
|
|
|
|
|
//温度测量
|
|
|
|
|
ble.write("0B0B6300", true);
|
|
|
|
|
//距离测量
|
|
|
|
|
//ble.write("4453", true);
|
|
|
|
|
//转速测量
|
|
|
|
|
//ble.write("0F3101", true);
|
|
|
|
|
//cmd="0F3101";
|
|
|
|
|
//加速度测量
|
|
|
|
|
//ble.write("1F11", true);
|
|
|
|
|
//速度测量。
|
|
|
|
|
//ble.write("1F12", true);
|
|
|
|
|
//位移测量
|
|
|
|
|
//ble.write("1F13", true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
lv.setAdapter(mLeDeviceListAdapter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -84,6 +236,19 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
ble.scan();
|
|
|
|
|
ble.disconnect();
|
|
|
|
|
// if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){
|
|
|
|
|
// BleManager.getInstance().cancelScan();
|
|
|
|
|
// }
|
|
|
|
|
// BleManager.getInstance().disconnectAllDevice();
|
|
|
|
|
// BleManager.getInstance().destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initPermission(){
|
|
|
|
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
|
|
|
|
|
// Android 版本大于等于 Android12 时
|
|
|
|
|
@ -135,12 +300,12 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void initBlueTooth() {
|
|
|
|
|
boolean bluetoothAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
|
|
|
|
|
if(bluetoothAvailable){
|
|
|
|
|
showLog("传统蓝牙可用");
|
|
|
|
|
}else{
|
|
|
|
|
showLog("传统蓝牙不可用");
|
|
|
|
|
}
|
|
|
|
|
// boolean bluetoothAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
|
|
|
|
|
// if(bluetoothAvailable){
|
|
|
|
|
// showLog("传统蓝牙可用");
|
|
|
|
|
// }else{
|
|
|
|
|
// showLog("传统蓝牙不可用");
|
|
|
|
|
// }
|
|
|
|
|
boolean bluetoothLEAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
|
|
|
|
|
|
|
|
|
|
if(bluetoothLEAvailable){
|
|
|
|
|
@ -152,41 +317,65 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initBlueToothFastBle() {
|
|
|
|
|
BleManager.getInstance()
|
|
|
|
|
.enableLog(true)
|
|
|
|
|
.setReConnectCount(1, 5000)
|
|
|
|
|
.setSplitWriteNum(20)
|
|
|
|
|
.setConnectOverTime(10000)
|
|
|
|
|
.setOperateTimeout(5000);
|
|
|
|
|
// public void initBlueToothFastBle() {
|
|
|
|
|
// if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){
|
|
|
|
|
// BleManager.getInstance().cancelScan();
|
|
|
|
|
// }
|
|
|
|
|
// BleManager.getInstance()
|
|
|
|
|
// .enableLog(true)
|
|
|
|
|
// .setReConnectCount(1, 5000)
|
|
|
|
|
// .setSplitWriteNum(20)
|
|
|
|
|
// .setConnectOverTime(10000)
|
|
|
|
|
// .setOperateTimeout(5000);
|
|
|
|
|
//
|
|
|
|
|
// BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
|
|
|
|
|
// .setAutoConnect(true)
|
|
|
|
|
// .setScanTimeOut(10000)
|
|
|
|
|
// .build();
|
|
|
|
|
// BleManager.getInstance().initScanRule(scanRuleConfig);
|
|
|
|
|
|
|
|
|
|
BleManager.getInstance().scan(new BleScanCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanStarted(boolean success) {
|
|
|
|
|
showToast("开始蓝牙扫描");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanning(BleDevice bleDevice) {
|
|
|
|
|
showToast("发现设备");
|
|
|
|
|
mLeDeviceListAdapter.addDevice(bleDevice.getDevice());
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanFinished(List<BleDevice> scanResultList) {
|
|
|
|
|
showToast("蓝牙扫描结束");
|
|
|
|
|
showLog(String.valueOf(scanResultList.size()));
|
|
|
|
|
showLog(new Gson().toJson(scanResultList));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// BleManager.getInstance().scan(new BleScanCallback() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onScanStarted(boolean success) {
|
|
|
|
|
// showToast("开始蓝牙扫描");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onScanning(BleDevice bleDevice) {
|
|
|
|
|
//// if (context != null && !BlueToothDeviceActivity.this.isFinishing() && !BlueToothDeviceActivity.this.isDestroyed()) {
|
|
|
|
|
//// showToast("发现设备");
|
|
|
|
|
//// }
|
|
|
|
|
// BluetoothDevice device = bleDevice.getDevice();
|
|
|
|
|
// if(device!=null){
|
|
|
|
|
// if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// // TODO: Consider calling
|
|
|
|
|
// // ActivityCompat#requestPermissions
|
|
|
|
|
// // here to request the missing permissions, and then overriding
|
|
|
|
|
// // public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// // int[] grantResults)
|
|
|
|
|
// // to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// // for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// String deviceName = device.getName();
|
|
|
|
|
// if (deviceName != null && !deviceName.isEmpty()){
|
|
|
|
|
// showLog(new Gson().toJson(device));
|
|
|
|
|
// mLeDeviceListAdapter.addDevice(bleDevice.getDevice());
|
|
|
|
|
// mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onScanFinished(List<BleDevice> scanResultList) {
|
|
|
|
|
// if (context != null && !BlueToothDeviceActivity.this.isFinishing() && !BlueToothDeviceActivity.this.isDestroyed()) {
|
|
|
|
|
// showToast("蓝牙扫描结束");
|
|
|
|
|
// }
|
|
|
|
|
// showLog(String.valueOf(mLeDeviceListAdapter.getCount()));
|
|
|
|
|
// showLog(new Gson().toJson(scanResultList));
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private void scanLeDevice() {
|
|
|
|
|
if (!scanning) {
|
|
|
|
|
@ -238,4 +427,151 @@ public class BlueToothDeviceActivity extends BaseActivity {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private class LeDeviceListAdapter extends BaseAdapter {
|
|
|
|
|
private class ViewHolder {
|
|
|
|
|
TextView deviceName;
|
|
|
|
|
TextView deviceStatus;
|
|
|
|
|
TextView deviceBind;
|
|
|
|
|
TextView deviceDeBind;
|
|
|
|
|
TextView sendData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<BluetoothDevice> mLeDevices;
|
|
|
|
|
private LayoutInflater mInflator;
|
|
|
|
|
private CallBackBlueToothBind callBackBlueToothBind;
|
|
|
|
|
private CallBackBlueToothUnBind callBackBlueToothUnBind;
|
|
|
|
|
private CallBackBlueToothSendData callBackBlueToothSendData;
|
|
|
|
|
|
|
|
|
|
public LeDeviceListAdapter(CallBackBlueToothBind callBackBlueToothBind,CallBackBlueToothUnBind callBackBlueToothUnBind,
|
|
|
|
|
CallBackBlueToothSendData callBackBlueToothSendData) {
|
|
|
|
|
super();
|
|
|
|
|
mLeDevices = new ArrayList<BluetoothDevice>();
|
|
|
|
|
mInflator = BlueToothDeviceActivity.this.getLayoutInflater();
|
|
|
|
|
this.callBackBlueToothBind=callBackBlueToothBind;
|
|
|
|
|
this.callBackBlueToothUnBind=callBackBlueToothUnBind;
|
|
|
|
|
this.callBackBlueToothSendData=callBackBlueToothSendData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface CallBackBlueToothBind {
|
|
|
|
|
void Click(int position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface CallBackBlueToothUnBind {
|
|
|
|
|
void Click(int position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface CallBackBlueToothSendData {
|
|
|
|
|
void Click(int position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addDevice(BluetoothDevice device) {
|
|
|
|
|
if (!mLeDevices.contains(device)) {
|
|
|
|
|
mLeDevices.add(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BluetoothDevice getDevice(int position) {
|
|
|
|
|
return mLeDevices.get(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
|
mLeDevices.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getCount() {
|
|
|
|
|
return mLeDevices.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object getItem(int i) {
|
|
|
|
|
return mLeDevices.get(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public long getItemId(int i) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
|
|
|
LeDeviceListAdapter.ViewHolder viewHolder;
|
|
|
|
|
if (view == null) {
|
|
|
|
|
view = mInflator.inflate(R.layout.listitem_device, null);
|
|
|
|
|
viewHolder = new LeDeviceListAdapter.ViewHolder();
|
|
|
|
|
viewHolder.deviceName = view.findViewById(R.id.device_name);
|
|
|
|
|
viewHolder.deviceStatus = view.findViewById(R.id.device_status);
|
|
|
|
|
viewHolder.deviceBind = view.findViewById(R.id.device_bind);
|
|
|
|
|
viewHolder.deviceDeBind = view.findViewById(R.id.device_de_bind);
|
|
|
|
|
viewHolder.sendData = view.findViewById(R.id.send_data);
|
|
|
|
|
view.setTag(viewHolder);
|
|
|
|
|
} else {
|
|
|
|
|
viewHolder = (LeDeviceListAdapter.ViewHolder) view.getTag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BluetoothDevice device = mLeDevices.get(i);
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
String deviceName = device.getName();
|
|
|
|
|
if (deviceName != null && !deviceName.isEmpty())
|
|
|
|
|
viewHolder.deviceName.setText(deviceName);
|
|
|
|
|
else{
|
|
|
|
|
viewHolder.deviceName.setText(R.string.unknown_device);
|
|
|
|
|
}
|
|
|
|
|
//当前点击选中的变色
|
|
|
|
|
if (i == selectItem) {
|
|
|
|
|
viewHolder.deviceName.getPaint().setFakeBoldText(true);
|
|
|
|
|
//viewHolder.deviceName.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
|
|
|
|
|
viewHolder.deviceStatus.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
|
|
|
|
|
viewHolder.deviceStatus.setText("已连接");
|
|
|
|
|
} else {
|
|
|
|
|
viewHolder.deviceName.getPaint().setFakeBoldText(false);
|
|
|
|
|
//viewHolder.deviceName.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
|
|
|
|
|
viewHolder.deviceStatus.setTextColor(Color.parseColor("#000000"));
|
|
|
|
|
viewHolder.deviceStatus.setText("未绑定");
|
|
|
|
|
}
|
|
|
|
|
viewHolder.deviceBind.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if(callBackBlueToothBind!=null){
|
|
|
|
|
callBackBlueToothBind.Click(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
viewHolder.deviceDeBind.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if(callBackBlueToothUnBind!=null){
|
|
|
|
|
callBackBlueToothUnBind.Click(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
viewHolder.sendData.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if(callBackBlueToothSendData!=null){
|
|
|
|
|
callBackBlueToothSendData.Click(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSelectItem(int selectItem) {
|
|
|
|
|
this.selectItem = selectItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int selectItem = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|