package com.heshun.blecustom.activity;
import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.heshun.blecustom.R;
import com.heshun.blecustom.adapter.CMDListAdapter;
import com.heshun.blecustom.base.BaseResponseBody;
import com.heshun.blecustom.base.BleMessage;
import com.heshun.blecustom.base.Head;
import com.heshun.blecustom.entity.CMDItem;
import com.heshun.blecustom.entity.ChargeMode;
import com.heshun.blecustom.entity.requestBodyEntity.ChargeNowRequest;
import com.heshun.blecustom.entity.requestBodyEntity.DownloadPackageRequest;
import com.heshun.blecustom.entity.requestBodyEntity.GunIdRequest;
import com.heshun.blecustom.entity.requestBodyEntity.RemoteUpgradeRequest;
import com.heshun.blecustom.entity.requestBodyEntity.SetChargeModeRequest;
import com.heshun.blecustom.entity.requestBodyEntity.SetVolumeRequest;
import com.heshun.blecustom.entity.requestBodyEntity.SysInfoRequest;
import com.heshun.blecustom.entity.requestBodyEntity.TimeSyncRequest;
import com.heshun.blecustom.entity.responseBodyEntity.DownloadPackageResponse;
import com.heshun.blecustom.entity.responseBodyEntity.DownloadSuccessfullyResponse;
import com.heshun.blecustom.tools.FileUtils;
import com.heshun.blecustom.tools.ToolsUtils;
import com.heshun.blecustom.wheel.DividerItemDecoration;
import com.heshun.blecustom.wheel.StrericWheelAdapter;
import com.heshun.blecustom.wheel.WheelView;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import static com.heshun.blecustom.tools.ToolsUtils.writeMsg;
/**
* author:Jics
* 2017/6/5 16:20
*/
public class CMDListActivity extends Activity implements CMDListAdapter.CmdItemClickListener, View.OnClickListener {
private String TAG = "cmd_request";
public static final int TIME_OUT = 1;//超时时间
private RecyclerView recyclerView;
private AlertDialog.Builder loadingDialog;
private AlertDialog dialog;
private Timer timer;
private int timeOut = TIME_OUT;
private byte currentCMD = (byte) 0xFF;
private Button btn_dis_connect;
private Button btn_connect;
private boolean fileSucc = false;//文件是否加载成功
private boolean isDownloadding = false;//是否有下载任务
private byte[] fileblocks;//文件的byte流
// ----------- 蓝牙部分 -----------
private boolean isTiming = false;//是否正在计时
byte[] responseBuffer = new byte[0];//接收数据buff
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
private String mDeviceName;
private String mDeviceAddress;
private BluetoothLeService mBluetoothLeService;
private boolean mConnected = false;
private BluetoothGattCharacteristic mWriteCharacteristic;//全局可读写的特征()
// 蓝牙服务的ServiceConnection
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
//通过binder获取到BluetoothLeService的实体
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "初始化蓝牙连接失败");
finish();
}
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private String filePath;//文件路径
// ------------ END -------------
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cmd_list);
initView();
}
/**
* 初始化视图
*/
private void initView() {
//获取bundle里的数据
final Intent intent = getIntent();
mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
//绑定并启动蓝牙服务
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
btn_dis_connect = (Button) findViewById(R.id.btn_dis_connect);
btn_dis_connect.setText(btn_dis_connect.getText().toString() + "( " + mDeviceName + " )");
btn_connect = (Button) findViewById(R.id.btn_connect);
btn_connect.setText(btn_connect.getText().toString() + "( " + mDeviceName + " )");
btn_connect.setOnClickListener(this);
btn_dis_connect.setOnClickListener(this);
recyclerView = (RecyclerView) findViewById(R.id.list_contener);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
CMDListAdapter cmdListAdapter = new CMDListAdapter(this, makeCMDList());
cmdListAdapter.setCmdItemClickListener(this);
recyclerView.setAdapter(cmdListAdapter);
loadingDialog = new AlertDialog.Builder(this);
}
/**
* 生成列表
* 可重构出去
*
* @return
*/
private List<CMDItem> makeCMDList() {
List<CMDItem> cmdList = new ArrayList<>();
cmdList.add(new CMDItem(false, "时间同步", Head.CMD_TIME_SYNCHRONIZATION));
cmdList.add(new CMDItem(false, "查询系统信息", Head.CMD_QUERY_SYSTEM_INFORMATION));
cmdList.add(new CMDItem(true, "设置音量", Head.CMD_SET_VOLUME));
cmdList.add(new CMDItem(true, "设置充电模式", Head.CMD_SET_CHARGE_MODE));
cmdList.add(new CMDItem(true, "立即充电", Head.CMD_CHARGE_NOW));
cmdList.add(new CMDItem(true, "查询状态", Head.CMD_QUERY_STATE));
cmdList.add(new CMDItem(true, "结束充电", Head.CMD_END_CHARGE));
cmdList.add(new CMDItem(true, "查询充电历史记录", Head.CMD_QUERY_CHARGING_HISTORY));
cmdList.add(new CMDItem(true, "查询累计电量", Head.CMD_QUERY_CUMULATIVE_CHARGE));
cmdList.add(new CMDItem(true, "启动远程升级", Head.CMD_START_REMOTE_UPGRADE));
return cmdList;
}
@Override
public void onCmdClick(boolean isJump, byte cmd) {
if (isDownloadding) {//远程升级过程中不允许其他操作
Toast.makeText(CMDListActivity.this, "远程升级过程中不允许其他操作", Toast.LENGTH_SHORT).show();
} else {
if (mConnected) {
if (mWriteCharacteristic != null) {
//发送(head+body)命令
makeCMD(cmd);
} else {
Toast.makeText(CMDListActivity.this, "未配对此设备或此设备无可匹配UUID的特征值", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "蓝牙未连接", Toast.LENGTH_SHORT).show();
}
}
}
/**
* 信息出口
* 普通指令信息
* 响应数据处理,接收普通指令数据
* 数据包大小限制20byte,分包接收两条数据间隔1秒视为数据接收完毕
* 超时或者接收数据完整后将请求命令设为失�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 自定义蓝牙传输协议的充电桩控制器.zip
资源推荐
资源详情
资源评论
收起资源包目录
自定义蓝牙传输协议的充电桩控制器.zip (111个子文件)
gradlew.bat 2KB
.gitignore 118B
.gitignore 7B
build.gradle 952B
build.gradle 498B
settings.gradle 15B
gradlew 5KB
gradle-wrapper.jar 52KB
CMDListActivity.java 35KB
WheelView.java 23KB
DeviceControlActivity.java 17KB
BluetoothLeService.java 13KB
ByteUtils.java 10KB
FileUtils.java 10KB
Ymodem.java 9KB
DeviceScanActivity.java 8KB
ToolsUtils.java 6KB
SysInfoResponse.java 6KB
Head.java 5KB
GetTime.java 5KB
QueryStateResponse.java 4KB
BleMessage.java 4KB
DividerItemDecoration.java 4KB
ChargeHistoryResponse.java 3KB
CMDListAdapter.java 3KB
CRC16.java 3KB
Test.java 3KB
RemoteUpgradeRequest.java 2KB
SetChargeModeRequest.java 2KB
SampleGattAttributes.java 2KB
ToolseTest.java 2KB
EPAdapter.java 1KB
ImageTextButton.java 1KB
StrericWheelAdapter.java 1KB
CumulativeChargeResponse.java 1KB
GunStatus.java 1KB
DownloadPackageResponse.java 1KB
OnWheelChangedListener.java 1KB
WheelAdapter.java 1KB
ChargeMode.java 1KB
OnWheelScrollListener.java 1KB
GetCurrentTime.java 1000B
TimeSyncResponse.java 934B
SetChargeModeResponse.java 930B
ChargeNowResponse.java 923B
SetVolumeResponse.java 910B
DownloadPackageRequest.java 885B
CMDJumpHereActivity.java 881B
StopChargeResponse.java 864B
ChargeNowRequest.java 829B
DownloadSuccessfullyResponse.java 776B
ExampleInstrumentedTest.java 717B
CMDItem.java 667B
ElectricityParameter.java 664B
GunIdRequest.java 656B
SetVolumeRequest.java 650B
DataSimulation.java 603B
RemoteUpgradeResponse.java 458B
BaseResponseBody.java 434B
ExampleUnitTest.java 383B
TimeSyncRequest.java 374B
SysInfoRequest.java 316B
DownloadSuccessfullyRequest.java 294B
BaseRequestBody.java 216B
TimeSync.java 128B
ic_launcher.png 25KB
ic_launcher.png 23KB
ic_launcher.png 21KB
ic_launcher.png 20KB
ic_launcher.png 20KB
ic_launcher.png 19KB
ic_launcher.png 19KB
ic_launcher.png 18KB
ic_launcher.png 17KB
stile.9.png 196B
proguard-rules.pro 653B
gradle.properties 730B
gradle-wrapper.properties 233B
dialog_set_charge_mode.xml 6KB
gatt_services_characteristics.xml 5KB
time_picker.xml 4KB
dialog_remote_update.xml 3KB
AndroidManifest.xml 2KB
strings.xml 2KB
strings.xml 2KB
ep_item.xml 2KB
activity_main.xml 1KB
template-styles.xml 1KB
listitem_device.xml 1KB
activity_cmd_list.xml 1KB
dialog_charge_now.xml 1KB
gatt_services.xml 1KB
main.xml 1KB
dialog_gun_num.xml 1KB
template-dimens.xml 1KB
dialog_set_volume.xml 1KB
wheel_bg.xml 1KB
base-strings.xml 984B
actionbar_indeterminate_progress.xml 942B
layout_bg.xml 940B
共 111 条
- 1
- 2
资源评论
土豆片片
- 粉丝: 1576
- 资源: 5636
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功