// Modify according to FASTBLE

public abstract class BaseChengActivity {

public BluetoothService mBluetoothService;
public WeightAdapter mResultAdapter;                  
public static final int PROPERTY_NOTIFY = 4;         
public static final UUID mServeUUID = UUID.fromString("0000ffe0-0000-1000-8000-00805F9B34FB");   //特定服务 UUOID 
public static final UUID mCHarUUID = UUID.fromString("0000ffe1-0000-1000-8000-00805F9B34FB");    //特定CHar UUID
public BluetoothGattService mBluetoothGattService = null;
public BluetoothGattCharacteristic mBluetoothGattCharacteristic = null;
public ChengCallBack cheng;
public BluetoothCallBack bluetoothCallBack;
public BluetoothGatt gatt;

@Override
public final void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 12:
            if (grantResults.length > 0) {
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        onPermissionGranted(permissions[i]);
                    }
                }
            }
            break;
    }
}

public void registerCheng(ChengCallBack cheng) {
    this.cheng = cheng;
}

public void registerBluetoothCallBack(BluetoothCallBack bluetoothCallBack) {
    this.bluetoothCallBack = bluetoothCallBack;
}

protected void checkPermissions() {
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
    List<String> permissionDeniedList = new ArrayList<>();
    for (String permission : permissions) {
        int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            onPermissionGranted(permission);
        } else {
            permissionDeniedList.add(permission);
        }
    }
    if (!permissionDeniedList.isEmpty()) {
        String[] deniedPermissions = permissionDeniedList.toArray(new String[permissionDeniedList.size()]);
        ActivityCompat.requestPermissions(this, deniedPermissions, 12);
    }
}

private void onPermissionGranted(String permission) {
    switch (permission) {
        case Manifest.permission.ACCESS_FINE_LOCATION:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !checkGPSIsOpen()) {
                new AlertDialog.Builder(this)
                        .setTitle(R.string.notifyTitle)
                        .setMessage(R.string.gpsNotifyMsg)
                        .setNegativeButton(R.string.cancel,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                })
                        .setPositiveButton(R.string.setting,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                        startActivityForResult(intent, 1);
                                    }
                                })

                        .setCancelable(false)
                        .show();
            } else {
                startScan();
            }
            break;
    }
}

private boolean checkGPSIsOpen() {
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (locationManager == null)
        return false;
    return locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (checkGPSIsOpen()) {
            startScan();
        }
    }
}

public void bindService() {
    Intent bindIntent = new Intent(this, BluetoothService.class);
    this.bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
}

public void unbindService() {
    this.unbindService(mFhrSCon);
}

public ServiceConnection mFhrSCon = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService();
        mBluetoothService.setScanCallback(callback);
        mBluetoothService.scanDevice();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mBluetoothService = null;
    }
};

public BluetoothService.Callback callback = new BluetoothService.Callback() {
    @Override
    public void onStartScan() {
        Log.e("Darren", "onStartScan");
        bluetoothCallBack.onStartScan();
    }

    @Override
    public void onScanning(ScanResult result) {
        Log.e("Darren", "onScanning");
        bluetoothCallBack.onScanning(result);
    }

    @Override
    public void onScanComplete() {
        Log.e("Darren", "onScanComplete");

    }

    @Override
    public void onConnecting() {
        Log.e("Darren", "onConnecting");
        LoadingDialog.loading();
        bluetoothCallBack.onConnecting();
    }

    @Override
    public void onConnectFail() {
        Log.e("Darren", "onConnectFail");
        LoadingDialog.loadFail();
        showToast("电子秤蓝牙连接失败");
        bluetoothCallBack.onConnectFail();
        if (mBluetoothService != null) {
            mBluetoothService.closeConnect();
        }
    }

    @Override
    public void onDisConnected() {
        Log.e("Darren", "onDisConnected");
        LoadingDialog.loadFail();
        showToast("电子秤蓝牙连接失败");
        bluetoothCallBack.onDisConnected();
        if (mBluetoothService != null) {
            mBluetoothService.closeConnect();
        }
    }

    @Override
    public void onServicesDiscovered() {
        Log.e("Darren", "onServicesDiscovered");
        bluetoothCallBack.onServicesDiscovered();
    }
};

public void startScan() {
    if (mBluetoothService == null) {
        bindService();
    } else {
        mBluetoothService.scanDevice();
    }
}

public void bluetoothinit() {
    if (mBluetoothService.getGatt() == null) {
        LoadingDialog.loadFail();
        showToast("电子秤蓝牙连接失败");
        return;
    }
    gatt = mBluetoothService.getGatt();
    List<BluetoothGattService> listBluetoothGattService = gatt.getServices();
    for (int i = 0; i < listBluetoothGattService.size(); i++) {
        BluetoothGattService oneBluetoothGattService = listBluetoothGattService.get(i);
        if (oneBluetoothGattService.getUuid().toString().equals(mServeUUID.toString())) {
            mBluetoothGattService = oneBluetoothGattService;
        }
    }
    if (mBluetoothGattService == null) {
        LoadingDialog.loadFail();
        showToast("电子秤蓝牙连接失败");
        return;
    }
    mBluetoothService.setService(mBluetoothGattService);
    List<BluetoothGattCharacteristic> listBluetoothGattCharacteristic = mBluetoothGattService.getCharacteristics();
    for (int i = 0; i < listBluetoothGattCharacteristic.size(); i++) {
        BluetoothGattCharacteristic oneBluetoothGattCharacteristic = listBluetoothGattCharacteristic.get(i);
        if (oneBluetoothGattCharacteristic.getUuid().toString().equals(mCHarUUID.toString())) {
            mBluetoothGattCharacteristic = oneBluetoothGattCharacteristic;
        }
    }
    if (mBluetoothGattCharacteristic == null) {
        LoadingDialog.loadFail();
        showToast("电子秤蓝牙连接失败");
        return;
    }
    mBluetoothService.setCharacteristic(mBluetoothGattCharacteristic);
    mBluetoothService.setCharaProp(PROPERTY_NOTIFY);
    LoadingDialog.loadSuccess();
    bluetoothCallBack.onScanComplete();

}


@Override
public void start() {
    if (mBluetoothGattService == null) {
        showToast("电子秤蓝牙未连接");
        return;
    }
    mBluetoothService.notify(mBluetoothGattService.getUuid().toString(), mBluetoothGattCharacteristic.getUuid().toString(), new BleCharacterCallback() {
        @Override
        public void onSuccess(BluetoothGattCharacteristic characteristic) {

            byte[] weight = characteristic.getValue();
            String mweightStr = ConvertHelper.byteArray2AsciiStr(weight);
            Log.i("xioa", mweightStr);
            cheng.onSuccess(mweightStr.replace("=", ""));
        }

        @Override
        public void onFailure(BleException exception) {
            Log.i("Darren", "onFailure" + exception.getDescription());
            showToast("电子秤蓝牙连接失败");
            bluetoothCallBack.onConnectFail();
        }

        @Override
        public void onInitiatedResult(boolean result) {
            Log.i("Darren", "onInitiatedResult" + result);
            if (!result) {
                bluetoothCallBack.onConnectFail();
            }
        }
    });
}

@Override
public void stop() {
    if (mBluetoothGattService == null) {
        return;
    }
    mBluetoothService.stopNotify(mBluetoothGattService.getUuid().toString(), mBluetoothGattCharacteristic.getUuid().toString());
}

@Override
public boolean isWeight() {
    if (mBluetoothGattService == null) {
        showToast("电子秤蓝牙未连接");
        return false;
    }
    return true;
}
Copy the code

}