|
|
|
|
@ -0,0 +1,565 @@
|
|
|
|
|
package com.rehome.dywoa.bleUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
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.BroadcastReceiver;
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
|
import android.content.ServiceConnection;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
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 com.google.gson.Gson;
|
|
|
|
|
import com.rehome.dywoa.Contans;
|
|
|
|
|
import com.rehome.dywoa.R;
|
|
|
|
|
import com.rehome.dywoa.base.BaseActivity;
|
|
|
|
|
import com.rehome.dywoa.utils.SPUtils;
|
|
|
|
|
import com.rehome.dywoa.weiget.AuditDialog;
|
|
|
|
|
import com.rehome.dywoa.weiget.ConfirmDialog;
|
|
|
|
|
import com.rehome.dywoa.weiget.WaitDialog;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Timer;
|
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BlueToothDeviceSdkActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
ListView lv;
|
|
|
|
|
TextView tvNodata;
|
|
|
|
|
TextView ble_status;
|
|
|
|
|
TextView ble_dianlang;
|
|
|
|
|
// TextView txData;
|
|
|
|
|
// TextView ztData;
|
|
|
|
|
private String cmd = "";
|
|
|
|
|
|
|
|
|
|
private LeDeviceListAdapter mLeDeviceListAdapter;
|
|
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
|
|
private BluetoothLeScanner bluetoothLeScanner;
|
|
|
|
|
private boolean mScanning;
|
|
|
|
|
private Handler mHandler;
|
|
|
|
|
private static final int REQUEST_ENABLE_BT = 1;
|
|
|
|
|
private static final long SCAN_PERIOD = 10000;
|
|
|
|
|
private static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 123;
|
|
|
|
|
private boolean initconn = false;
|
|
|
|
|
private String mDeviceAddress;
|
|
|
|
|
private String mBleName;
|
|
|
|
|
|
|
|
|
|
private String deviceNameSave;
|
|
|
|
|
private String deviceAddressSave;
|
|
|
|
|
private BluetoothLeService mBluetoothLeService;
|
|
|
|
|
private boolean mConnected = false;
|
|
|
|
|
private ArrayList<String> mPermissionList = new ArrayList();
|
|
|
|
|
|
|
|
|
|
private WaitDialog dialog;
|
|
|
|
|
|
|
|
|
|
private Timer timer;
|
|
|
|
|
static int bleSendType=0;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getContentViewID() {
|
|
|
|
|
return R.layout.activity_bluetoothlist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initView() {
|
|
|
|
|
initToolbar("蓝牙传感器管理", "扫描蓝牙", new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
showLog("扫描蓝牙设备");
|
|
|
|
|
reConnectBle();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
lv = findViewById(R.id.lv);
|
|
|
|
|
tvNodata = findViewById(R.id.tv_nodata);
|
|
|
|
|
ble_status = findViewById(R.id.ble_status);
|
|
|
|
|
ble_dianlang = findViewById(R.id.ble_dianlang);
|
|
|
|
|
|
|
|
|
|
dialog = new WaitDialog(context, "正在扫描蓝牙传感器...");
|
|
|
|
|
dialog.setCancelable(true);
|
|
|
|
|
|
|
|
|
|
tvNodata.setVisibility(View.GONE);
|
|
|
|
|
lv.setVisibility(View.VISIBLE);
|
|
|
|
|
mHandler = new Handler();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initData() {
|
|
|
|
|
deviceAddressSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_ADDRESS,"");
|
|
|
|
|
showLog("-------deviceAddressSave-------");
|
|
|
|
|
showLog(deviceAddressSave);
|
|
|
|
|
deviceNameSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_NAME,"");
|
|
|
|
|
showLog("-------deviceNameSave-------");
|
|
|
|
|
showLog(mBleName);
|
|
|
|
|
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave)){
|
|
|
|
|
mHandler.postDelayed(this::reConnectBle, 1000);
|
|
|
|
|
}else{
|
|
|
|
|
//initPermission();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
|
|
|
|
|
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
|
|
|
|
|
//finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setAdapter();
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void reConnectBle(){
|
|
|
|
|
mLeDeviceListAdapter.clear();
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
mLeDeviceListAdapter.setSelectItem(-1);
|
|
|
|
|
// 注销广播接收器
|
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mGattUpdateReceiver);
|
|
|
|
|
if(mBluetoothLeService!=null){
|
|
|
|
|
mBluetoothLeService.disconnect();
|
|
|
|
|
mBluetoothLeService.close();
|
|
|
|
|
unbindService(mServiceConnection);
|
|
|
|
|
}
|
|
|
|
|
Intent intent = new Intent(this, BluetoothLeService.class);
|
|
|
|
|
stopService(intent);
|
|
|
|
|
|
|
|
|
|
initPermission();
|
|
|
|
|
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
|
|
|
|
|
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
|
|
|
|
|
//finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setAdapter();
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo 蓝牙动态申请权限
|
|
|
|
|
private void initPermission() {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
|
|
|
// Android 版本大于等于 Android12 时
|
|
|
|
|
// 只包括蓝牙这部分的权限,其余的需要什么权限自己添加
|
|
|
|
|
mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
|
|
|
|
|
mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
|
|
|
|
|
mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT);
|
|
|
|
|
} else {
|
|
|
|
|
// Android 版本小于 Android12 及以下版本
|
|
|
|
|
mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
|
|
|
|
|
mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
|
|
|
|
|
}
|
|
|
|
|
showLog(new Gson().toJson(mPermissionList.toArray(new String[0])));
|
|
|
|
|
ActivityCompat.requestPermissions(this, mPermissionList.toArray(new String[0]), 1001);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
Log.i("app", "onRequestPermissionsResult");
|
|
|
|
|
Log.i("app", String.valueOf(grantResults.length));
|
|
|
|
|
Log.i("app", new Gson().toJson(grantResults));
|
|
|
|
|
boolean hasGrant = true;
|
|
|
|
|
for (int grantResult : grantResults) {
|
|
|
|
|
if (grantResult != 0) {
|
|
|
|
|
//未授权
|
|
|
|
|
hasGrant = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (hasGrant) {
|
|
|
|
|
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
|
|
|
|
|
mBluetoothAdapter = bluetoothManager.getAdapter();
|
|
|
|
|
if (mBluetoothAdapter == null) {
|
|
|
|
|
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
|
|
|
|
|
// setAdapter();
|
|
|
|
|
Intent gattServiceIntent = new Intent(BlueToothDeviceSdkActivity.this, BluetoothLeService.class);
|
|
|
|
|
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
|
|
|
|
initconn = true;
|
|
|
|
|
|
|
|
|
|
//已授权
|
|
|
|
|
scanLeDevice(true);
|
|
|
|
|
} else {
|
|
|
|
|
showToast("您未授权开启蓝牙连接到Blue设备,请先开启权限");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {
|
|
|
|
|
super.onResume();
|
|
|
|
|
if (mBluetoothAdapter != null && (!mBluetoothAdapter.isEnabled())) {
|
|
|
|
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
|
|
|
|
}
|
|
|
|
|
if(timer==null){
|
|
|
|
|
try {
|
|
|
|
|
timer = new Timer();
|
|
|
|
|
timer.schedule(new TimerTask() {
|
|
|
|
|
public void run() {
|
|
|
|
|
// if(mBluetoothLeService!=null){
|
|
|
|
|
// bleSendType++;
|
|
|
|
|
// if(bleSendType%2==0){
|
|
|
|
|
// mBluetoothLeService.writeHex("0B0B6300");
|
|
|
|
|
// }
|
|
|
|
|
// if(bleSendType%3==0){
|
|
|
|
|
// mBluetoothLeService.writeString("BV");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}, 6000, 6000);
|
|
|
|
|
// 设定指定的时间time,此处为10000毫秒
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
showLog("timer is nnull");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
|
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
|
|
|
|
|
finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
|
finish();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scanLeDevice(final boolean enable) {
|
|
|
|
|
if (enable) {
|
|
|
|
|
dialog.show();
|
|
|
|
|
mHandler.postDelayed(() -> {
|
|
|
|
|
mScanning = false;
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
|
|
bluetoothLeScanner.stopScan(leScanCallback);
|
|
|
|
|
}, SCAN_PERIOD);
|
|
|
|
|
mScanning = true;
|
|
|
|
|
//mBluetoothAdapter.startLeScan(mLeScanCallback);
|
|
|
|
|
bluetoothLeScanner.startScan(leScanCallback);
|
|
|
|
|
} else {
|
|
|
|
|
mScanning = false;
|
|
|
|
|
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
|
|
if(bluetoothLeScanner!=null){
|
|
|
|
|
bluetoothLeScanner.stopScan(leScanCallback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//扫描到每台设备后调用回调函数callback.
|
|
|
|
|
// private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
|
|
// runOnUiThread(() -> {
|
|
|
|
|
// mLeDeviceListAdapter.addDevice(device);
|
|
|
|
|
// mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
private ScanCallback leScanCallback = new ScanCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onScanResult(int callbackType, ScanResult result) {
|
|
|
|
|
super.onScanResult(callbackType, result);
|
|
|
|
|
BluetoothDevice device = result.getDevice();
|
|
|
|
|
showLog("发现新设备啦");
|
|
|
|
|
showLog(new Gson().toJson(device));
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String bleName = device.getName();
|
|
|
|
|
String bleAddress = device.getAddress();
|
|
|
|
|
showLog(bleName);
|
|
|
|
|
showLog(bleAddress);
|
|
|
|
|
if (bleName != null && bleName.startsWith("W3")) {
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
mLeDeviceListAdapter.addDevice(device);
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave) && deviceAddressSave.equals(bleAddress) && deviceNameSave.equals(bleName)){
|
|
|
|
|
int count = mLeDeviceListAdapter.getCount();
|
|
|
|
|
confirmConnect(count-1);
|
|
|
|
|
//mBluetoothLeService.connect(deviceAddressSave);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private void setAdapter() {
|
|
|
|
|
mLeDeviceListAdapter = new LeDeviceListAdapter();
|
|
|
|
|
lv.setAdapter(mLeDeviceListAdapter);
|
|
|
|
|
lv.setOnItemClickListener((parent, view, position, id) -> {
|
|
|
|
|
AuditDialog confirmDialog = new AuditDialog(context, "您确定要绑定当前蓝牙传感器吗", new AuditDialog.AuditDialogListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void confirm() {
|
|
|
|
|
confirmConnect(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void cancel() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
confirmDialog.setCancelable(true);
|
|
|
|
|
confirmDialog.show();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void confirmConnect(int position) {
|
|
|
|
|
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
|
|
|
|
|
if (device == null) return;
|
|
|
|
|
if (mScanning) {
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
|
|
|
|
|
bluetoothLeScanner.stopScan(leScanCallback);
|
|
|
|
|
mScanning = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mDeviceAddress = device.getAddress();
|
|
|
|
|
mBleName = device.getName();
|
|
|
|
|
mLeDeviceListAdapter.setSelectItem(position);//记载当前点击选中的节点position,为了变色
|
|
|
|
|
mLeDeviceListAdapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
|
|
if (initconn) {
|
|
|
|
|
if(mBluetoothLeService!=null){
|
|
|
|
|
if (!mConnected) mBluetoothLeService.connect(mDeviceAddress);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Intent gattServiceIntent = new Intent(BlueToothDeviceSdkActivity.this, BluetoothLeService.class);
|
|
|
|
|
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
|
|
|
|
|
initconn = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class LeDeviceListAdapter extends BaseAdapter {
|
|
|
|
|
private class ViewHolder {
|
|
|
|
|
TextView deviceName;
|
|
|
|
|
TextView deviceStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ArrayList<BluetoothDevice> mLeDevices;
|
|
|
|
|
private LayoutInflater mInflator;
|
|
|
|
|
|
|
|
|
|
public LeDeviceListAdapter() {
|
|
|
|
|
super();
|
|
|
|
|
mLeDevices = new ArrayList<BluetoothDevice>();
|
|
|
|
|
mInflator = BlueToothDeviceSdkActivity.this.getLayoutInflater();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
ViewHolder viewHolder;
|
|
|
|
|
if (view == null) {
|
|
|
|
|
view = mInflator.inflate(R.layout.listitem_device, null);
|
|
|
|
|
viewHolder = new ViewHolder();
|
|
|
|
|
viewHolder.deviceName = view.findViewById(R.id.device_name);
|
|
|
|
|
viewHolder.deviceStatus = view.findViewById(R.id.device_status);
|
|
|
|
|
view.setTag(viewHolder);
|
|
|
|
|
} else {
|
|
|
|
|
viewHolder = (ViewHolder) view.getTag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BluetoothDevice device = mLeDevices.get(i);
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
final String deviceName = device.getName();
|
|
|
|
|
if (deviceName != null && deviceName.length() > 0)
|
|
|
|
|
viewHolder.deviceName.setText(deviceName);
|
|
|
|
|
else
|
|
|
|
|
viewHolder.deviceName.setText(R.string.unknown_device);
|
|
|
|
|
//当前点击选中的变色
|
|
|
|
|
if (i == selectItem) {
|
|
|
|
|
viewHolder.deviceStatus.getPaint().setFakeBoldText(true);
|
|
|
|
|
viewHolder.deviceStatus.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
|
|
|
|
|
viewHolder.deviceStatus.setText("已绑定");
|
|
|
|
|
} else {
|
|
|
|
|
viewHolder.deviceStatus.getPaint().setFakeBoldText(false);
|
|
|
|
|
viewHolder.deviceStatus.setTextColor(Color.parseColor("#000000"));
|
|
|
|
|
viewHolder.deviceStatus.setText("未绑定");
|
|
|
|
|
}
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSelectItem(int selectItem) {
|
|
|
|
|
this.selectItem = selectItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int selectItem = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ServiceConnection mServiceConnection = new ServiceConnection() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceConnected(ComponentName componentName, IBinder service) {
|
|
|
|
|
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
|
|
|
|
|
if (!mBluetoothLeService.initialize()) {
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
mBluetoothLeService.connect(mDeviceAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onServiceDisconnected(ComponentName componentName) {
|
|
|
|
|
mBluetoothLeService = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
|
final String action = intent.getAction();
|
|
|
|
|
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
|
|
|
|
|
mConnected = true;
|
|
|
|
|
//ztData.setText("蓝牙状态:已连接");
|
|
|
|
|
ble_status.setText("已连接");
|
|
|
|
|
ble_status.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
|
|
|
|
|
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_ADDRESS, mDeviceAddress);
|
|
|
|
|
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_NAME, mBleName);
|
|
|
|
|
|
|
|
|
|
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
|
|
|
|
|
mConnected = false;
|
|
|
|
|
//ztData.setText("蓝牙状态:已断开");
|
|
|
|
|
ble_status.setText("已断开");
|
|
|
|
|
ble_status.setTextColor(Color.parseColor("#ff0000"));//连接之后的颜色
|
|
|
|
|
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
|
|
|
|
|
//ztData.setText("蓝牙状态:已连接,仪器准备就绪");
|
|
|
|
|
showLog("数据传输通道准备就绪,可以发送数据了。");
|
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
mBluetoothLeService.writeString("BV");
|
|
|
|
|
}
|
|
|
|
|
},2000);
|
|
|
|
|
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
|
|
|
|
|
//String Data = "";
|
|
|
|
|
String resultReceive = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
|
|
|
|
|
// String type = BleData.substring(0, 1);
|
|
|
|
|
// if (type.equals("T")) {
|
|
|
|
|
// Data = "温度:" + BleData.substring(2, 6);
|
|
|
|
|
// } else {
|
|
|
|
|
// Data = "震动:" + BleData.substring(2, 6);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if(!TextUtils.isEmpty(resultReceive)){
|
|
|
|
|
List<String> listDianLiang = stringToListBlank(resultReceive);
|
|
|
|
|
if(!listDianLiang.isEmpty()){
|
|
|
|
|
showLog(listDianLiang.get(0));
|
|
|
|
|
showLog(listDianLiang.get(1));
|
|
|
|
|
List<String> strDianLiang = stringToList(listDianLiang.get(0));
|
|
|
|
|
if(!strDianLiang.isEmpty()){
|
|
|
|
|
ble_dianlang.setText(strDianLiang.get(1)+strDianLiang.get(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
showLog(resultReceive);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//txData.setText(Data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static IntentFilter makeGattUpdateIntentFilter() {
|
|
|
|
|
final IntentFilter intentFilter = new IntentFilter();
|
|
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
|
|
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
|
|
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
|
|
|
|
|
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
|
|
|
|
|
return intentFilter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> stringToList(String strs) {
|
|
|
|
|
String str[] = strs.split(":");
|
|
|
|
|
return Arrays.asList(str);
|
|
|
|
|
}
|
|
|
|
|
private List<String> stringToListBlank(String strs) {
|
|
|
|
|
String str[] = strs.split("\n");
|
|
|
|
|
return Arrays.asList(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
scanLeDevice(false);
|
|
|
|
|
mLeDeviceListAdapter.clear();
|
|
|
|
|
if(mBluetoothLeService!=null){
|
|
|
|
|
unbindService(mServiceConnection);
|
|
|
|
|
}
|
|
|
|
|
// 注销广播接收器
|
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mGattUpdateReceiver);
|
|
|
|
|
if(timer!=null){
|
|
|
|
|
timer.cancel();
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
}
|