youhua
This commit is contained in:
parent
e2c7c647d3
commit
c0cd52b1e2
|
|
@ -26,5 +26,5 @@ android.enableJetifier=true
|
|||
android.useAndroidX=true
|
||||
android.useDeprecatedNdk=true
|
||||
|
||||
versionCode=45
|
||||
versionName=4.11.5
|
||||
versionCode=47
|
||||
versionName=4.11.7
|
||||
|
|
@ -43,7 +43,7 @@ import java.util.ArrayList;
|
|||
* @date 2016-5-20 下午5:29:10
|
||||
*/
|
||||
public class BindTwoDeviceActivity extends BaseActivity {
|
||||
private ImageView iv_camera, iv_device, iv_more, iv_hot, iv_pet,iv_bluetooth,iv_bluetooth2;
|
||||
private ImageView iv_camera, iv_device, iv_more, iv_hot, iv_pet;
|
||||
private int width, height;
|
||||
private WifiAdmin wifiAdmin;
|
||||
private String wifissid = "";
|
||||
|
|
@ -81,8 +81,6 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
iv_more = findMyViewById(R.id.iv_more);
|
||||
iv_hot = findMyViewById(R.id.iv_hot);
|
||||
iv_pet = findMyViewById(R.id.iv_pet);
|
||||
iv_bluetooth = findMyViewById(R.id.iv_bluetooth);
|
||||
iv_bluetooth2 = findMyViewById(R.id.iv_bluetooth2);
|
||||
//得到屏幕宽度按照比例为控件设置高度
|
||||
// double f=width/1.432;
|
||||
// height=(int)f;
|
||||
|
|
@ -100,13 +98,7 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
iv_device.setOnClickListener(this);
|
||||
iv_hot.setOnClickListener(this);
|
||||
iv_pet.setOnClickListener(this);
|
||||
iv_bluetooth.setOnClickListener(this);
|
||||
iv_bluetooth2.setOnClickListener(this);
|
||||
|
||||
if (BuildConfig.DEBUG){
|
||||
iv_bluetooth.setVisibility(View.VISIBLE);
|
||||
iv_bluetooth2.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -231,70 +223,49 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
break;
|
||||
}
|
||||
|
||||
case R.id.iv_bluetooth:{
|
||||
permissionHelper = new PermissionHelper(BindTwoDeviceActivity.this, perlist, 10);
|
||||
if (permissionHelper.checkSelfPermission(perlist)){
|
||||
isOpen();
|
||||
}else {
|
||||
permissionHelper.request(new PermissionHelper.PermissionCallback() {
|
||||
@Override
|
||||
public void onPermissionGranted() {
|
||||
isOpen();
|
||||
}
|
||||
// case R.id.iv_bluetooth:{
|
||||
// permissionHelper = new PermissionHelper(BindTwoDeviceActivity.this, perlist, 10);
|
||||
// if (permissionHelper.checkSelfPermission(perlist)){
|
||||
// isOpen();
|
||||
// }else {
|
||||
// permissionHelper.request(new PermissionHelper.PermissionCallback() {
|
||||
// @Override
|
||||
// public void onPermissionGranted() {
|
||||
// isOpen();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onIndividualPermissionGranted(String[] grantedPermission) {
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onPermissionDenied() {
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onPermissionDeniedBySystem() {
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
|
||||
@Override
|
||||
public void onIndividualPermissionGranted(String[] grantedPermission) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPermissionDenied() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPermissionDeniedBySystem() {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case R.id.iv_bluetooth2:{
|
||||
Intent intent1 = new Intent();
|
||||
intent1.setClass(BindTwoDeviceActivity.this, BluetoothActivity.class);
|
||||
intent1.putExtra("devicetype", "bluetooth");
|
||||
intent1.putExtra("title", "蓝牙插座");
|
||||
startActivity(intent1);
|
||||
AnimationUtil.startAnimation(BindTwoDeviceActivity.this);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void getPer(){
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void toBlueTooth(){
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(BindTwoDeviceActivity.this, BlueTooth2Activity.class);
|
||||
intent.putExtra("devicetype", "bluetooth");
|
||||
intent.putExtra("title", "蓝牙插座");
|
||||
startActivity(intent);
|
||||
AnimationUtil.startAnimation(BindTwoDeviceActivity.this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode==10){
|
||||
isOpen();
|
||||
}else if (requestCode == 11){ //打开蓝牙
|
||||
toBlueTooth();
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
// super.onActivityResult(requestCode, resultCode, data);
|
||||
// if (requestCode==10){
|
||||
// isOpen();
|
||||
// }else if (requestCode == 11){ //打开蓝牙
|
||||
// toBlueTooth();
|
||||
// }
|
||||
// }
|
||||
|
||||
private void openBlueTooth(){
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(BindTwoDeviceActivity.this, AlertDialog.THEME_HOLO_LIGHT);
|
||||
|
|
@ -316,12 +287,4 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
});
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void isOpen(){
|
||||
if (blueToothController.isOpen()){
|
||||
toBlueTooth();
|
||||
}else {
|
||||
openBlueTooth();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,699 +0,0 @@
|
|||
package com.ifish.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.ifish.baseclass.BaseActivity;
|
||||
import com.ifish.bluetooth.ClsUtils;
|
||||
import com.ifish.bluetooth.ConnectThread;
|
||||
import com.ifish.bluetooth.ConnectedThread;
|
||||
import com.ifish.bluetooth.LVDevicesAdapter;
|
||||
import com.ifish.utils.L;
|
||||
|
||||
|
||||
public class BlueTooth2Activity extends BaseActivity implements View.OnClickListener {
|
||||
|
||||
private static final String TAG = "BtMain";
|
||||
|
||||
private static final int CONNECT_SUCCESS = 0x01;
|
||||
private static final int CONNECT_FAILURE = 0x02;
|
||||
private static final int DISCONNECT_SUCCESS = 0x03;
|
||||
private static final int SEND_SUCCESS = 0x04;
|
||||
private static final int SEND_FAILURE= 0x05;
|
||||
private static final int RECEIVE_SUCCESS= 0x06;
|
||||
private static final int RECEIVE_FAILURE =0x07;
|
||||
private static final int START_DISCOVERY = 0x08;
|
||||
private static final int STOP_DISCOVERY = 0x09;
|
||||
private static final int DISCOVERY_DEVICE = 0x0A;
|
||||
private static final int DEVICE_BOND_NONE= 0x0B;
|
||||
private static final int DEVICE_BONDING = 0x0C;
|
||||
private static final int DEVICE_BONDED = 0x0D;
|
||||
|
||||
private Button btSearch;
|
||||
private TextView tvCurConState;
|
||||
private TextView tvCurBondState;
|
||||
private TextView tvName;
|
||||
private TextView tvAddress;
|
||||
private Button btConnect;
|
||||
private Button btDisconnect;
|
||||
private Button btBound;
|
||||
private Button btDisBound;
|
||||
private EditText etSendMsg;
|
||||
private Button btSend;
|
||||
private TextView tvSendResult;
|
||||
private TextView tvReceive;
|
||||
private LinearLayout llDeviceList;
|
||||
private LinearLayout llDataSendReceive;
|
||||
private ListView lvDevices;
|
||||
private LVDevicesAdapter lvDevicesAdapter;
|
||||
|
||||
//蓝牙
|
||||
private BluetoothAdapter bluetoothAdapter;
|
||||
private BtBroadcastReceiver btBroadcastReceiver;
|
||||
//连接设备的UUID
|
||||
public static final String MY_BLUETOOTH_UUID = "00001101-0000-1000-8000-00805F9B34FB"; //蓝牙通讯
|
||||
//当前要连接的设备
|
||||
private BluetoothDevice curBluetoothDevice;
|
||||
//发起连接的线程
|
||||
private ConnectThread connectThread;
|
||||
//管理连接的线程
|
||||
private ConnectedThread connectedThread;
|
||||
//当前设备连接状态
|
||||
private boolean curConnState = false;
|
||||
//当前设备与系统配对状态
|
||||
private boolean curBondState = false;
|
||||
|
||||
|
||||
@SuppressLint("HandlerLeak")
|
||||
private Handler mHandler = new Handler(){
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
|
||||
switch(msg.what){
|
||||
case START_DISCOVERY:
|
||||
Log.d(TAG, "开始搜索设备...");
|
||||
break;
|
||||
|
||||
case STOP_DISCOVERY:
|
||||
Log.d(TAG, "停止搜索设备...");
|
||||
break;
|
||||
|
||||
case DISCOVERY_DEVICE: //扫描到设备
|
||||
BluetoothDevice bluetoothDevice = (BluetoothDevice) msg.obj;
|
||||
lvDevicesAdapter.addDevice(bluetoothDevice);
|
||||
|
||||
break;
|
||||
|
||||
case CONNECT_FAILURE: //连接失败
|
||||
Log.d(TAG, "连接失败");
|
||||
tvCurConState.setText("连接失败");
|
||||
curConnState = false;
|
||||
break;
|
||||
|
||||
case CONNECT_SUCCESS: //连接成功
|
||||
Log.d(TAG, "连接成功");
|
||||
tvCurConState.setText("连接成功");
|
||||
curConnState = true;
|
||||
llDataSendReceive.setVisibility(View.VISIBLE);
|
||||
llDeviceList.setVisibility(View.GONE);
|
||||
break;
|
||||
|
||||
case DISCONNECT_SUCCESS:
|
||||
tvCurConState.setText("断开成功");
|
||||
curConnState = false;
|
||||
|
||||
break;
|
||||
|
||||
case SEND_FAILURE: //发送失败
|
||||
Toast.makeText(BlueTooth2Activity.this, "发送失败", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
|
||||
case SEND_SUCCESS: //发送成功
|
||||
String sendResult = (String) msg.obj;
|
||||
tvSendResult.setText(sendResult);
|
||||
break;
|
||||
|
||||
case RECEIVE_FAILURE: //接收失败
|
||||
String receiveError = (String) msg.obj;
|
||||
tvReceive.setText(receiveError);
|
||||
break;
|
||||
|
||||
case RECEIVE_SUCCESS: //接收成功
|
||||
String receiveResult = (String) msg.obj;
|
||||
tvReceive.setText(receiveResult);
|
||||
break;
|
||||
|
||||
case DEVICE_BOND_NONE: //已解除配对
|
||||
tvCurBondState.setText("解除配对成功");
|
||||
curBondState = false;
|
||||
|
||||
break;
|
||||
|
||||
case DEVICE_BONDING: //正在配对
|
||||
tvCurBondState.setText("正在配对...");
|
||||
break;
|
||||
|
||||
case DEVICE_BONDED: //已配对
|
||||
tvCurBondState.setText("配对成功");
|
||||
curBondState = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.layout_blue2);
|
||||
initTitle("蓝牙选择设备");
|
||||
//初始化视图
|
||||
initView();
|
||||
//初始化监听
|
||||
iniListener();
|
||||
//初始化数据
|
||||
initData();
|
||||
//初始化蓝牙
|
||||
initBluetooth();
|
||||
//初始化蓝牙广播
|
||||
initBtBroadcast();
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
|
||||
|
||||
|
||||
lvDevicesAdapter = new LVDevicesAdapter(BlueTooth2Activity.this);
|
||||
lvDevices.setAdapter(lvDevicesAdapter);
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
btSearch = findViewById(R.id.bt_search);
|
||||
tvCurConState = findViewById(R.id.tv_cur_con_state);
|
||||
tvCurBondState = findViewById(R.id.tv_cur_bond_state);
|
||||
btConnect = findViewById(R.id.bt_connect);
|
||||
btDisconnect = findViewById(R.id.bt_disconnect);
|
||||
btBound = findViewById(R.id.bt_bound);
|
||||
btDisBound = findViewById(R.id.bt_disBound);
|
||||
tvName = findViewById(R.id.tv_name);
|
||||
tvAddress = findViewById(R.id.tv_address);
|
||||
etSendMsg = findViewById(R.id.et_send_msg);
|
||||
btSend = findViewById(R.id.bt_to_send);
|
||||
tvSendResult = findViewById(R.id.tv_send_result);
|
||||
tvReceive = findViewById(R.id.tv_receive_result);
|
||||
llDeviceList = findViewById(R.id.ll_device_list);
|
||||
llDataSendReceive = findViewById(R.id.ll_data_send_receive);
|
||||
lvDevices = findViewById(R.id.lv_devices);
|
||||
}
|
||||
|
||||
private void iniListener() {
|
||||
btSearch.setOnClickListener(this);
|
||||
btConnect.setOnClickListener(this);
|
||||
btDisconnect.setOnClickListener(this);
|
||||
btBound.setOnClickListener(this);
|
||||
btDisBound.setOnClickListener(this);
|
||||
btSend.setOnClickListener(this);
|
||||
|
||||
lvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
BluetoothDevice bluetoothDevice = (BluetoothDevice) lvDevicesAdapter.getItem(i);
|
||||
tvName.setText(bluetoothDevice.getName());
|
||||
tvAddress.setText(bluetoothDevice.getAddress());
|
||||
curBluetoothDevice = bluetoothDevice;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.bt_search: //搜索蓝牙
|
||||
Toast.makeText(BlueTooth2Activity.this,"",Toast.LENGTH_SHORT).show();
|
||||
llDataSendReceive.setVisibility(View.GONE);
|
||||
llDeviceList.setVisibility(View.VISIBLE);
|
||||
searchBtDevice();
|
||||
break;
|
||||
|
||||
case R.id.bt_connect: //连接蓝牙
|
||||
if(!curConnState) {
|
||||
startConnectDevice(curBluetoothDevice, MY_BLUETOOTH_UUID, 10000);
|
||||
}else{
|
||||
Toast.makeText(this, "当前设备已连接", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
|
||||
case R.id.bt_disconnect: //断开连接
|
||||
if(curConnState) {
|
||||
clearConnectedThread();
|
||||
}else{
|
||||
Toast.makeText(this, "当前设备未连接", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
|
||||
case R.id.bt_bound: //配对设备
|
||||
if(!curBondState) {
|
||||
boundDevice(curBluetoothDevice);
|
||||
}else{
|
||||
Toast.makeText(this, "当前设备已经与系统蓝牙建立配对", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
|
||||
case R.id.bt_disBound: //解除配对
|
||||
if(curBondState) {
|
||||
disBoundDevice(curBluetoothDevice);
|
||||
}else{
|
||||
Toast.makeText(this, "当前设备尚未与系统蓝牙建立配对", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
|
||||
case R.id.bt_to_send: //发送数据
|
||||
if(curConnState){
|
||||
String sendMsg = etSendMsg.getText().toString();
|
||||
if(sendMsg.isEmpty()){
|
||||
Toast.makeText(this, "发送数据为空!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
sendData(sendMsg,true); //以16进制字符串形式发送数据
|
||||
}else{
|
||||
Toast.makeText(this, "请先连接当前设备", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////// 搜索设备 /////////////////////////////////////////////////
|
||||
@SuppressLint("MissingPermission")
|
||||
private void searchBtDevice() {
|
||||
if (bluetoothAdapter.isDiscovering()) { //当前正在搜索设备...
|
||||
return;
|
||||
}
|
||||
//开始搜索
|
||||
bluetoothAdapter.startDiscovery();
|
||||
}
|
||||
|
||||
////////////////////////////////// 配对/接触配对设备 ////////////////////////////////////////////
|
||||
/**
|
||||
* 执行绑定 反射
|
||||
* @param bluetoothDevice 蓝牙设备
|
||||
* @return true 执行绑定 false 未执行绑定
|
||||
*/
|
||||
public boolean boundDevice(BluetoothDevice bluetoothDevice){
|
||||
if(bluetoothDevice == null){
|
||||
Log.e(TAG,"boundDevice-->bluetoothDevice == null");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && false) {
|
||||
L.i(bluetoothDevice.getName()+"jjia--------1--------type="+bluetoothDevice.getType());
|
||||
return bluetoothDevice.createBond();
|
||||
}else {
|
||||
L.i(bluetoothDevice.getName()+"jjia--------2--------type="+bluetoothDevice.getType());
|
||||
// return ClsUtils.createBond(BluetoothDevice.class,bluetoothDevice);
|
||||
return ClsUtils.connn(bluetoothDevice,MY_BLUETOOTH_UUID);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行解绑 反射
|
||||
* @param bluetoothDevice 蓝牙设备
|
||||
* @return true 执行解绑 false未执行解绑
|
||||
*/
|
||||
public boolean disBoundDevice(BluetoothDevice bluetoothDevice){
|
||||
if(bluetoothDevice == null){
|
||||
Log.e(TAG,"disBoundDevice-->bluetoothDevice == null");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return ClsUtils.removeBond(BluetoothDevice.class,bluetoothDevice);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////// 连接设备 ///////////////////////////////////////////////
|
||||
/**
|
||||
* 开始连接设备
|
||||
* @param bluetoothDevice 蓝牙设备
|
||||
* @param uuid 发起连接的UUID
|
||||
* @param conOutTime 连接超时时间
|
||||
*/
|
||||
public void startConnectDevice(final BluetoothDevice bluetoothDevice, String uuid, long conOutTime){
|
||||
if(bluetoothDevice == null){
|
||||
Log.e(TAG,"startConnectDevice-->bluetoothDevice == null");
|
||||
return;
|
||||
}
|
||||
if(bluetoothAdapter == null){
|
||||
Log.e(TAG,"startConnectDevice-->bluetooth3Adapter == null");
|
||||
return;
|
||||
}
|
||||
//发起连接
|
||||
connectThread = new ConnectThread(bluetoothAdapter,curBluetoothDevice,uuid);
|
||||
connectThread.setOnBluetoothConnectListener(new ConnectThread.OnBluetoothConnectListener() {
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
public void onStartConn() {
|
||||
Log.d(TAG,"startConnectDevice-->开始连接..." + bluetoothDevice.getName() + "-->" + bluetoothDevice.getAddress());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onConnSuccess(BluetoothSocket bluetoothSocket) {
|
||||
//移除连接超时
|
||||
mHandler.removeCallbacks(connectOuttimeRunnable);
|
||||
Log.d(TAG,"startConnectDevice-->移除连接超时");
|
||||
Log.w(TAG,"startConnectDevice-->连接成功");
|
||||
|
||||
Message message = new Message();
|
||||
message.what = CONNECT_SUCCESS;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
//标记当前连接状态为true
|
||||
curConnState = true;
|
||||
//管理连接,收发数据
|
||||
managerConnectSendReceiveData(bluetoothSocket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnFailure(String errorMsg) {
|
||||
Log.e(TAG,"startConnectDevice-->" + errorMsg);
|
||||
|
||||
Message message = new Message();
|
||||
message.what = CONNECT_FAILURE;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
//标记当前连接状态为false
|
||||
curConnState = false;
|
||||
|
||||
//断开管理连接
|
||||
clearConnectedThread();
|
||||
}
|
||||
});
|
||||
|
||||
connectThread.start();
|
||||
//设置连接超时时间
|
||||
mHandler.postDelayed(connectOuttimeRunnable,conOutTime);
|
||||
|
||||
}
|
||||
|
||||
//连接超时
|
||||
private Runnable connectOuttimeRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.e(TAG,"startConnectDevice-->连接超时" );
|
||||
|
||||
Message message = new Message();
|
||||
message.what = CONNECT_FAILURE;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
//标记当前连接状态为false
|
||||
curConnState = false;
|
||||
//断开管理连接
|
||||
clearConnectedThread();
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////// 断开连接 //////////////////////////////////////////////
|
||||
/**
|
||||
* 断开已有的连接
|
||||
*/
|
||||
public void clearConnectedThread(){
|
||||
Log.d(TAG,"clearConnectedThread-->即将断开");
|
||||
|
||||
//connectedThread断开已有连接
|
||||
if(connectedThread == null){
|
||||
Log.e(TAG,"clearConnectedThread-->connectedThread == null");
|
||||
return;
|
||||
}
|
||||
connectedThread.terminalClose(connectThread);
|
||||
|
||||
//等待线程运行完后再断开
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
connectedThread.cancel(); //释放连接
|
||||
|
||||
connectedThread = null;
|
||||
}
|
||||
},10);
|
||||
|
||||
Log.w(TAG,"clearConnectedThread-->成功断开连接");
|
||||
Message message = new Message();
|
||||
message.what = DISCONNECT_SUCCESS;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////////////////////////// 管理已有连接、收发数据 //////////////////////////////////
|
||||
|
||||
/**
|
||||
* 管理已建立的连接,收发数据
|
||||
* @param bluetoothSocket 已建立的连接
|
||||
*/
|
||||
public void managerConnectSendReceiveData(BluetoothSocket bluetoothSocket){
|
||||
//管理已有连接
|
||||
connectedThread = new ConnectedThread(bluetoothSocket);
|
||||
connectedThread.start();
|
||||
connectedThread.setOnSendReceiveDataListener(new ConnectedThread.OnSendReceiveDataListener() {
|
||||
@Override
|
||||
public void onSendDataSuccess(byte[] data) {
|
||||
Log.w(TAG,"发送数据成功,长度" + data.length + "->" + bytes2HexString(data,data.length));
|
||||
Message message = new Message();
|
||||
message.what = SEND_SUCCESS;
|
||||
message.obj = "发送数据成功,长度" + data.length + "->" + bytes2HexString(data,data.length);
|
||||
mHandler.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendDataError(byte[] data,String errorMsg) {
|
||||
Log.e(TAG,"发送数据出错,长度" + data.length + "->" + bytes2HexString(data,data.length));
|
||||
Message message = new Message();
|
||||
message.what = SEND_FAILURE;
|
||||
message.obj = "发送数据出错,长度" + data.length + "->" + bytes2HexString(data,data.length);
|
||||
mHandler.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceiveDataSuccess(byte[] buffer) {
|
||||
Log.w(TAG,"成功接收数据,长度" + buffer.length + "->" + bytes2HexString(buffer,buffer.length));
|
||||
Message message = new Message();
|
||||
message.what = RECEIVE_SUCCESS;
|
||||
message.obj = "成功接收数据,长度" + buffer.length + "->" + bytes2HexString(buffer,buffer.length);
|
||||
mHandler.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceiveDataError(String errorMsg) {
|
||||
Log.e(TAG,"接收数据出错:" + errorMsg);
|
||||
Message message = new Message();
|
||||
message.what = RECEIVE_FAILURE;
|
||||
message.obj = "接收数据出错:" + errorMsg;
|
||||
mHandler.sendMessage(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
///////////////////////////////// 发送数据 /////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 发送数据
|
||||
* @param data 要发送的数据 字符串
|
||||
* @param isHex 是否是16进制字符串
|
||||
* @return true 发送成功 false 发送失败
|
||||
*/
|
||||
public boolean sendData(String data,boolean isHex){
|
||||
if(connectedThread == null){
|
||||
Log.e(TAG,"sendData:string -->connectedThread == null");
|
||||
return false;
|
||||
}
|
||||
if(data == null || data.length() == 0){
|
||||
Log.e(TAG,"sendData:string-->要发送的数据为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(isHex){ //是16进制字符串
|
||||
data.replace(" ",""); //取消空格
|
||||
//检查16进制数据是否合法
|
||||
if(data.length() % 2 != 0){
|
||||
//不合法,最后一位自动填充0
|
||||
String lasts = "0" + data.charAt(data.length() - 1);
|
||||
data = data.substring(0,data.length() - 2) + lasts;
|
||||
}
|
||||
Log.d(TAG,"sendData:string -->准备写入:" + data); //加空格显示
|
||||
return connectedThread.write(hexString2Bytes(data));
|
||||
}
|
||||
|
||||
//普通字符串
|
||||
Log.d(TAG,"sendData:string -->准备写入:" + data);
|
||||
return connectedThread.write(data.getBytes());
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////////////////////// 数据类型转换 //////////////////////////////////////////////
|
||||
/**
|
||||
* 字节数组-->16进制字符串
|
||||
* @param b 字节数组
|
||||
* @param length 字节数组长度
|
||||
* @return 16进制字符串 有空格类似“0A D5 CD 8F BD E5 F8”
|
||||
*/
|
||||
public static String bytes2HexString(byte[] b, int length) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
String hex;
|
||||
for (int i = 0; i < length; i++) {
|
||||
hex = Integer.toHexString(b[i] & 0xFF);
|
||||
if (hex.length() == 1) {
|
||||
hex = '0' + hex;
|
||||
}
|
||||
result.append(hex.toUpperCase()).append(" ");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* hexString2Bytes
|
||||
* 16进制字符串-->字节数组
|
||||
* @param src 16进制字符串
|
||||
* @return 字节数组
|
||||
*/
|
||||
public static byte[] hexString2Bytes(String src) {
|
||||
int l = src.length() / 2;
|
||||
byte[] ret = new byte[l];
|
||||
for (int i = 0; i < l; i++) {
|
||||
ret[i] = (byte) Integer
|
||||
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 初始化蓝牙
|
||||
*/
|
||||
@SuppressLint("MissingPermission")
|
||||
private void initBluetooth() {
|
||||
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bluetoothAdapter == null) {
|
||||
Toast.makeText(this, "当前手机设备不支持蓝牙", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
//手机设备支持蓝牙,判断蓝牙是否已开启
|
||||
if (bluetoothAdapter.isEnabled()) {
|
||||
Toast.makeText(this, "手机蓝牙已开启", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
//蓝牙没有打开,去打开蓝牙。推荐使用第二种打开蓝牙方式
|
||||
//第一种方式:直接打开手机蓝牙,没有任何提示
|
||||
// bluetoothAdapter.enable(); //BLUETOOTH_ADMIN权限
|
||||
//第二种方式:友好提示用户打开蓝牙
|
||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
startActivity(enableBtIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化蓝牙广播
|
||||
*/
|
||||
private void initBtBroadcast() {
|
||||
//注册广播接收
|
||||
btBroadcastReceiver = new BtBroadcastReceiver();
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //开始扫描
|
||||
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//扫描结束
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//搜索到设备
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //配对状态监听
|
||||
// 监视蓝牙关闭和打开的状态
|
||||
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||
registerReceiver(btBroadcastReceiver,intentFilter);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
//注销广播接收
|
||||
unregisterReceiver(btBroadcastReceiver);
|
||||
}
|
||||
|
||||
/**
|
||||
* 蓝牙广播接收器
|
||||
*/
|
||||
private class BtBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (TextUtils.equals(action, BluetoothAdapter.ACTION_DISCOVERY_STARTED)) { //开启搜索
|
||||
Log.d(TAG,"开启搜索...");
|
||||
Message message = new Message();
|
||||
message.what = START_DISCOVERY;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
} else if (TextUtils.equals(action, BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {//完成搜素
|
||||
Log.d(TAG,"停止搜索...");
|
||||
Message message = new Message();
|
||||
message.what = STOP_DISCOVERY;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
} else if (TextUtils.equals(action, BluetoothDevice.ACTION_FOUND)) { //3.0搜索到设备
|
||||
//蓝牙设备
|
||||
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
//信号强度
|
||||
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
|
||||
|
||||
Log.w(TAG, "扫描到设备:" + bluetoothDevice.getName() + "-->" + bluetoothDevice.getAddress());
|
||||
Message message = new Message();
|
||||
message.what = DISCOVERY_DEVICE;
|
||||
message.obj = bluetoothDevice;
|
||||
mHandler.sendMessage(message);
|
||||
|
||||
}else if(TextUtils.equals(action,BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
|
||||
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
int bondSate = bluetoothDevice.getBondState();
|
||||
L.i(bluetoothDevice.getName()+"jjia----------------bondSate="+bondSate);
|
||||
switch(bondSate) {
|
||||
case BluetoothDevice.BOND_NONE:
|
||||
Log.d(TAG, "已解除配对");
|
||||
Message message1 = new Message();
|
||||
message1.what = DEVICE_BOND_NONE;
|
||||
mHandler.sendMessage(message1);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.BOND_BONDING:
|
||||
Log.d(TAG, "正在配对...");
|
||||
Message message2 = new Message();
|
||||
message2.what = DEVICE_BONDING;
|
||||
mHandler.sendMessage(message2);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.BOND_BONDED:
|
||||
Log.d(TAG, "已配对");
|
||||
Message message3 = new Message();
|
||||
message3.what = DEVICE_BONDED;
|
||||
mHandler.sendMessage(message3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,350 +0,0 @@
|
|||
package com.ifish.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCallback;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattDescriptor;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.ParcelUuid;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.clj.fastble.BleManager;
|
||||
import com.clj.fastble.callback.BleGattCallback;
|
||||
import com.clj.fastble.callback.BleMtuChangedCallback;
|
||||
import com.clj.fastble.callback.BleNotifyCallback;
|
||||
import com.clj.fastble.callback.BleScanCallback;
|
||||
import com.clj.fastble.callback.BleWriteCallback;
|
||||
import com.clj.fastble.data.BleDevice;
|
||||
import com.clj.fastble.exception.BleException;
|
||||
import com.clj.fastble.scan.BleScanRuleConfig;
|
||||
import com.clj.fastble.utils.HexUtil;
|
||||
import com.ifish.adapter.BlueToothController;
|
||||
import com.ifish.basebean.BaseBean;
|
||||
import com.ifish.basebean.Device;
|
||||
import com.ifish.baseclass.BaseActivity;
|
||||
import com.ifish.bluetooth.ClsUtils;
|
||||
import com.ifish.tcp.BackFunctionCode7_11;
|
||||
import com.ifish.tcp.ModelCodec;
|
||||
import com.ifish.tcp.OrderDeviceConnectModel;
|
||||
import com.ifish.tcp.OrderModel;
|
||||
import com.ifish.utils.BlueToothUtil;
|
||||
import com.ifish.utils.ByteUtil;
|
||||
import com.ifish.utils.Commons;
|
||||
import com.ifish.utils.HttpListener;
|
||||
import com.ifish.utils.HttpManager;
|
||||
import com.ifish.utils.L;
|
||||
import com.ifish.utils.ToastUtil;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class BluetoothActivity extends BaseActivity {
|
||||
|
||||
public static final String SERVICE_UUID = "000000ff-0000-1000-8000-00805f9b34fb"; //蓝牙通讯服务
|
||||
public static final String READ_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //读特征
|
||||
public static final String WRITE_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //写特征
|
||||
|
||||
IntentFilter foundFilter = null;
|
||||
TextView tvname ;
|
||||
TextView tvname2 ;
|
||||
Button tv_pair;
|
||||
|
||||
BleDevice currentDevice;
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bluetooth);
|
||||
BleManager.getInstance().init(getApplication());
|
||||
|
||||
UUID[] uuids = {UUID.fromString(SERVICE_UUID)};
|
||||
BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
|
||||
.setServiceUuids(uuids) // 只扫描指定的服务的设备,可选
|
||||
// .setDeviceName(true, names) // 只扫描指定广播名的设备,可选
|
||||
// .setDeviceMac(mac) // 只扫描指定mac的设备,可选
|
||||
// .setAutoConnect(isAutoConnect) // 连接时的autoConnect参数,可选,默认false
|
||||
.setScanTimeOut(20000) // 扫描超时时间,可选,默认10秒;小于等于0表示不限制扫描时间
|
||||
.build();
|
||||
BleManager.getInstance().initScanRule(scanRuleConfig);
|
||||
|
||||
|
||||
|
||||
tvname = findMyViewById(R.id.tv_name);
|
||||
tvname2 = findMyViewById(R.id.tv_name2);
|
||||
tv_pair = findMyViewById(R.id.tv_pair);
|
||||
Button a1 = findMyViewById(R.id.a1);
|
||||
Button a2 = findMyViewById(R.id.a2);
|
||||
Button scan = findMyViewById(R.id.scan);
|
||||
Button conn = findMyViewById(R.id.conn);
|
||||
Button write = findMyViewById(R.id.write);
|
||||
Button nofi = findMyViewById(R.id.nofi);
|
||||
Button A = findMyViewById(R.id.A);
|
||||
Button stop = findMyViewById(R.id.stop);
|
||||
|
||||
stop.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
int value = currentDevice.getRssi();
|
||||
int iRssi = Math.abs(value);
|
||||
double power = (iRssi-50)/(10*3.3);
|
||||
double aaa = Math.pow(10, power);
|
||||
tvname2.setText(""+aaa);
|
||||
}
|
||||
});
|
||||
A.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
int value = currentDevice.getRssi();
|
||||
tvname2.setText(""+value);
|
||||
}
|
||||
});
|
||||
initTitle("蓝牙选择设备");
|
||||
|
||||
List<BleDevice> ss = BleManager.getInstance().getAllConnectedDevice();
|
||||
if (ss!=null){
|
||||
ToastUtil.show(this,ss.size()+"");
|
||||
for (BleDevice xx:ss){
|
||||
if (xx!=null && xx.getDevice()!=null && xx.getDevice().getName()!=null && xx.getDevice().getName().startsWith("ifish")){
|
||||
currentDevice = xx;
|
||||
tvname.setText(xx.getDevice().getName()+"已连接");
|
||||
}
|
||||
L.i("jjia-----conn----"+xx.getDevice().getName());
|
||||
}
|
||||
}
|
||||
a1.setOnClickListener(view -> {
|
||||
tvname.setText(String.format("%s", BleManager.getInstance().isSupportBle()));
|
||||
});
|
||||
|
||||
a2.setOnClickListener(view -> {
|
||||
tvname.setText(String.format("%s", BleManager.getInstance().isBlueEnable()));
|
||||
});
|
||||
|
||||
scan.setOnClickListener(view -> {
|
||||
BleManager.getInstance().scan(new BleScanCallback() {
|
||||
@Override
|
||||
public void onScanFinished(List<BleDevice> scanResultList) {
|
||||
ToastUtil.show(BluetoothActivity.this,"扫描结束");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanStarted(boolean success) {
|
||||
ToastUtil.show(BluetoothActivity.this,"开始扫描");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanning(BleDevice bleDevice) {
|
||||
|
||||
if (bleDevice!=null && bleDevice.getDevice()!=null){
|
||||
BluetoothDevice bean = bleDevice.getDevice();
|
||||
if (!TextUtils.isEmpty(bean.getName()) && bean.getName().startsWith("ifish")){
|
||||
currentDevice = bleDevice;
|
||||
tvname.setText(bean.getName()+bean.getAddress());
|
||||
BleManager.getInstance().cancelScan();
|
||||
}
|
||||
L.i(bean.getName()+"jjia---------"+bean.getAddress());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
conn.setOnClickListener(view -> {
|
||||
BleManager.getInstance().connect(currentDevice.getDevice().getAddress(), new BleGattCallback() {
|
||||
@Override
|
||||
public void onStartConnect() {
|
||||
ToastUtil.show(BluetoothActivity.this,"开始连接");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectFail(BleDevice bleDevice, BleException exception) {
|
||||
ToastUtil.show(BluetoothActivity.this,"连接失败");
|
||||
tvname.setText("连接失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
|
||||
ToastUtil.show(BluetoothActivity.this,"连接成功"+bleDevice.getRssi());
|
||||
|
||||
tvname.setText("连接成功"+bleDevice.getRssi());
|
||||
BleManager.getInstance().setMtu(bleDevice, 512, new BleMtuChangedCallback() {
|
||||
@Override
|
||||
public void onSetMTUFailure(BleException exception) {
|
||||
L.i("jjia-------MTU--"+exception.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMtuChanged(int mtu) {
|
||||
L.i("jjia-------MTU--"+mtu);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
||||
ToastUtil.show(BluetoothActivity.this,"连接断开了");
|
||||
tvname.setText("连接断开了");
|
||||
}
|
||||
});
|
||||
});
|
||||
tv_pair.setOnClickListener(view -> {
|
||||
|
||||
doPostBindDevice();
|
||||
});
|
||||
|
||||
nofi.setOnClickListener(view -> {
|
||||
BleManager.getInstance().notify(currentDevice, SERVICE_UUID, WRITE_UUID, new BleNotifyCallback() {
|
||||
@Override
|
||||
public void onNotifySuccess() {
|
||||
ToastUtil.show(BluetoothActivity.this,"打开通知成功");
|
||||
tvname.setText("打开通知成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotifyFailure(BleException exception) {
|
||||
ToastUtil.show(BluetoothActivity.this,"打开通知失败");
|
||||
tvname.setText("打开通知失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicChanged(byte[] data) {
|
||||
if (data!=null && data.length>0){
|
||||
String ssx = ByteUtil.bytesToHexString(data);
|
||||
tvname2.setText(ssx);
|
||||
int check_code = data[1];
|
||||
int length = data[14];
|
||||
byte[] dataByte = new byte[length];
|
||||
for (int i = 0; i < dataByte.length; i++) {
|
||||
dataByte[i] = data[i];
|
||||
}
|
||||
|
||||
String device_mac = null;
|
||||
Object object = ModelCodec.deCode(dataByte);
|
||||
if (object!=null && object instanceof BackFunctionCode7_11){
|
||||
BackFunctionCode7_11 model = (BackFunctionCode7_11) object;
|
||||
device_mac = ByteUtil.bytesToHexString(model.getSrc());
|
||||
tvname2.setText(device_mac);
|
||||
}else {
|
||||
//
|
||||
}
|
||||
L.i("jjia-------check_code-"+check_code);
|
||||
L.i("jjia-------length-"+length);
|
||||
L.i("jjia-------device_mac-"+device_mac);
|
||||
// L.i("jjia-------通知--"+ss1);
|
||||
// L.i("jjia-------通知--"+ss2);
|
||||
// L.i("jjia-------通知--"+ss3);
|
||||
// L.i("jjia-------通知--"+ss);
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
write.setOnClickListener(view -> {
|
||||
|
||||
String ssid = "CMDD";
|
||||
String pwd = "juanandyo";
|
||||
|
||||
OrderDeviceConnectModel model = OrderModel.OrderDeviceConnectModel(ssid, pwd);//设置正式环境域名
|
||||
byte[] data = ModelCodec.enCode(model);//
|
||||
|
||||
// String s = new String(data);
|
||||
// L.i("jjia-----------111-"+s);
|
||||
// byte [] xx = HexUtil.hexStringToBytes(s);
|
||||
//
|
||||
// L.i("jjia-----------数据大小-"+data.length);
|
||||
// L.i("jjia-----------222-"+xx.length);
|
||||
// L.i("jjia-----------222-"+xx);
|
||||
// try {
|
||||
// L.i("jjia-----------222-"+new String(xx,"UTF-8"));
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
BleManager.getInstance().write(currentDevice, SERVICE_UUID, WRITE_UUID, data,false, new BleWriteCallback() {
|
||||
@Override
|
||||
public void onWriteSuccess(int current, int total, byte[] justWrite) {
|
||||
|
||||
try {
|
||||
String aa = new String(justWrite,"UTF-8");
|
||||
String ssx = ByteUtil.bytesToHexString(justWrite);
|
||||
L.i(current+"x"+total+"jjia--------------写成功-"+aa +"$"+ssx);
|
||||
tvname.setText(current+"x"+total+"写成功-"+aa);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWriteFailure(BleException exception) {
|
||||
tvname.setText("写失败-"+exception.toString());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void bleNotify(){
|
||||
|
||||
}
|
||||
|
||||
Device deviceObj;
|
||||
|
||||
private void doPostBindDevice() {
|
||||
try {
|
||||
|
||||
HttpManager hm = HttpManager.getInstance();
|
||||
hm.bindDevice(new HttpListener<BaseBean<Device>>() {
|
||||
private int result;
|
||||
|
||||
@Override
|
||||
public void success(BaseBean<Device> baseBean) {
|
||||
result = baseBean.result;
|
||||
if (result == Commons.NetWork.Success) {
|
||||
// deviceObj = baseBean.data;
|
||||
// deviceObj.setMacAddress(currentDevice.getMac());
|
||||
// /**
|
||||
// //当服务器返回的设备控制器数和定时器数为null的时候 不跳转进入主界面
|
||||
// */
|
||||
// if (deviceObj.getControlAmount() == null || deviceObj.getTimerAmount() == null) {
|
||||
// L.i("第一次获取空");
|
||||
//
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Exception e, String msg) {
|
||||
result = Commons.NetWork.ERROR;
|
||||
}
|
||||
}, Commons.USER.getUserId(), "e4b06318fd98");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -76,6 +76,12 @@ public class CheckWifiUtil {
|
|||
ToastUtil.show(activity.getApplicationContext(), "请输入路由器名称");
|
||||
return false;
|
||||
}
|
||||
wifipwd = wifipwd.trim();
|
||||
if (TextUtils.isEmpty(wifipwd)) {
|
||||
ToastUtil.show(activity.getApplicationContext(), "请输入路由器密码");
|
||||
return false;
|
||||
}
|
||||
|
||||
WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(WIFI_SERVICE);
|
||||
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
ToastUtil.show(activity.getApplicationContext(), "缺少联网权限");
|
||||
|
|
@ -112,11 +118,7 @@ public class CheckWifiUtil {
|
|||
return false;
|
||||
}
|
||||
|
||||
wifipwd = wifipwd.trim();
|
||||
if (TextUtils.isEmpty(wifipwd)) {
|
||||
ToastUtil.show(activity.getApplicationContext(), "请输入路由器密码");
|
||||
return false;
|
||||
}
|
||||
|
||||
//保存wifiname和wifipwd
|
||||
// KVUtil.setString(wifiname, wifipwd);
|
||||
|
||||
|
|
|
|||
|
|
@ -122,6 +122,7 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
private String bssid = "";
|
||||
private SPUtil sp;
|
||||
private Dialog waterTempDialog;
|
||||
String type = null;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
|
@ -138,7 +139,7 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
initWifiName();
|
||||
checkWifiUtil = new CheckWifiUtil();
|
||||
airKissUtil = new AirKissUtil();
|
||||
String type = getIntent().getStringExtra("type");
|
||||
type = getIntent().getStringExtra("type");
|
||||
devicetype = getIntent().getStringExtra("devicetype");
|
||||
L.i("jjia----------------devicetype="+devicetype);
|
||||
airKissUtil.init(this,devicetype);
|
||||
|
|
@ -154,14 +155,8 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
switchUIConnectType(curConnectType);
|
||||
|
||||
if ("bluetooth".equals(type)){ // 主动去联网
|
||||
|
||||
currentDevice = getIntent().getParcelableExtra("device");
|
||||
if (currentDevice!=null){
|
||||
showProgressDialogCancelble("开始连接蓝牙设备");
|
||||
connectBle();
|
||||
}
|
||||
}
|
||||
|
||||
EventBus.getDefault().register(this);
|
||||
}
|
||||
|
||||
|
|
@ -385,8 +380,8 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
public void connectDevice() {
|
||||
airKissUtil.stopAirKiss();
|
||||
//根据选择的联网类型跳转不同的逻辑
|
||||
String ssid = tvWifiName.getText().toString();
|
||||
String pwd = tvWifiPwd.getText().toString();
|
||||
String ssid = tvWifiName.getText().toString().trim();
|
||||
String pwd = tvWifiPwd.getText().toString().trim();
|
||||
|
||||
//一系列判断逻辑
|
||||
boolean canConnect = checkWifiUtil.checkWifiCanUse(this, ssid, pwd);
|
||||
|
|
@ -423,13 +418,20 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
break;
|
||||
case BlueTooth:
|
||||
|
||||
|
||||
|
||||
boolean support = BleManager.getInstance().isSupportBle();
|
||||
if (!support){
|
||||
ToastUtil.show(NewBindDeviceActivity.this,"当前设备不支持低功耗蓝牙");
|
||||
} else {
|
||||
boolean enable = BleManager.getInstance().isBlueEnable();
|
||||
if (enable){
|
||||
blueScan();
|
||||
if (TextUtils.isEmpty(type)){
|
||||
blueScan();
|
||||
}else {
|
||||
showProgressDialogCancelble("开始连接蓝牙设备");
|
||||
connectBle();
|
||||
}
|
||||
}else {
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(NewBindDeviceActivity.this);
|
||||
dialog.setCancelable(false);
|
||||
|
|
@ -787,7 +789,7 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
||||
ToastUtil.show(NewBindDeviceActivity.this,"连接断开了");
|
||||
// ToastUtil.show(NewBindDeviceActivity.this,"连接断开了");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ package com.ifish.basebean;
|
|||
|
||||
public class EventBean {
|
||||
|
||||
public String name;
|
||||
public String name;
|
||||
|
||||
public static String BLUETOOTH_START_SCAN = "start_scan";
|
||||
public static String BLUETOOTH_STOP_SCAN = "stop_scan";
|
||||
|
||||
public EventBean(String name){
|
||||
this.name = name;
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ import android.view.ViewGroup;
|
|||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.multidex.MultiDexApplication;
|
||||
|
||||
import com.alibaba.sdk.android.push.CloudPushService;
|
||||
|
|
@ -32,9 +34,13 @@ import com.alibaba.sdk.android.push.register.MiPushRegister;
|
|||
import com.clj.fastble.BleManager;
|
||||
import com.clj.fastble.scan.BleScanRuleConfig;
|
||||
import com.ifish.activity.BuildConfig;
|
||||
import com.ifish.basebean.EventBean;
|
||||
import com.ifish.utils.ActivityManager;
|
||||
import com.ifish.utils.ImageDownLoader;
|
||||
import com.ifish.utils.KVUtil;
|
||||
import com.ifish.utils.L;
|
||||
import com.ifish.utils.SystemUtil;
|
||||
import com.ifish.utils.ToastUtil;
|
||||
import com.ifish.utils.TrustAllCerts;
|
||||
import com.p2p.core.P2PSpecial.P2PSpecial;
|
||||
//import com.tencent.bugly.crashreport.CrashReport;
|
||||
|
|
@ -54,6 +60,7 @@ import javax.net.ssl.SSLSession;
|
|||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
/**
|
||||
|
|
@ -81,6 +88,8 @@ public class BaseApplication extends MultiDexApplication {
|
|||
// public static String BASE = "http://139.196.24.156:7080/";//测试环境 接口访问地址
|
||||
public static String BASE_URL = BASE + "api/";// 接口URL
|
||||
public static final String SERVICE_UUID = "000000ff-0000-1000-8000-00805f9b34fb";
|
||||
|
||||
private int count = 0;
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
|
@ -109,6 +118,59 @@ public class BaseApplication extends MultiDexApplication {
|
|||
.setScanTimeOut(20000) // 扫描超时时间,可选,默认10秒;小于等于0表示不限制扫描时间
|
||||
.build();
|
||||
BleManager.getInstance().initScanRule(scanRuleConfig);
|
||||
|
||||
|
||||
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
|
||||
@Override
|
||||
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStarted(@NonNull Activity activity) {
|
||||
if (count==0){
|
||||
Activity activityName = ActivityManager.getInstance().lastActivity();
|
||||
if (activityName!=null && activityName.toString().contains("MainTabActivity")){
|
||||
EventBean eventBean = new EventBean(EventBean.BLUETOOTH_START_SCAN);
|
||||
EventBus.getDefault().post(eventBean);
|
||||
}
|
||||
}
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResumed(@NonNull Activity activity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityPaused(@NonNull Activity activity) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStopped(@NonNull Activity activity) {
|
||||
count--;
|
||||
if (count==0){
|
||||
Activity activityName = ActivityManager.getInstance().lastActivity();
|
||||
if (activityName!=null && activityName.toString().contains("MainTabActivity")){
|
||||
EventBean eventBean = new EventBean(EventBean.BLUETOOTH_STOP_SCAN);
|
||||
EventBus.getDefault().post(eventBean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroyed(@NonNull Activity activity) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ import com.ifish.adapter.ViewPagerDefaultAdapter;
|
|||
import com.ifish.basebean.AdInfos;
|
||||
import com.ifish.basebean.BaseBean;
|
||||
import com.ifish.basebean.Device;
|
||||
import com.ifish.basebean.EventBean;
|
||||
import com.ifish.basebean.GradeRuleInfo;
|
||||
import com.ifish.basebean.GradeValue;
|
||||
import com.ifish.basebean.Information;
|
||||
|
|
@ -2139,6 +2140,12 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
|
|||
}
|
||||
}
|
||||
|
||||
public void onEventMainThread(EventBean eventBean){
|
||||
if (eventBean!= null){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
|
|
|
|||
|
|
@ -35,9 +35,13 @@ public class ActivityManager {
|
|||
// 删除Activity于容器中
|
||||
public void delActivity(Activity activity) {
|
||||
activityList.remove(activity);
|
||||
}
|
||||
|
||||
// 遍历所有Activity并finish
|
||||
}
|
||||
|
||||
public Activity lastActivity() {
|
||||
return activityList.get(activityList.size()-1);
|
||||
}
|
||||
|
||||
// 遍历所有Activity并finish
|
||||
public void exit() {
|
||||
for (Activity activity : activityList) {
|
||||
activity.finish();
|
||||
|
|
|
|||
|
|
@ -13,17 +13,14 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.ifish.activity.BluetoothActivity;
|
||||
import com.ifish.basebean.EventBean;
|
||||
import com.ifish.baseclass.BaseApplication;
|
||||
import com.ifish.bluetooth.ClsUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
|
|
|||
|
|
@ -50,24 +50,6 @@
|
|||
android:adjustViewBounds="true"
|
||||
android:src="@drawable/device_petdevice"
|
||||
android:scaleType="fitXY" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/iv_bluetooth"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:adjustViewBounds="true"
|
||||
android:src="@drawable/device_petdevice"
|
||||
android:visibility="gone"
|
||||
android:scaleType="fitXY" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/iv_bluetooth2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:adjustViewBounds="true"
|
||||
android:visibility="gone"
|
||||
android:src="@drawable/device_petdevice"
|
||||
android:scaleType="fitXY" />
|
||||
<ImageView
|
||||
android:id="@+id/iv_more"
|
||||
android:layout_width="match_parent"
|
||||
|
|
|
|||
Loading…
Reference in New Issue