public class BleUtil {
public static final int ERROR_CODE_TIMEOUT = 100; //超时
public static final int ERROR_CODE_GATT = 101; //协议错误
public static final int ERROR_CODE_OTHER = 102; //其他错误
public static final int ERROR_CODE_NOT_FOUND_DEVICE = 103; //未发现设备
public static final int ERROR_CODE_BLUETOOTH_NOT_ENABLE = 104; //蓝牙不允许
public static final int ERROR_CODE_SCAN_FAILED = 105; //扫描失败
public static final String SERVICE_UUID = "0000fee1-0000-1000-8000-00805f9b34fb";
public static final String BYTES_ID = "F501001175310000000012034150508800";
//
private BleConnectionSuccess bleConnectionSuccess;
//
static BleManager bleManager;
private BluetoothGatt mBluetoothGatt = null;
private Context mContext;
private String macAddress;
private MyDialog mLoadingDialog;
//
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice device;
//
private static final int HANDLER_CONNECT = 2;
private static final int HANDLER_CHECK_CARLOCK = 0;
private static final int HANDLER_INIT_BLUETOOTH = 3;
private static final int HANDLER_ERROE = 1;//错误
private static final int HANDLER_DROP_CARLOCK = 4;
//
BluetoothGatt bluetoothGatt;
//
private String setMsg;
private MyDialog bleDialog;
//
private int bleFrequency;
/**
*连接成功回调接口
*/
public void setBleConnectionSuccess(BleConnectionSuccess bleConnectionSuccess){
this.bleConnectionSuccess = bleConnectionSuccess;
}
private Handler delayHanlder = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==HANDLER_CONNECT){ //扫描到后的匹配
}else if(msg.what==HANDLER_CHECK_CARLOCK){ //连接成功
mLoadingDialog.dismiss();
bleClose();//清楚之前连接过的服务
bleConnectionSuccess.getBle(HANDLER_CHECK_CARLOCK);
}else if(msg.what==HANDLER_INIT_BLUETOOTH){ //开始
bleFrequency++;
bleClose();//清楚之前连接过的服务
getBleConnection();
}else if(msg.what==HANDLER_ERROE){ //连接失败
mLoadingDialog.dismiss();
bleClose();//清楚之前连接过的服务
bleConnectionSuccess.getBle(HANDLER_ERROE);
}else if(msg.what==HANDLER_DROP_CARLOCK){//写入
if (null != bluetoothGatt){
BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(SERVICE_UUID));
if (null != service){
for (BluetoothGattCharacteristic bluetoothGattCharacteristic : service.getCharacteristics()) {
if ((bluetoothGattCharacteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
bleManager.writeDevice(bluetoothGattCharacteristic.getService().getUuid().toString(),
bluetoothGattCharacteristic.getUuid().toString(),
HexUtil.hexStringToBytes(BYTES_ID),mBleCharacterCallback);
}
}
}else{
mLoadingDialog.dismiss();
bleClose();//清楚之前连接过的服务
bleConnectionSuccess.getBle(HANDLER_ERROE);
}
}else{
mLoadingDialog.dismiss();
bleClose();//清楚之前连接过的服务
bleConnectionSuccess.getBle(HANDLER_ERROE);
}
}
}
};
/**
* 权限
* @param context
* @param mac
*/
public void getFastBle(Context context, String mac,String msg) {
mContext = context;
macAddress = mac;
setMsg = msg;
bleFrequency = 0;
if (null == mLoadingDialog){
mLoadingDialog = new MyDialog.Builder(mContext).createUpdataMsgLoadingDialog("连接中..."); //加载框
}
//权限
new RxPermissions((Activity) context)
.request(Manifest.permission.BLUETOOTH)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean aBoolean) {
if(aBoolean){
mLoadingDialog.show();
delayHanlder.sendEmptyMessageDelayed(HANDLER_INIT_BLUETOOTH,100);
} else{
Toast.makeText(mContext, R.string.check_permission, Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 连接mac蓝牙 第一种方法
*/
public void getBleConnection(){
if (bleManager == null){
bleManager = new BleManager(mContext);
}
if(!bleManager.isBlueEnable()){ //开启蓝牙
setBleDialog(); //开启蓝牙的提示框
mLoadingDialog.dismiss();
//bleManager.enableBluetooth(); //给他打开蓝牙
return;
}
boolean isBle = bleManager.isSupportBle(); //是否支持Ble
if (isBle){
//scanNameAndConnect 名字 scanMacAndConnect MAC地址
bleManager.scanMacAndConnect(
macAddress,
5000,
false,
new BleGattCallback() {
@Override
public void onFoundDevice(ScanResult scanResult) { //找到设备的回调;
Log.i("jam", "找到设备的回调: "+scanResult.getDevice().getName()+"---"+scanResult.getRssi()+"---"+scanResult.getDevice().getAddress());
}
@Override
public void onConnecting(BluetoothGatt gatt, int status) { //正在连接的回调;
Log.i("jam", "正在连接的回调: "+status);
}
@Override
public void onConnectError(BleException exception) { //连接未成功的回调,通过解析BleException来判断具体未成功的原因;
Log.i("jam", "连接未成功的回调: "+exception.getCode());
//连接失败时从新连接
if (bleFrequency < 3){
delayHanlder.sendEmptyMessageDelayed(HANDLER_INIT_BLUETOOTH,100);
}else{
delayHanlder.sendEmptyMessageDelayed(HANDLER_ERROE, 1000);
}
}
@Override
public void onConnectSuccess(BluetoothGatt gatt, int status) { //连接成功的回调;
Log.i("jam", "连接成功的回调: "+status);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { //发现服务的回调;
Log.i("jam", "发现服务的回调: "+status);
//把操作放在这里,连接成功在此之前执行
if (status == BluetoothGatt.GATT_SUCCESS){
if(gatt.getService(UUID.fromString(SERVICE_UUID)) != null){
bluetoothGatt = gatt;
delayHanlder.sendEmptyMessageDelayed(HANDLER_DROP_CARLOCK,1000);
}else{
delayHanlder.sendEmptyMessageDelayed(HANDLER_ERROE, 1000);
}
} else {
delayHanlder.sendEmptyMessageDelayed(HANDLER_ERROE, 1000);
}
}
@Override
public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) { //连接断开的回调;
Log.i("jam", "连接断开的回调: "+status+"---"+exception.getDescription().toString());
//连接失败时从新连接
if (bleFrequency < 5){
delayHanlder.sendEmptyMessageDelayed(HANDLER_INIT_BLUETOOTH,100);
}else{
delayHanlder.sendEmptyMessageDelayed(HANDLER_ERROE, 1000);
}
}
});
}else{
Toast.makeText(mContext, "手机不支持BLE", Toast.LENGTH_SHORT).show();
}
}
BleCharacterCallback mBleCharacterCallback = new BleCharacterCallback() {
@Override
public void onSuccess(BluetoothGattCharacteristic characteristic) {
Log.i("jam","写入成功");
delayHanlder.sendEmptyMessageDelayed(HANDLER_CHECK_CARLOCK,1000);
}
@Override
public void onFailure(BleException exception) {
Log.i("jam","写入异常"+exception.getDescription());
}
@Override
public void onInitiatedResult(boolean result) {
Log.i("jam","写入是否成功"+result);
if (!result){
delayHanlder.sendEmptyMessageDelayed(HANDLER_ERROE, 1000);
}
}
};
/**
* 未打开蓝牙的提示
*/
public void setBleDialog(){
if (null == bleDialog){
bleDialog = new MyDialog.Builder(mContext).createProposalDialog(new MyDialog.OnChooseYesOrNoListener() {
@Override
public void chooseYes() {
}
@Override
public void chooseNo() {
}
},"提示",setMsg,"知道了");
}
bleDialog.show();
}
/**
* 成功的回调
*/
public interface BleConnectionSuccess{
public void getBle(int status);
}
public void bleClose() {
if (bluetoothGatt != null){
bluetoothGatt.disconnect();
bluetoothGatt.close();
bluetoothGatt = null;
}
if (bleManager != null){
bleManager.closeBluetoothGatt();//清除下
}
}
}
Android Ble连接,Ble133异常处理,写入消息
最新推荐文章于 2024-03-24 21:55:58 发布